Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
54 changes: 53 additions & 1 deletion src/bitmap/fmt.rs
Original file line number Diff line number Diff line change
@@ -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<const BYTES: usize> core::fmt::Display for Bitmap<BYTES> {
/// 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<const BYTES: usize> core::fmt::Debug for Bitmap<BYTES> {
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<const BYTES: usize> core::fmt::Display for Bitmap<BYTES> {
/// Formats a bitmap. Only shows the last 2 bytes if the bitmap is longer.
/// The bytes will be separated by space `' '`.
Expand Down Expand Up @@ -41,6 +91,7 @@ impl<const BYTES: usize> core::fmt::Display for Bitmap<BYTES> {
}
}

#[cfg(feature = "alloc")]
impl<const BYTES: usize> core::fmt::Debug for Bitmap<BYTES> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut contents = String::new();
Expand All @@ -63,6 +114,7 @@ impl<const BYTES: usize> core::fmt::Debug for Bitmap<BYTES> {
}
}

#[cfg(feature = "alloc")]
impl<const BYTES: usize> Bitmap<BYTES> {
/// Format a range of bits into a [`Option<String>`].
///
Expand Down Expand Up @@ -117,4 +169,4 @@ impl<const BYTES: usize> Bitmap<BYTES> {

Some(contents)
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
//! not `BitRef` due to a similar issue.
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

mod tools;
Expand Down