This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
pt/members/libpt-bintols/src/datalayout.rs

43 lines
1.5 KiB
Rust
Raw Normal View History

2023-09-20 14:28:40 +02:00
//* # See what's behind the datatypes of Rust
//*
//* This Crate shows off how datatypes of rust are stored in memory.
2023-09-20 18:16:21 +02:00
// reexport macros
pub use crate::investigate_memory_layout;
2023-09-20 14:28:40 +02:00
/// ## Investigate the internal representation of variables
///
/// Takes 1. the Type and 2. a [`Vec`] of items (of that datatype).
#[macro_export]
macro_rules! investigate_memory_layout {
($t:ty, $v:tt) => {
2023-09-20 18:16:21 +02:00
println!("Type:\t{}", std::any::type_name::<$t>());
2023-09-20 14:28:40 +02:00
println!("\talign:\t{:?} B", std::mem::align_of::<$t>());
2023-09-20 18:16:21 +02:00
println!("\tID:\t{:?}\n", std::any::TypeId::of::<$t>());
2023-09-20 14:28:40 +02:00
println!("\tItems:");
unsafe {
for (index, item) in $v.iter().enumerate() {
let pointer = item as *const $t;
let mut memory: [u8; std::mem::size_of::<$t>()] = std::mem::transmute(item.clone());
memory.reverse();
2024-01-10 21:52:13 +01:00
println!(
"\
2023-09-20 14:28:40 +02:00
\t{index:02x}\titem:\t\t{item:?}\n\
\t\tpointer: \t{:X?}\n\
\t\talign: \t{}\n\
\t\tsize: \t{}\n\
\t\tmemory: \t{:X?}\n\
\t\tbin mem: \t{}\n\
\t\tnote: \tmemory order reversed\n\
",
pointer,
2023-09-20 18:16:21 +02:00
display::byte_bit_display(std::mem::align_of_val(item)),
display::byte_bit_display(memory.len()),
2023-09-20 14:28:40 +02:00
memory,
2023-09-20 18:16:21 +02:00
display::bytes_to_bin(&memory)
2023-09-20 14:28:40 +02:00
);
}
}
};
}