Replace as *... raw pointer-type changes with more explicit .cast() (#685)

`.cast()` allows changing the pointer type without hiding (accidental)
mutability changes (noting that `*mut` still coerces to `*.const`).

For mutability changes Rust 1.65 included `cast_mut()` and
`cast_const()`, but those would bump our MSRV too eagerly for now.
This commit is contained in:
Marijn Suijten 2022-11-22 19:52:14 +01:00
parent 24000c1249
commit 047676a443
8 changed files with 26 additions and 24 deletions

View file

@ -51,7 +51,7 @@ pub unsafe fn create_surface(
(RawDisplayHandle::Xlib(display), RawWindowHandle::Xlib(window)) => { (RawDisplayHandle::Xlib(display), RawWindowHandle::Xlib(window)) => {
let surface_desc = vk::XlibSurfaceCreateInfoKHR::builder() let surface_desc = vk::XlibSurfaceCreateInfoKHR::builder()
.dpy(display.display as *mut _) .dpy(display.display.cast())
.window(window.window); .window(window.window);
let surface_fn = khr::XlibSurface::new(entry, instance); let surface_fn = khr::XlibSurface::new(entry, instance);
surface_fn.create_xlib_surface(&surface_desc, allocation_callbacks) surface_fn.create_xlib_surface(&surface_desc, allocation_callbacks)
@ -77,7 +77,7 @@ pub unsafe fn create_surface(
use raw_window_metal::{appkit, Layer}; use raw_window_metal::{appkit, Layer};
let layer = match appkit::metal_layer_from_handle(window) { let layer = match appkit::metal_layer_from_handle(window) {
Layer::Existing(layer) | Layer::Allocated(layer) => layer as *mut _, Layer::Existing(layer) | Layer::Allocated(layer) => layer.cast(),
Layer::None => return Err(vk::Result::ERROR_INITIALIZATION_FAILED), Layer::None => return Err(vk::Result::ERROR_INITIALIZATION_FAILED),
}; };
@ -91,7 +91,7 @@ pub unsafe fn create_surface(
use raw_window_metal::{uikit, Layer}; use raw_window_metal::{uikit, Layer};
let layer = match uikit::metal_layer_from_handle(window) { let layer = match uikit::metal_layer_from_handle(window) {
Layer::Existing(layer) | Layer::Allocated(layer) => layer as *mut _, Layer::Existing(layer) | Layer::Allocated(layer) => layer.cast(),
Layer::None => return Err(vk::Result::ERROR_INITIALIZATION_FAILED), Layer::None => return Err(vk::Result::ERROR_INITIALIZATION_FAILED),
}; };

View file

@ -2032,7 +2032,7 @@ impl Device {
first_query, first_query,
query_count, query_count,
data_size, data_size,
data.as_mut_ptr() as *mut _, data.as_mut_ptr().cast(),
mem::size_of::<T>() as _, mem::size_of::<T>() as _,
flags, flags,
) )

View file

@ -200,7 +200,7 @@ impl AccelerationStructure {
acceleration_structures.as_ptr(), acceleration_structures.as_ptr(),
query_type, query_type,
data.len(), data.len(),
data.as_mut_ptr() as *mut std::ffi::c_void, data.as_mut_ptr().cast(),
stride, stride,
) )
.result() .result()

View file

@ -206,7 +206,7 @@ impl RayTracing {
first_group, first_group,
group_count, group_count,
data.len(), data.len(),
data.as_mut_ptr() as *mut std::ffi::c_void, data.as_mut_ptr().cast(),
) )
.result() .result()
} }
@ -223,7 +223,7 @@ impl RayTracing {
self.handle, self.handle,
accel_struct, accel_struct,
std::mem::size_of::<u64>(), std::mem::size_of::<u64>(),
handle_ptr as *mut std::ffi::c_void, handle_ptr.cast(),
) )
.result_with_success(handle) .result_with_success(handle)
} }

View file

@ -31,7 +31,7 @@ impl<T: Copy> Align<T> {
use std::slice::from_raw_parts_mut; use std::slice::from_raw_parts_mut;
if self.elem_size == size_of::<T>() as u64 { if self.elem_size == size_of::<T>() as u64 {
unsafe { unsafe {
let mapped_slice = from_raw_parts_mut(self.ptr as *mut T, slice.len()); let mapped_slice = from_raw_parts_mut(self.ptr.cast(), slice.len());
mapped_slice.copy_from_slice(slice); mapped_slice.copy_from_slice(slice);
} }
} else { } else {
@ -75,7 +75,9 @@ impl<'a, T: Copy + 'a> Iterator for AlignIter<'a, T> {
} }
unsafe { unsafe {
// Need to cast to *mut u8 because () has size 0 // Need to cast to *mut u8 because () has size 0
let ptr = (self.align.ptr as *mut u8).offset(self.current as isize) as *mut T; let ptr = (self.align.ptr.cast::<u8>())
.offset(self.current as isize)
.cast();
self.current += self.align.elem_size; self.current += self.align.elem_size;
Some(&mut *ptr) Some(&mut *ptr)
} }
@ -118,7 +120,9 @@ pub fn read_spv<R: io::Read + io::Seek>(x: &mut R) -> io::Result<Vec<u32>> {
// reading uninitialized memory. // reading uninitialized memory.
let mut result = vec![0u32; words]; let mut result = vec![0u32; words];
x.seek(io::SeekFrom::Start(0))?; x.seek(io::SeekFrom::Start(0))?;
x.read_exact(unsafe { slice::from_raw_parts_mut(result.as_mut_ptr() as *mut u8, words * 4) })?; x.read_exact(unsafe {
slice::from_raw_parts_mut(result.as_mut_ptr().cast::<u8>(), words * 4)
})?;
const MAGIC_NUMBER: u32 = 0x0723_0203; const MAGIC_NUMBER: u32 = 0x0723_0203;
if !result.is_empty() && result[0] == MAGIC_NUMBER.swap_bytes() { if !result.is_empty() && result[0] == MAGIC_NUMBER.swap_bytes() {
for word in &mut result { for word in &mut result {

View file

@ -5208,7 +5208,7 @@ impl<'a> SpecializationInfoBuilder<'a> {
#[inline] #[inline]
pub fn data(mut self, data: &'a [u8]) -> Self { pub fn data(mut self, data: &'a [u8]) -> Self {
self.inner.data_size = data.len(); self.inner.data_size = data.len();
self.inner.p_data = data.as_ptr() as *const c_void; self.inner.p_data = data.as_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
@ -6906,7 +6906,7 @@ impl<'a> PipelineCacheCreateInfoBuilder<'a> {
#[inline] #[inline]
pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self { pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self {
self.inner.initial_data_size = initial_data.len(); self.inner.initial_data_size = initial_data.len();
self.inner.p_initial_data = initial_data.as_ptr() as *const c_void; self.inner.p_initial_data = initial_data.as_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
@ -12726,7 +12726,7 @@ impl<'a> DebugMarkerObjectTagInfoEXTBuilder<'a> {
#[inline] #[inline]
pub fn tag(mut self, tag: &'a [u8]) -> Self { pub fn tag(mut self, tag: &'a [u8]) -> Self {
self.inner.tag_size = tag.len(); self.inner.tag_size = tag.len();
self.inner.p_tag = tag.as_ptr() as *const c_void; self.inner.p_tag = tag.as_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
@ -26002,7 +26002,7 @@ impl<'a> WriteDescriptorSetInlineUniformBlockBuilder<'a> {
#[inline] #[inline]
pub fn data(mut self, data: &'a [u8]) -> Self { pub fn data(mut self, data: &'a [u8]) -> Self {
self.inner.data_size = data.len() as _; self.inner.data_size = data.len() as _;
self.inner.p_data = data.as_ptr() as *const c_void; self.inner.p_data = data.as_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
@ -26305,7 +26305,7 @@ impl<'a> ValidationCacheCreateInfoEXTBuilder<'a> {
#[inline] #[inline]
pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self { pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self {
self.inner.initial_data_size = initial_data.len(); self.inner.initial_data_size = initial_data.len();
self.inner.p_initial_data = initial_data.as_ptr() as *const c_void; self.inner.p_initial_data = initial_data.as_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
@ -27834,7 +27834,7 @@ impl<'a> DebugUtilsObjectTagInfoEXTBuilder<'a> {
#[inline] #[inline]
pub fn tag(mut self, tag: &'a [u8]) -> Self { pub fn tag(mut self, tag: &'a [u8]) -> Self {
self.inner.tag_size = tag.len(); self.inner.tag_size = tag.len();
self.inner.p_tag = tag.as_ptr() as *const c_void; self.inner.p_tag = tag.as_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
@ -43328,7 +43328,7 @@ impl<'a> PipelineExecutableInternalRepresentationKHRBuilder<'a> {
#[inline] #[inline]
pub fn data(mut self, data: &'a mut [u8]) -> Self { pub fn data(mut self, data: &'a mut [u8]) -> Self {
self.inner.data_size = data.len(); self.inner.data_size = data.len();
self.inner.p_data = data.as_mut_ptr() as *mut c_void; self.inner.p_data = data.as_mut_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
@ -47840,7 +47840,7 @@ impl<'a> AccelerationStructureBuildGeometryInfoKHRBuilder<'a> {
geometries_ptrs: &'a [&'a AccelerationStructureGeometryKHR], geometries_ptrs: &'a [&'a AccelerationStructureGeometryKHR],
) -> Self { ) -> Self {
self.inner.geometry_count = geometries_ptrs.len() as _; self.inner.geometry_count = geometries_ptrs.len() as _;
self.inner.pp_geometries = geometries_ptrs.as_ptr() as *const *const _; self.inner.pp_geometries = geometries_ptrs.as_ptr().cast();
self self
} }
#[inline] #[inline]
@ -59999,7 +59999,7 @@ impl<'a> CuModuleCreateInfoNVXBuilder<'a> {
#[inline] #[inline]
pub fn data(mut self, data: &'a [u8]) -> Self { pub fn data(mut self, data: &'a [u8]) -> Self {
self.inner.data_size = data.len(); self.inner.data_size = data.len();
self.inner.p_data = data.as_ptr() as *const c_void; self.inner.p_data = data.as_ptr().cast();
self self
} }
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"] #[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]

View file

@ -34,7 +34,7 @@ macro_rules! offset_of {
#[allow(unused_unsafe)] #[allow(unused_unsafe)]
unsafe { unsafe {
let b: $base = mem::zeroed(); let b: $base = mem::zeroed();
(&b.$field as *const _ as isize) - (&b as *const _ as isize) std::ptr::addr_of!(b.$field) as isize - std::ptr::addr_of!(b) as isize
} }
}}; }};
} }

View file

@ -1712,10 +1712,8 @@ pub fn derive_setters(
// Interpret void array as byte array // Interpret void array as byte array
if field.basetype == "void" && matches!(field.reference, Some(vkxml::ReferenceType::Pointer)) { if field.basetype == "void" && matches!(field.reference, Some(vkxml::ReferenceType::Pointer)) {
let mutable = if field.is_const { quote!(const) } else { quote!(mut) };
slice_param_ty_tokens = quote!([u8]); slice_param_ty_tokens = quote!([u8]);
ptr = quote!(#ptr as *#mutable c_void); ptr = quote!(#ptr.cast());
}; };
let set_size_stmt = if field.is_pointer_to_static_sized_array() { let set_size_stmt = if field.is_pointer_to_static_sized_array() {
@ -1733,7 +1731,7 @@ pub fn derive_setters(
let array_size = if let Some(array_size) = array_size.strip_suffix(",1") { let array_size = if let Some(array_size) = array_size.strip_suffix(",1") {
param_ident_short = format_ident!("{}_ptrs", param_ident_short); param_ident_short = format_ident!("{}_ptrs", param_ident_short);
slice_param_ty_tokens = field.safe_type_tokens(quote!('a), Some(1)); slice_param_ty_tokens = field.safe_type_tokens(quote!('a), Some(1));
ptr = quote!(#ptr as *const *const _); ptr = quote!(#ptr.cast());
array_size array_size
} else { } else {
array_size array_size