Skip to content

Commit

Permalink
refactor: warn on and satisfy clippy::{ptr_as_ptr,ref_as_ptr} in `w…
Browse files Browse the repository at this point in the history
…gpu-{core,hal}`

…using `cargo +1.79.0 clippy --workspace --all-features --all-targets
--fix`. 😀
  • Loading branch information
ErichDonGubler committed Jul 24, 2024
1 parent fa93676 commit 28a6024
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ impl Global {
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
let data = trace.make_binary("spv", unsafe {
std::slice::from_raw_parts(source.as_ptr() as *const u8, source.len() * 4)
std::slice::from_raw_parts(source.as_ptr().cast::<u8>(), source.len() * 4)
});
trace.add(trace::Action::CreateShaderModule {
id: fid.id(),
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl Global {
) -> Result<SurfaceId, CreateSurfaceError> {
profiling::scope!("Instance::instance_create_surface_from_visual");
self.instance_create_surface_dx12(id_in, |inst| unsafe {
inst.create_surface_from_visual(visual as _)
inst.create_surface_from_visual(visual.cast())
})
}

Expand Down Expand Up @@ -672,7 +672,7 @@ impl Global {
) -> Result<SurfaceId, CreateSurfaceError> {
profiling::scope!("Instance::instance_create_surface_from_swap_chain_panel");
self.instance_create_surface_dx12(id_in, |inst| unsafe {
inst.create_surface_from_swap_chain_panel(swap_chain_panel as _)
inst.create_surface_from_swap_chain_panel(swap_chain_panel.cast())
})
}

Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ the documentation for `wgpu-core` is empty unless built with
rustdoc::private_intra_doc_links
)]
#![warn(
clippy::ptr_as_ptr,
trivial_casts,
trivial_numeric_casts,
unsafe_op_in_unsafe_fn,
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/auxil/renderdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl RenderDoc {
match unsafe { get_api(10401, &mut obj) } {
1 => RenderDoc::Available {
api: RenderDocApi {
api: unsafe { *(obj as *mut renderdoc_sys::RENDERDOC_API_1_4_1) },
api: unsafe { *obj.cast::<renderdoc_sys::RENDERDOC_API_1_4_1>() },
lib: renderdoc_lib,
},
},
Expand Down
20 changes: 10 additions & 10 deletions wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl super::Adapter {
unsafe {
device.CheckFeatureSupport(
d3d12_ty::D3D12_FEATURE_FEATURE_LEVELS,
&mut device_levels as *mut _ as *mut _,
ptr::from_mut(&mut device_levels).cast(),
mem::size_of::<d3d12_ty::D3D12_FEATURE_DATA_FEATURE_LEVELS>() as _,
)
};
Expand All @@ -111,7 +111,7 @@ impl super::Adapter {
assert_eq!(0, unsafe {
device.CheckFeatureSupport(
d3d12_ty::D3D12_FEATURE_ARCHITECTURE,
&mut features_architecture as *mut _ as *mut _,
ptr::from_mut(&mut features_architecture).cast(),
mem::size_of::<d3d12_ty::D3D12_FEATURE_DATA_ARCHITECTURE>() as _,
)
});
Expand Down Expand Up @@ -156,7 +156,7 @@ impl super::Adapter {
assert_eq!(0, unsafe {
device.CheckFeatureSupport(
d3d12_ty::D3D12_FEATURE_D3D12_OPTIONS,
&mut options as *mut _ as *mut _,
ptr::from_mut(&mut options).cast(),
mem::size_of::<d3d12_ty::D3D12_FEATURE_DATA_D3D12_OPTIONS>() as _,
)
});
Expand All @@ -167,7 +167,7 @@ impl super::Adapter {
let hr = unsafe {
device.CheckFeatureSupport(
d3d12_ty::D3D12_FEATURE_D3D12_OPTIONS2,
&mut features2 as *mut _ as *mut _,
ptr::from_mut(&mut features2).cast(),
mem::size_of::<d3d12_ty::D3D12_FEATURE_DATA_D3D12_OPTIONS2>() as _,
)
};
Expand All @@ -180,7 +180,7 @@ impl super::Adapter {
let hr = unsafe {
device.CheckFeatureSupport(
21, // D3D12_FEATURE_D3D12_OPTIONS3
&mut features3 as *mut _ as *mut _,
ptr::from_mut(&mut features3).cast(),
mem::size_of::<crate::dx12::types::D3D12_FEATURE_DATA_D3D12_OPTIONS3>() as _,
)
};
Expand Down Expand Up @@ -210,7 +210,7 @@ impl super::Adapter {
if 0 == unsafe {
device.CheckFeatureSupport(
7, // D3D12_FEATURE_SHADER_MODEL
&mut sm as *mut _ as *mut _,
ptr::from_mut(&mut sm).cast(),
mem::size_of::<crate::dx12::types::D3D12_FEATURE_DATA_SHADER_MODEL>()
as _,
)
Expand Down Expand Up @@ -337,7 +337,7 @@ impl super::Adapter {
let hr = unsafe {
device.CheckFeatureSupport(
d3d12_ty::D3D12_FEATURE_FORMAT_SUPPORT,
&mut bgra8unorm_info as *mut _ as *mut _,
ptr::from_mut(&mut bgra8unorm_info).cast(),
mem::size_of::<d3d12_ty::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as _,
)
};
Expand All @@ -353,7 +353,7 @@ impl super::Adapter {
let hr = unsafe {
device.CheckFeatureSupport(
d3d12_ty::D3D12_FEATURE_D3D12_OPTIONS1,
&mut features1 as *mut _ as *mut _,
ptr::from_mut(&mut features1).cast(),
mem::size_of::<d3d12_ty::D3D12_FEATURE_DATA_D3D12_OPTIONS1>() as _,
)
};
Expand All @@ -378,7 +378,7 @@ impl super::Adapter {
let hr = unsafe {
device.CheckFeatureSupport(
37, // D3D12_FEATURE_D3D12_OPTIONS9
&mut features9 as *mut _ as *mut _,
ptr::from_mut(&mut features9).cast(),
mem::size_of::<crate::dx12::types::D3D12_FEATURE_DATA_D3D12_OPTIONS9>() as _,
)
};
Expand Down Expand Up @@ -586,7 +586,7 @@ impl crate::Adapter for super::Adapter {
assert_eq!(winerror::S_OK, unsafe {
self.device.CheckFeatureSupport(
d3d12_ty::D3D12_FEATURE_FORMAT_SUPPORT,
&mut data as *mut _ as *mut _,
ptr::from_mut(&mut data).cast(),
mem::size_of::<d3d12_ty::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as _,
)
});
Expand Down
6 changes: 3 additions & 3 deletions wgpu-hal/src/dx12/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl super::CommandEncoder {
self.pass.kind = kind;
if let Some(label) = label {
let (wide_label, size) = self.temp.prepare_marker(label);
unsafe { list.BeginEvent(0, wide_label.as_ptr() as *const _, size) };
unsafe { list.BeginEvent(0, wide_label.as_ptr().cast(), size) };
self.pass.has_label = true;
}
self.pass.dirty_root_elements = 0;
Expand Down Expand Up @@ -950,7 +950,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
self.list
.as_ref()
.unwrap()
.SetMarker(0, wide_label.as_ptr() as *const _, size)
.SetMarker(0, wide_label.as_ptr().cast(), size)
};
}
unsafe fn begin_debug_marker(&mut self, group_label: &str) {
Expand All @@ -959,7 +959,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
self.list
.as_ref()
.unwrap()
.BeginEvent(0, wide_label.as_ptr() as *const _, size)
.BeginEvent(0, wide_label.as_ptr().cast(), size)
};
}
unsafe fn end_debug_marker(&mut self) {
Expand Down
6 changes: 3 additions & 3 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ impl crate::Device for super::Device {
};
for attribute in vbuf.attributes {
input_element_descs.push(d3d12_ty::D3D12_INPUT_ELEMENT_DESC {
SemanticName: NAGA_LOCATION_SEMANTIC.as_ptr() as *const _,
SemanticName: NAGA_LOCATION_SEMANTIC.as_ptr().cast(),
SemanticIndex: attribute.shader_location,
Format: auxil::dxgi::conv::map_vertex_format(attribute.format),
InputSlot: i as u32,
Expand Down Expand Up @@ -1749,7 +1749,7 @@ impl crate::Device for super::Device {
{
unsafe {
self.render_doc
.start_frame_capture(self.raw.as_mut_ptr() as *mut _, ptr::null_mut())
.start_frame_capture(self.raw.as_mut_ptr().cast(), ptr::null_mut())
}
}
#[cfg(not(feature = "renderdoc"))]
Expand All @@ -1760,7 +1760,7 @@ impl crate::Device for super::Device {
#[cfg(feature = "renderdoc")]
unsafe {
self.render_doc
.end_frame_capture(self.raw.as_mut_ptr() as *mut _, ptr::null_mut())
.end_frame_capture(self.raw.as_mut_ptr().cast(), ptr::null_mut())
}
}

Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/dx12/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl crate::Instance for super::Instance {
let hr = unsafe {
factory5.CheckFeatureSupport(
dxgi1_5::DXGI_FEATURE_PRESENT_ALLOW_TEARING,
&mut allow_tearing as *mut _ as *mut _,
std::ptr::from_mut(&mut allow_tearing).cast(),
mem::size_of::<minwindef::BOOL>() as _,
)
};
Expand Down
6 changes: 3 additions & 3 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ impl crate::Surface for Surface {
self.factory
.unwrap_factory2()
.create_swapchain_for_composition(
device.present_queue.as_mut_ptr() as *mut _,
device.present_queue.as_mut_ptr().cast(),
&desc,
)
.into_result()
Expand All @@ -733,7 +733,7 @@ impl crate::Surface for Surface {
.clone()
.ok_or(crate::SurfaceError::Other("IDXGIFactoryMedia not found"))?
.create_swapchain_for_composition_surface_handle(
device.present_queue.as_mut_ptr() as *mut _,
device.present_queue.as_mut_ptr().cast(),
handle,
&desc,
)
Expand All @@ -745,7 +745,7 @@ impl crate::Surface for Surface {
.as_factory2()
.unwrap()
.create_swapchain_for_hwnd(
device.present_queue.as_mut_ptr() as *mut _,
device.present_queue.as_mut_ptr().cast(),
hwnd,
&desc,
)
Expand Down
5 changes: 2 additions & 3 deletions wgpu-hal/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ impl super::CommandBuffer {
}

fn add_push_constant_data(&mut self, data: &[u32]) -> Range<u32> {
let data_raw = unsafe {
std::slice::from_raw_parts(data.as_ptr() as *const _, mem::size_of_val(data))
};
let data_raw =
unsafe { std::slice::from_raw_parts(data.as_ptr().cast(), mem::size_of_val(data)) };
let start = self.data_bytes.len();
assert!(start < u32::MAX as usize);
self.data_bytes.extend_from_slice(data_raw);
Expand Down
5 changes: 2 additions & 3 deletions wgpu-hal/src/gles/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ impl super::Queue {
}
let query_data = unsafe {
slice::from_raw_parts(
temp_query_results.as_ptr() as *const u8,
temp_query_results.as_ptr().cast::<u8>(),
temp_query_results.len() * mem::size_of::<u64>(),
)
};
Expand Down Expand Up @@ -1526,8 +1526,7 @@ impl super::Queue {

debug_assert_eq!(data_required, raw.len());

let slice: &[T] =
unsafe { slice::from_raw_parts(raw.as_ptr() as *const _, COUNT) };
let slice: &[T] = unsafe { slice::from_raw_parts(raw.as_ptr().cast(), COUNT) };

slice.try_into().unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/gles/wgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl AdapterContext {
}

pub fn raw_context(&self) -> *mut c_void {
self.inner.lock().context.context as *mut _
self.inner.lock().context.context.cast()
}

/// Obtain a lock to the WGL context and get handle to the [`glow::Context`] that can be used to
Expand Down Expand Up @@ -427,7 +427,7 @@ impl crate::Instance for Instance {

unsafe fn init(desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> {
profiling::scope!("Init OpenGL (WGL) Backend");
let opengl_module = unsafe { LoadLibraryA("opengl32.dll\0".as_ptr() as *const _) };
let opengl_module = unsafe { LoadLibraryA("opengl32.dll\0".as_ptr().cast()) };
if opengl_module.is_null() {
return Err(crate::InstanceError::with_source(
String::from("unable to load the OpenGL library"),
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
clippy::pattern_type_mismatch,
)]
#![warn(
clippy::ptr_as_ptr,
trivial_casts,
trivial_numeric_casts,
unsafe_op_in_unsafe_fn,
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
layout.raw,
conv::map_shader_stage(stages),
offset_bytes,
slice::from_raw_parts(data.as_ptr() as _, data.len() * 4),
slice::from_raw_parts(data.as_ptr().cast(), data.len() * 4),
)
};
}
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl gpu_alloc::MemoryDevice<vk::DeviceMemory> for super::DeviceShared {
self.raw
.map_memory(*memory, offset, size, vk::MemoryMapFlags::empty())
} {
Ok(ptr) => Ok(ptr::NonNull::new(ptr as *mut u8)
Ok(ptr) => Ok(ptr::NonNull::new(ptr.cast::<u8>())
.expect("Pointer to memory mapping must not be null")),
Err(vk::Result::ERROR_OUT_OF_DEVICE_MEMORY) => {
Err(gpu_alloc::DeviceMapError::OutOfDeviceMemory)
Expand Down Expand Up @@ -1513,7 +1513,7 @@ impl crate::Device for super::Device {
// SAFETY: similar to safety notes for `slice_get_ref`, but we have a
// mutable reference which is also guaranteed to be valid for writes.
unsafe {
&mut *(to_init as *mut [MaybeUninit<T>] as *mut [T])
&mut *(ptr::from_mut::<[MaybeUninit<T>]>(to_init) as *mut [T])
}
};
(Self { remainder }, init)
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/vulkan/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ unsafe extern "system" fn debug_utils_messenger_callback(
}

let cd = unsafe { &*callback_data_ptr };
let user_data = unsafe { &*(user_data as *mut super::DebugUtilsMessengerUserData) };
let user_data = unsafe { &*user_data.cast::<super::DebugUtilsMessengerUserData>() };

const VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912: i32 = 0x56146426;
if cd.message_id_number == VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912 {
Expand Down

0 comments on commit 28a6024

Please sign in to comment.