rusticl: Use std::mem::offset_of!()
Support for nested fields got stabilized with Rust 1.82. Reviewed-by: Faith Ekstrand <faith.ekstrand@collabora.com> Reviewed-by: Karol Herbst <kherbst@redhat.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36526>
This commit is contained in:
@@ -412,7 +412,7 @@ macro_rules! impl_cl_type_trait_base {
|
||||
return Err($err);
|
||||
}
|
||||
|
||||
let offset = ::mesa_rust_util::offset_of!($t, $($field).+);
|
||||
let offset = ::std::mem::offset_of!($t, $($field).+);
|
||||
// SAFETY: We offset the pointer back from the ICD specified base type to our
|
||||
// internal type.
|
||||
let obj_ptr: *const $t = unsafe { self.byte_sub(offset) }.cast();
|
||||
@@ -427,7 +427,7 @@ macro_rules! impl_cl_type_trait_base {
|
||||
if ptr.is_null() {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
let offset = ::mesa_rust_util::offset_of!($t, $($field).+);
|
||||
let offset = ::std::mem::offset_of!($t, $($field).+);
|
||||
// SAFETY: The resulting pointer is safe as we simply offset into the ICD specified
|
||||
// base type.
|
||||
unsafe { ptr.byte_add(offset) as Self }
|
||||
|
||||
@@ -98,28 +98,6 @@ impl<T> CheckedPtr<T> for *mut T {
|
||||
}
|
||||
}
|
||||
|
||||
// While std::mem::offset_of!() is stable from 1.77.0, support for nested fields
|
||||
// (required in some rusticl cases) wasn't stabilized until 1.82.0.
|
||||
// from https://internals.rust-lang.org/t/discussion-on-offset-of/7440/2
|
||||
#[macro_export]
|
||||
macro_rules! offset_of {
|
||||
($Struct:path, $($field:ident).+ $(,)?) => {{
|
||||
// Using a separate function to minimize unhygienic hazards
|
||||
// (e.g. unsafety of #[repr(packed)] field borrows).
|
||||
// Uncomment `const` when `const fn`s can juggle pointers.
|
||||
/*const*/
|
||||
fn offset() -> usize {
|
||||
let u = std::mem::MaybeUninit::<$Struct>::uninit();
|
||||
let f = unsafe { &(*u.as_ptr()).$($field).+ };
|
||||
let o = (f as *const _ as usize).wrapping_sub(&u as *const _ as usize);
|
||||
// Triple check that we are within `u` still.
|
||||
assert!((0..=std::mem::size_of_val(&u)).contains(&o));
|
||||
o
|
||||
}
|
||||
offset()
|
||||
}};
|
||||
}
|
||||
|
||||
// Adapted from libstd since std::ptr::is_aligned_to is still unstable
|
||||
// See https://github.com/rust-lang/rust/issues/96284
|
||||
#[must_use]
|
||||
|
||||
Reference in New Issue
Block a user