#[repr(transparent)]
pub struct BitSet<T, const N: usize> { /* private fields */ }
Expand description

The bit set itself.

This wrapper is #![repr(transparent)] and guaranteed to have the same memory representation as the inner bit array

Panics

All non-try functions taking a bit parameter panics if the bit is bigger than the capacity of the set. For non-panicking versions, use try_.

Implementations

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance of BitSet.

Examples
use rbitset::BitSet;

let set = BitSet::<u8, 1>::new();

Create an empty instance with default value.

This function is the same as new but without the constness.

Examples
use rbitset::BitSet;

let set = BitSet::<u32, 7>::new();

Clears the set, disabling all bits, removing all elements.

Probably faster than what fill(.., false) would be.

Examples
use rbitset::BitSet8;

let mut set = BitSet8::new();
set.insert(1);
assert!(!set.is_empty());
set.clear();
assert!(set.is_empty());

Return the inner integer array.

Examples
use rbitset::BitSet8;

let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.into_inner(), [0b00001110]);

Returns the capacity of the set, in other words how many bits it can hold.

This function may very well overflow if the size or length is too big, but if you’re making that big allocations you probably got bigger things to worry about.

Examples
use rbitset::BitSet;

let capacity = BitSet::<u32, 3>::capacity();
assert_eq!(capacity, 32 * 3);

Transmutes a reference to a borrowed bit array to a borrowed BitSet with the same lifetime.

Examples
use rbitset::BitSet;

let mut raw = [0b00001110, 0u8];
let set = BitSet::from_ref(&mut raw);
assert!(set.contains(1));
assert!(set.contains(2));
assert!(set.contains(3));

Tries to move all elements from other into self, leaving other empty.

Examples
use rbitset::BitSet16;

let mut a = BitSet16::new();
a.insert(1);
a.insert(2);
a.insert(3);

let mut b = BitSet16::new();
b.insert(3);
b.insert(4);
b.insert(5);

a.try_append(&mut b).expect("An error ocurred");

assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);

assert!(a.contains(1));
assert!(a.contains(2));
assert!(a.contains(3));
assert!(a.contains(4));
assert!(a.contains(5));

Tries to add a value to the set.

If the set did not have this value present, true is returned.

If the set did have this value present, false is returned.

Examples
use rbitset::{BitSet16, BitSetError};

let mut set = BitSet16::new();

assert_eq!(set.try_insert(2), Ok(true));
assert_eq!(set.try_insert(2), Ok(false));
assert_eq!(set.try_insert(16), Err(BitSetError::BiggerThanCapacity));

Removes a value from the set. Returns whether the value was present in the set.

If the bit is already disabled this is a no-op.

Examples
use rbitset::BitSet8;

let mut set = BitSet8::new();

set.insert(2);
assert_eq!(set.remove(2), true);
assert_eq!(set.remove(2), false);

Move all elements from other into self, leaving other empty.

Panics

This function may panic if other contains activated bits bigger than what self capacity.

Examples
use rbitset::BitSet16;

let mut a = BitSet16::new();
a.insert(1);
a.insert(2);
a.insert(3);

let mut b = BitSet16::new();
b.insert(3);
b.insert(4);
b.insert(5);

a.append(&mut b);

assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);

assert!(a.contains(1));
assert!(a.contains(2));
assert!(a.contains(3));
assert!(a.contains(4));
assert!(a.contains(5));

Adds a value to the set.

If the set did not have this value present, true is returned.

If the set did have this value present, false is returned.

Panics

This function may panic if bit value trying to be inserted is bigger than the capacity of the BitSet. Check try_insert for a non-panicking version

Examples
use rbitset::BitSet16;

let mut set = BitSet16::new();

assert_eq!(set.insert(2), true);
assert_eq!(set.insert(2), false);
assert_eq!(set.len(), 1);

Removes a value from the set. Returns whether the value was present in the set.

If the bit is already disabled this is a no-op.

Panics

This function may panic if bit value trying to be removed is bigger than the capacity of the BitSet. Check try_remove for a non-panicking version

Examples
use rbitset::BitSet8;

let mut set = BitSet8::new();

set.insert(2);
assert_eq!(set.remove(2), true);
assert_eq!(set.remove(2), false);

Retains only the elements specified by the predicate.

In other words, remove all elements e for which f(&e) returns false. The elements are visited in ascending order.

Examples
use rbitset::BitSet16;

let mut set = BitSet16::from_iter([1u8, 2, 3, 4, 5, 6]);
// Keep only the even numbers.
set.retain(|k| k % 2 == 0);
let res = BitSet16::from_iter([2u8, 4, 6]);
assert_eq!(set, res);

Returns true if the specified bit is enabled, in other words, if the set contains a value.

Examples
use rbitset::BitSet8;

let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.contains(1), true);
assert_eq!(set.contains(4), false);

Returns true if the specified bit is enabled, in other words, if the set contains a value.

Examples
use rbitset::BitSet8;

let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.try_contains(1), Ok(true));
assert_eq!(set.try_contains(4), Ok(false));

Returns the number of elements in the set.

Examples
use rbitset::BitSet16;

let mut set = BitSet16::new();
assert_eq!(set.len(), 0);
set.insert(1);
assert_eq!(set.len(), 1);

Returns true if the set contains no elements.

Examples
use rbitset::BitSet16;

let mut set = BitSet16::new();
assert!(set.is_empty());
set.insert(1);
assert!(!set.is_empty());

Returns true if self has no elements in common with other. This is equivalent to checking for an empty intersection.

Examples
use rbitset::BitSet128;

let a = BitSet128::from_iter([1u8, 2, 3]);
let mut b = BitSet128::new();

assert!(a.is_disjoint(&b));
b.insert(4);
assert!(a.is_disjoint(&b));
b.insert(1);
assert!(!a.is_disjoint(&b));

Returns true if the set is a subset of another, i.e., other contains at least all the values in self.

Examples
use rbitset::BitSet8;

let sup = BitSet8::from_iter([1u8, 2, 3]);
let mut set = BitSet8::new();

assert!(set.is_subset(&sup));
set.insert(2);
assert!(set.is_subset(&sup));
set.insert(4);
assert!(!set.is_subset(&sup));

Returns true if the set is a superset of another, i.e., self contains at least all the values in other.

Examples
use rbitset::BitSet8;

let sub = BitSet8::from_iter([1u8, 2]);
let mut set = BitSet8::new();

assert!(!set.is_superset(&sub));

set.insert(0);
set.insert(1);
assert!(!set.is_superset(&sub));

set.insert(2);
assert!(set.is_superset(&sub));

Returns the total number of enabled bits.

Examples
use rbitset::BitSet8;

let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.count_ones(), 3);

Returns the total number of disabled bits.

Examples
use rbitset::BitSet8;

let set = BitSet8::from_iter([1u8, 2, 3]);
assert_eq!(set.count_zeros(), 5);

Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.

If the returned iterator is dropped before being fully consumed, it drops the remaining elements. The returned iterator keeps a mutable borrow on the vector to optimize its implementation.

Examples
use rbitset::BitSet8;

let mut set = BitSet8::from_iter([1u8, 2, 3]);
assert!(!set.is_empty());

for i in set.drain() {
    println!("{i}");
}

assert!(set.is_empty());

Visits the values representing the difference, i.e., the values that are in self but not in other.

Examples
use rbitset::BitSet8;

let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);

// Can be seen as `a - b`.
for x in a.difference(&b) {
    println!("{x}"); // Print 1
}

let diff: BitSet8 = a.difference(&b).collect();
let res = BitSet8::from_iter([1u8]);
assert_eq!(diff, res);

// Note that difference is not symmetric,
// and `b - a` means something else:
let diff: BitSet8 = b.difference(&a).collect();
let res = BitSet8::from_iter([4u8]);
assert_eq!(diff, res);

Visits the values representing the intersection, i.e., the values that are both in self and other.

Examples
use rbitset::BitSet8;

let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);

for x in a.intersection(&b) {
    println!("{x}");
}

let intersection: BitSet8 = a.intersection(&b).collect();
let test = BitSet8::from_iter([2u8, 3]);
assert_eq!(intersection, test);

Visits the values representing the symmetric difference, i.e., the values that are in self or in other but not in both.

Examples
use rbitset::BitSet8;

let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);

for x in a.symmetric_difference(&b) {
    println!("{x}");
}

let diff1: BitSet8 = a.symmetric_difference(&b).collect();
let diff2: BitSet8 = b.symmetric_difference(&a).collect();

assert_eq!(diff1, diff2);
let res = BitSet8::from_iter([1u8, 4]);
assert_eq!(diff1, res);

Visits the values representing the union, i.e., all the values in self or other, without duplicates.

Examples
use rbitset::BitSet8;

let a = BitSet8::from_iter([1u8, 2, 3]);
let b = BitSet8::from_iter([4u8, 2, 3, 4]);

for x in a.union(&b) {
    println!("{x}");
}

let union: BitSet8 = a.union(&b).collect();
let res = BitSet8::from_iter([1u8, 2, 3, 4]);
assert_eq!(union, res);

An iterator visiting all elements in the set.

Examples
use rbitset::BitSet8;

let mut set = BitSet8::new();
set.insert(1);
set.insert(2);

for x in set.iter() {
    println!("{x}");
}

Set all bits in a range. fill(.., false) is effectively the same as clear().

Panics

Panics if the start or end bounds are more than the capacity.

Trait Implementations

Formats the value using the given formatter.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Converts to this type from the input type.

Creates a value from an iterator. Read more

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.