This commit is contained in:
Christoph J. Scherr 2023-09-20 15:37:50 +02:00
parent 3c8a2d9661
commit ebac3201cd
5 changed files with 48 additions and 31 deletions

View File

@ -1,7 +1,7 @@
//* # See what's behind the datatypes of Rust
//*
//* This Crate shows off how datatypes of rust are stored in memory.
use std::any::{TypeId, type_name};
use crate::display::{bytes_to_bin, byte_bit_display};
/// ## Investigate the internal representation of variables
///
@ -31,29 +31,9 @@ macro_rules! investigate_memory_layout {
byte_bit_display(std::mem::align_of_val(item)),
byte_bit_display(memory.len()),
memory,
bytes_to_bin(&memory),
bytes_to_bin(&memory)
);
}
}
};
}
fn bytes_to_bin(v: &[u8]) -> String {
if v.len() > 8 || v.len() < 1 {
return String::from("(impractical size for dump)")
}
let mut s = format!("0b{:08b}", v.first().unwrap());
for i in 1..v.len() {
s.push_str(&format!("_{:08b}", v[i]));
if i % 8 == 0 {
s.push_str("\n")
}
}
return s;
}
fn byte_bit_display(v: usize) -> String
{
format!("{:07} B = {:08} bit", v.clone(), v * 8)
}

View File

@ -0,0 +1,22 @@
//* # Tools that help display binary values, data sizes, etc
/// ## Get the binary representation for a Byte array [`&[u8]`]
///
/// ### Arguments
/// * `data` - The data you are trying to dump
pub fn bytes_to_bin(data: &[u8]) -> String {
let mut s = format!("0b{:08b}", data.first().unwrap());
for i in 1..data.len() {
s.push_str(&format!("_{:08b}", data[i]));
if i % 8 == 0 {
s.push_str("\n")
}
}
return s;
}
/// Quickly format a number of Bytes [`usize`] with the corresponding
/// number of bits
pub fn byte_bit_display(data: usize) -> String {
format!("{:07} B = {:08} bit", data.clone(), data * 8)
}

View File

@ -1,2 +1,3 @@
use pt_core;
// use pt_core;
pub mod datalayout;
pub mod display;

View File

@ -1,8 +1,15 @@
[package]
name = "pt-core"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
publish.workspace = true
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
description.workspace = true
readme.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
[dependencies]

View File

@ -1,9 +1,16 @@
[package]
name = "pt-log"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
publish.workspace = true
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
description.workspace = true
readme.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
[dependencies]
tracing = "0.1.37"