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