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.
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 15:37:50 +02:00
|
|
|
use crate::display::{bytes_to_bin, byte_bit_display};
|
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) => {
|
|
|
|
println!("Type:\t{}", type_name::<$t>());
|
|
|
|
println!("\talign:\t{:?} B", std::mem::align_of::<$t>());
|
|
|
|
println!("\tID:\t{:?}\n", TypeId::of::<$t>());
|
|
|
|
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();
|
|
|
|
println!("\
|
|
|
|
\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,
|
|
|
|
byte_bit_display(std::mem::align_of_val(item)),
|
|
|
|
byte_bit_display(memory.len()),
|
|
|
|
memory,
|
2023-09-20 15:37:50 +02:00
|
|
|
bytes_to_bin(&memory)
|
2023-09-20 14:28:40 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|