bool dump
This commit is contained in:
parent
8ec7bd2b38
commit
fcce2500f0
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue