nak/hw_test: 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:27:47 +02:00 committed by Marge Bot
parent 718407e806
commit 190fbbe1d6

View File

@ -10,31 +10,10 @@ use crate::sm70::ShaderModel70;
use acorn::Acorn;
use compiler::cfg::CFGBuilder;
use nak_bindings::*;
use std::mem::offset_of;
use std::str::FromStr;
use std::sync::OnceLock;
// 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()
}};
}
struct RunSingleton {
sm: Box<dyn ShaderModel + Send + Sync>,
run: Runner,