Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Binary and Display traits for FixedBitSet. #39

Merged
merged 4 commits into from Apr 15, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/lib.rs
Expand Up @@ -28,6 +28,10 @@ use core as std;

mod range;

#[cfg(feature = "std")]
use std::fmt::{Display, Error, Formatter, Binary};
use std::ops::{Add};

use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index};
use std::cmp::{Ord, Ordering};
use std::iter::{Chain, FromIterator};
Expand Down Expand Up @@ -356,6 +360,30 @@ impl FixedBitSet
}
}

#[cfg(feature = "std")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't depend on the std feature and will work fine with core, in fact. That's just a smaller thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should remove it though if it is true. On it.

impl Binary for FixedBitSet {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
let mut display_data = if f.alternate() {
String::from("0b")
} else {
String::new()
};

for block in self.data.iter().rev() {
display_data = display_data.add(&format!("{:b}", block));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. I'd suggest that we just write this piece by piece instead of creating an intermediate String.

};

write!(f, "{}", display_data)
}
}

#[cfg(feature = "std")]
impl Display for FixedBitSet {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
Binary::fmt(&self, f)
}
}

/// An iterator producing elements in the difference of two sets.
///
/// This struct is created by the [`FixedBitSet::difference`] method.
Expand Down Expand Up @@ -1373,3 +1401,26 @@ fn from_iterator_ones() {
assert_eq!(fb.len(), dup.len());
assert_eq!(fb.ones().collect::<Vec<usize>>(), dup.ones().collect::<Vec<usize>>());
}

#[cfg(feature = "std")]
#[test]
fn binary_trait() {
let items: Vec<usize> = vec![1, 5, 7, 10, 14, 15];
let fb = items.iter().cloned().collect::<FixedBitSet>();

assert_eq!(format!("{:b}", fb), "1100010010100010");
assert_eq!(format!("{:#b}", fb), "0b1100010010100010");
}

#[cfg(feature = "std")]
#[test]
fn display_trait() {
let len = 8;
let mut fb = FixedBitSet::with_capacity(len);

fb.put(4);
fb.put(2);

assert_eq!(format!("{}", fb), "10100");
assert_eq!(format!("{:#}", fb), "0b10100");
}