Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: warn on and satisfy clippy::{ptr_as_ptr,ref_as_ptr} in wgpu-{core,hal,types} #6035

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<A: HalApi> Drop for Queue<A> {
fn drop(&mut self) {
resource_log!("Drop {}", self.error_ident());
// SAFETY: we never access `self.raw` beyond this point.
let queue = unsafe { std::mem::ManuallyDrop::take(&mut self.raw) };
let queue = unsafe { ManuallyDrop::take(&mut self.raw) };
self.device.release_queue(queue);
}
}
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
8 changes: 4 additions & 4 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 Expand Up @@ -1816,7 +1816,7 @@ impl crate::Device for super::Device {
.allocations
.iter_mut()
.map(|alloc| wgt::AllocationReport {
name: std::mem::take(&mut alloc.name),
name: mem::take(&mut alloc.name),
offset: alloc.offset,
size: alloc.size,
})
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
15 changes: 9 additions & 6 deletions wgpu-hal/src/gles/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,10 @@ impl crate::Instance for Instance {

let ret = unsafe {
ndk_sys::ANativeWindow_setBuffersGeometry(
handle.a_native_window.as_ptr() as *mut ndk_sys::ANativeWindow,
handle
.a_native_window
.as_ptr()
.cast::<ndk_sys::ANativeWindow>(),
0,
0,
format,
Expand Down Expand Up @@ -1229,12 +1232,12 @@ impl crate::Surface for Surface {
let native_window_ptr = match (self.wsi.kind, self.raw_window_handle) {
(WindowKind::Unknown | WindowKind::X11, Rwh::Xlib(handle)) => {
temp_xlib_handle = handle.window;
&mut temp_xlib_handle as *mut _ as *mut ffi::c_void
ptr::from_mut(&mut temp_xlib_handle).cast::<ffi::c_void>()
}
(WindowKind::AngleX11, Rwh::Xlib(handle)) => handle.window as *mut ffi::c_void,
(WindowKind::Unknown | WindowKind::X11, Rwh::Xcb(handle)) => {
temp_xcb_handle = handle.window;
&mut temp_xcb_handle as *mut _ as *mut ffi::c_void
ptr::from_mut(&mut temp_xcb_handle).cast::<ffi::c_void>()
}
(WindowKind::AngleX11, Rwh::Xcb(handle)) => {
handle.window.get() as *mut ffi::c_void
Expand All @@ -1248,7 +1251,7 @@ impl crate::Surface for Surface {
unsafe { library.get(b"wl_egl_window_create") }.unwrap();
let window =
unsafe { wl_egl_window_create(handle.surface.as_ptr(), 640, 480) }
as *mut _;
.cast();
wl_window = Some(window);
window
}
Expand All @@ -1265,8 +1268,8 @@ impl crate::Surface for Surface {
use objc::{msg_send, runtime::Object, sel, sel_impl};
// ns_view always have a layer and don't need to verify that it exists.
let layer: *mut Object =
msg_send![handle.ns_view.as_ptr() as *mut Object, layer];
layer as *mut ffi::c_void
msg_send![handle.ns_view.as_ptr().cast::<Object>(), layer];
layer.cast::<ffi::c_void>()
};
window_ptr
}
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/gles/emscripten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub unsafe fn enable_extension(extension_name_null_terminated: &str) -> bool {
unsafe {
emscripten_webgl_enable_extension(
emscripten_webgl_get_current_context(),
extension_name_null_terminated.as_ptr() as _,
extension_name_null_terminated.as_ptr().cast(),
) == 1
}
}
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
10 changes: 5 additions & 5 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 @@ -184,7 +184,7 @@ fn load_gl_func(name: &str, module: Option<HMODULE>) -> *const c_void {

fn get_extensions(extra: &Wgl, dc: HDC) -> HashSet<String> {
if extra.GetExtensionsStringARB.is_loaded() {
unsafe { CStr::from_ptr(extra.GetExtensionsStringARB(dc as *const _)) }
unsafe { CStr::from_ptr(extra.GetExtensionsStringARB(dc.cast())) }
.to_str()
.unwrap_or("")
} else {
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 Expand Up @@ -472,7 +472,7 @@ impl crate::Instance for Instance {
0, // End of list
];
let context = unsafe {
extra.CreateContextAttribsARB(dc as *const _, ptr::null(), attributes.as_ptr())
extra.CreateContextAttribsARB(dc.cast(), ptr::null(), attributes.as_ptr())
};
if context.is_null() {
return Err(crate::InstanceError::with_source(
Expand All @@ -481,7 +481,7 @@ impl crate::Instance for Instance {
));
}
WglContext {
context: context as *mut _,
context: context.cast_mut().cast(),
}
} else {
context
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
Loading
Loading