From ebac3201cdbc70d5f7b3cde845e998bebb3a9199 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 20 Sep 2023 15:37:50 +0200 Subject: [PATCH] bintols --- members/pt-bintols/src/datalayout.rs | 24 ++---------------------- members/pt-bintols/src/display.rs | 22 ++++++++++++++++++++++ members/pt-bintols/src/lib.rs | 3 ++- members/pt-core/Cargo.toml | 15 +++++++++++---- members/pt-log/Cargo.toml | 15 +++++++++++---- 5 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 members/pt-bintols/src/display.rs diff --git a/members/pt-bintols/src/datalayout.rs b/members/pt-bintols/src/datalayout.rs index 32693ae..2777aca 100644 --- a/members/pt-bintols/src/datalayout.rs +++ b/members/pt-bintols/src/datalayout.rs @@ -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) -} - diff --git a/members/pt-bintols/src/display.rs b/members/pt-bintols/src/display.rs new file mode 100644 index 0000000..106564f --- /dev/null +++ b/members/pt-bintols/src/display.rs @@ -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) +} diff --git a/members/pt-bintols/src/lib.rs b/members/pt-bintols/src/lib.rs index a058ad3..8ad5bda 100644 --- a/members/pt-bintols/src/lib.rs +++ b/members/pt-bintols/src/lib.rs @@ -1,2 +1,3 @@ -use pt_core; +// use pt_core; pub mod datalayout; +pub mod display; diff --git a/members/pt-core/Cargo.toml b/members/pt-core/Cargo.toml index bc9f747..4468176 100644 --- a/members/pt-core/Cargo.toml +++ b/members/pt-core/Cargo.toml @@ -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] diff --git a/members/pt-log/Cargo.toml b/members/pt-log/Cargo.toml index af166c0..37cbe63 100644 --- a/members/pt-log/Cargo.toml +++ b/members/pt-log/Cargo.toml @@ -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"