Skip to content

Instantly share code, notes, and snippets.

@td-bn
Created January 11, 2023 12:19
Show Gist options
  • Save td-bn/da726a144f41ec06745f671dbfb7ca60 to your computer and use it in GitHub Desktop.
Save td-bn/da726a144f41ec06745f671dbfb7ca60 to your computer and use it in GitHub Desktop.
Repr packed Rust example
#[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