Created
January 11, 2023 12:19
-
-
Save td-bn/da726a144f41ec06745f671dbfb7ca60 to your computer and use it in GitHub Desktop.
Repr packed Rust example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[repr(packed)] | |
#[derive(Debug, Copy, Clone)] | |
struct Foo { | |
tiny: bool, | |
normal: u32, | |
small: u8, | |
long: u64, | |
short: u16, | |
} | |
macro_rules! raw_dbg {( | |
$expr:expr | |
) => (match $expr { expr => { | |
eprintln!("{:#?} = {:#x?}", expr, unsafe { | |
::core::slice::from_raw_parts( | |
&expr as *const _ as *const ::std::sync::atomic::AtomicU8, | |
::core::mem::size_of_val(&expr), | |
) | |
}); | |
expr | |
}})} | |
fn main () | |
{ | |
raw_dbg!( | |
Foo { | |
tiny: true, | |
normal: 2, | |
small: 1, | |
long: 3, | |
short: 4, | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment