compiler/rust: Use std::mem::offset_of!()

It got stabilized with Rust 1.77.

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/31510>
This commit is contained in:
LingMan 2024-10-03 16:32:19 +02:00 committed by Marge Bot
parent 190fbbe1d6
commit 0ff3e2a0f9

View File

@ -5,31 +5,10 @@ use crate::bindings::*;
use std::ffi::{c_void, CStr};
use std::marker::PhantomData;
use std::mem::offset_of;
use std::ptr::NonNull;
use std::str;
// from https://internals.rust-lang.org/t/discussion-on-offset-of/7440/2
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();
// Use pattern-matching to avoid accidentally going through Deref.
let &$Struct { $field: ref f, .. } = unsafe { &*u.as_ptr() };
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()
}};
}
pub struct ExecListIter<'a, T> {
n: &'a exec_node,
offset: usize,