mirror of
https://git.proxmox.com/git/proxmox
synced 2025-07-25 13:57:44 +00:00
lang: get offsetof const fn ready
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
af33a97547
commit
8f89f9ad60
@ -72,22 +72,21 @@ macro_rules! static_assert_size {
|
|||||||
/// #[repr(C)]
|
/// #[repr(C)]
|
||||||
/// struct Stuff {
|
/// struct Stuff {
|
||||||
/// first: u32,
|
/// first: u32,
|
||||||
/// second: u32,
|
/// second: u16,
|
||||||
|
/// third: u16,
|
||||||
|
/// fourth: u32,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
/// assert_eq!(offsetof!(Stuff, first), 0);
|
||||||
/// assert_eq!(offsetof!(Stuff, second), 4);
|
/// assert_eq!(offsetof!(Stuff, second), 4);
|
||||||
|
/// assert_eq!(offsetof!(Stuff, third), 6);
|
||||||
|
/// assert_eq!(offsetof!(Stuff, fourth), 8);
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
// FIXME: With 1.56 we get `const transmute` which may help making this usable `const fn` as we can
|
|
||||||
// avoid dereferencing the raw pointer by transmuting `0usize` into a proper reference instead.
|
|
||||||
//
|
|
||||||
// So with 1.56, do this instead:
|
|
||||||
//
|
|
||||||
// unsafe { &(std::mem::transmute::<_, &$ty>(0usize).$field) as *const _ as usize }
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! offsetof {
|
macro_rules! offsetof {
|
||||||
($ty:ty, $field:ident) => {
|
($ty:ty, $field:ident) => {
|
||||||
unsafe { &(*(std::ptr::null::<$ty>())).$field as *const _ as usize }
|
unsafe { &(std::mem::transmute::<_, &$ty>(0usize).$field) as *const _ as usize }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user