From fcce2500f024a329e093066137054d503cbc3f26 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Tue, 19 Sep 2023 18:15:06 +0200 Subject: [PATCH] bool dump --- members/what-is/src/main.rs | 39 +++++++++++++------------------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/members/what-is/src/main.rs b/members/what-is/src/main.rs index a50ff5c..ea707c9 100644 --- a/members/what-is/src/main.rs +++ b/members/what-is/src/main.rs @@ -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::(items); } -fn dump_type() { - println!("{:?}", TypeId::of::()); - let mut items = Vec::new(); - items.push(true); - items.push(false); +fn dump_type(items: Vec) { + println!("Type:\t{}", type_name::()); + println!("\tID:\t{:?}", TypeId::of::()); + 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, ); } }