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