Skip to content

Commit

Permalink
Replace as *... raw pointer-type changes with more explicit `.cast(…
Browse files Browse the repository at this point in the history
…)` (#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.
  • Loading branch information
MarijnS95 committed Nov 22, 2022
1 parent 24000c1 commit 047676a
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions ash-window/src/lib.rs
Expand Up @@ -51,7 +51,7 @@ pub unsafe fn create_surface(

(RawDisplayHandle::Xlib(display), RawWindowHandle::Xlib(window)) => {
let surface_desc = vk::XlibSurfaceCreateInfoKHR::builder()
.dpy(display.display as *mut _)
.dpy(display.display.cast())
.window(window.window);
let surface_fn = khr::XlibSurface::new(entry, instance);
surface_fn.create_xlib_surface(&surface_desc, allocation_callbacks)
Expand All @@ -77,7 +77,7 @@ pub unsafe fn create_surface(
use raw_window_metal::{appkit, Layer};

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),
};

Expand All @@ -91,7 +91,7 @@ pub unsafe fn create_surface(
use raw_window_metal::{uikit, Layer};

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),
};

Expand Down
2 changes: 1 addition & 1 deletion ash/src/device.rs
Expand Up @@ -2032,7 +2032,7 @@ impl Device {
first_query,
query_count,
data_size,
data.as_mut_ptr() as *mut _,
data.as_mut_ptr().cast(),
mem::size_of::<T>() as _,
flags,
)
Expand Down
2 changes: 1 addition & 1 deletion ash/src/extensions/khr/acceleration_structure.rs
Expand Up @@ -200,7 +200,7 @@ impl AccelerationStructure {
acceleration_structures.as_ptr(),
query_type,
data.len(),
data.as_mut_ptr() as *mut std::ffi::c_void,
data.as_mut_ptr().cast(),
stride,
)
.result()
Expand Down
4 changes: 2 additions & 2 deletions ash/src/extensions/nv/ray_tracing.rs
Expand Up @@ -206,7 +206,7 @@ impl RayTracing {
first_group,
group_count,
data.len(),
data.as_mut_ptr() as *mut std::ffi::c_void,
data.as_mut_ptr().cast(),
)
.result()
}
Expand All @@ -223,7 +223,7 @@ impl RayTracing {
self.handle,
accel_struct,
std::mem::size_of::<u64>(),
handle_ptr as *mut std::ffi::c_void,
handle_ptr.cast(),
)
.result_with_success(handle)
}
Expand Down
10 changes: 7 additions & 3 deletions ash/src/util.rs
Expand Up @@ -31,7 +31,7 @@ impl<T: Copy> Align<T> {
use std::slice::from_raw_parts_mut;
if self.elem_size == size_of::<T>() as u64 {
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);
}
} else {
Expand Down Expand Up @@ -75,7 +75,9 @@ impl<'a, T: Copy + 'a> Iterator for AlignIter<'a, T> {
}
unsafe {
// 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;
Some(&mut *ptr)
}
Expand Down Expand Up @@ -118,7 +120,9 @@ pub fn read_spv<R: io::Read + io::Seek>(x: &mut R) -> io::Result<Vec<u32>> {
// reading uninitialized memory.
let mut result = vec![0u32; words];
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;
if !result.is_empty() && result[0] == MAGIC_NUMBER.swap_bytes() {
for word in &mut result {
Expand Down
18 changes: 9 additions & 9 deletions ash/src/vk/definitions.rs
Expand Up @@ -5208,7 +5208,7 @@ impl<'a> SpecializationInfoBuilder<'a> {
#[inline]
pub fn data(mut self, data: &'a [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -6906,7 +6906,7 @@ impl<'a> PipelineCacheCreateInfoBuilder<'a> {
#[inline]
pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -12726,7 +12726,7 @@ impl<'a> DebugMarkerObjectTagInfoEXTBuilder<'a> {
#[inline]
pub fn tag(mut self, tag: &'a [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -26002,7 +26002,7 @@ impl<'a> WriteDescriptorSetInlineUniformBlockBuilder<'a> {
#[inline]
pub fn data(mut self, data: &'a [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -26305,7 +26305,7 @@ impl<'a> ValidationCacheCreateInfoEXTBuilder<'a> {
#[inline]
pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -27834,7 +27834,7 @@ impl<'a> DebugUtilsObjectTagInfoEXTBuilder<'a> {
#[inline]
pub fn tag(mut self, tag: &'a [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -43328,7 +43328,7 @@ impl<'a> PipelineExecutableInternalRepresentationKHRBuilder<'a> {
#[inline]
pub fn data(mut self, data: &'a mut [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -47840,7 +47840,7 @@ impl<'a> AccelerationStructureBuildGeometryInfoKHRBuilder<'a> {
geometries_ptrs: &'a [&'a AccelerationStructureGeometryKHR],
) -> Self {
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
}
#[inline]
Expand Down Expand Up @@ -59999,7 +59999,7 @@ impl<'a> CuModuleCreateInfoNVXBuilder<'a> {
#[inline]
pub fn data(mut self, data: &'a [u8]) -> Self {
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
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down
2 changes: 1 addition & 1 deletion examples/src/lib.rs
Expand Up @@ -34,7 +34,7 @@ macro_rules! offset_of {
#[allow(unused_unsafe)]
unsafe {
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
}
}};
}
Expand Down
6 changes: 2 additions & 4 deletions generator/src/lib.rs
Expand Up @@ -1712,10 +1712,8 @@ pub fn derive_setters(

// Interpret void array as byte array
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]);
ptr = quote!(#ptr as *#mutable c_void);
ptr = quote!(#ptr.cast());
};

let set_size_stmt = if field.is_pointer_to_static_sized_array() {
Expand All @@ -1733,7 +1731,7 @@ pub fn derive_setters(
let array_size = if let Some(array_size) = array_size.strip_suffix(",1") {
param_ident_short = format_ident!("{}_ptrs", param_ident_short);
slice_param_ty_tokens = field.safe_type_tokens(quote!('a), Some(1));
ptr = quote!(#ptr as *const *const _);
ptr = quote!(#ptr.cast());
array_size
} else {
array_size
Expand Down

0 comments on commit 047676a

Please sign in to comment.