bool dump

This commit is contained in:
Christoph J. Scherr 2023-09-19 18:15:06 +02:00
parent 8ec7bd2b38
commit fcce2500f0
1 changed files with 14 additions and 25 deletions

View File

@ -1,41 +1,30 @@
use std::any::{TypeId, type_name};
use std::mem::*;
use std::fmt::Debug;
//* # See what's behind the primitive datatypes of Rust
//*
//* This Crate shows off, how primitive Types of rust are stored in memory.
fn main() {
println!("bool");
let mut items = Vec::new();
items.push(true);
items.push(false);
unsafe {
for (index, item) in items.iter().enumerate() {
let mem_item: [u8; 1] = std::mem::transmute(*item);
let ref_item: [u8; 8] = std::mem::transmute(item);
println!("\t{index:02x}\titem:\t{item}\n\
\t\tmemory: {:X?}\n\n\
\t\tlocal ref: {:X?}",
mem_item,
ref_item,
);
}
}
dump_type::<bool>(items);
}
fn dump_type<T: 'static>() {
println!("{:?}", TypeId::of::<T>());
let mut items = Vec::new();
items.push(true);
items.push(false);
fn dump_type<T: Debug + 'static>(items: Vec<T>) {
println!("Type:\t{}", type_name::<T>());
println!("\tID:\t{:?}", TypeId::of::<T>());
println!("\tItems:");
unsafe {
for (index, item) in items.iter().enumerate() {
let mem_item: [u8; 1] = std::mem::transmute(*item);
let ref_item: [u8; 8] = std::mem::transmute(item);
println!("\t{index:02x}\titem:\t{item}\n\
\t\tmemory: {:X?}\n\n\
\t\tlocal ref: {:X?}",
mem_item,
ref_item,
let pointer = item as *const T;
let raw_pointer = pointer as *const u8;
println!("\t{index:02x}\titem:\t{item:?}\n\
\t\tpointer: {:X?}\n\
\t\tmemory: {:X?}",
pointer,
*raw_pointer,
);
}
}