From ca68649cbc91bc5d701e7b983ee9a080f9d495d1 Mon Sep 17 00:00:00 2001 From: rsahwe Date: Thu, 17 Apr 2025 20:52:59 +0200 Subject: [PATCH] Remove unnecessary alloc usage --- Cargo.toml | 4 ++++ README.md | 2 ++ src/bitmap/fmt.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 1 + 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2a8110b..6e7839c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,10 @@ bench = false # ignoring libtest check, so criterion cmd options can be used. [dev-dependencies] criterion = { version = "0.4", features = ["html_reports"] } +[features] +default = ["alloc"] +alloc = [] + [[example]] name = "bitmap-base" [[example]] diff --git a/README.md b/README.md index 6d58224..1e52eee 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,8 @@ the map is relatively small like 8 or 16 bits, you can put it on stack safely. If it is larger like 256 or 1024 bits, you may want to put it on heap. + + The alloc feature which is enabled by default is only used for formatting. ## Examples diff --git a/src/bitmap/fmt.rs b/src/bitmap/fmt.rs index 8bbf547..99731ad 100644 --- a/src/bitmap/fmt.rs +++ b/src/bitmap/fmt.rs @@ -1,9 +1,59 @@ //! Implementations of formating methods for `Bitmap`, including [`Debug`]. +#[cfg(feature = "alloc")] use alloc::{string::{String, ToString}, format}; use crate::bitmap::*; +#[cfg(not(feature = "alloc"))] +impl core::fmt::Display for Bitmap { + /// Formats a bitmap. Only shows the last 2 bytes if the bitmap is longer. + /// The bytes will be separated by space `' '`. + /// + /// The bits will be arranged from right to left. If the bitmap is longer than + /// 2 bytes, a `"..."` will show on the left. + /// On the very left, a bracket tells the bit length of the map (in a form + /// like `"[N bits]"`). A space `' '` will be between the bit contents and this + /// bracket. + /// + /// # Examples + /// ``` + /// use cbitmap::bitmap::*; + /// + /// let mut map: Bitmap<3> = 0.into(); + /// map.set(0); + /// map.set(8); + /// let str = &format!("{map}"); + /// assert_eq!(str, "[24 bits] ...00000001 00000001"); + /// ``` + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "[{} bits] ", BYTES * 8)?; + let size = 2.min(BYTES); + if BYTES > size { + write!(f, "...")?; + } + for i in 0..size { + if i > 0 { + write!(f, " ")?; + } + write!(f, "{:08b}", self.__copy_u8(size - i - 1))?; + } + Ok(()) + } +} + +#[cfg(not(feature = "alloc"))] +impl core::fmt::Debug for Bitmap { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("Bitmap") + .field("#bytes", &BYTES) + .field("#bits", &(BYTES * 8)) + .field("bits", &self.bits) + .finish() + } +} + +#[cfg(feature = "alloc")] impl core::fmt::Display for Bitmap { /// Formats a bitmap. Only shows the last 2 bytes if the bitmap is longer. /// The bytes will be separated by space `' '`. @@ -41,6 +91,7 @@ impl core::fmt::Display for Bitmap { } } +#[cfg(feature = "alloc")] impl core::fmt::Debug for Bitmap { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut contents = String::new(); @@ -63,6 +114,7 @@ impl core::fmt::Debug for Bitmap { } } +#[cfg(feature = "alloc")] impl Bitmap { /// Format a range of bits into a [`Option`]. /// @@ -117,4 +169,4 @@ impl Bitmap { Some(contents) } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index db84b8a..492a6a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,6 +166,7 @@ //! not `BitRef` due to a similar issue. #![no_std] +#[cfg(feature = "alloc")] extern crate alloc; mod tools;