Binvec

Struct Binvec 

Source
pub struct Binvec<const L: usize, const N: usize> { /* private fields */ }
Expand description

A fixed-length array type that stores 0 or 1. It uses up to 8 times less memory compared to a bool array.


§Note

The bool type uses 1 byte (8 bits). Therefore, storing 100 bools requires 100 bytes (800 bits). In general situations, this is not a big problem, but when the array length exceeds thousands or in embedded environments with limited memory capacity, this memory usage cannot be ignored.

To use minimal memory, calculate the minimum byte array size N needed to store L bits. The formula is as follows:

N = (L + 7) / 8

For example, to store 10 bits, 2 bytes are used. In this case, the remaining 6 bits are unused.

|----- byte0 -----|----- byte1 -----|
| 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 |
  ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
      10 bits used       Not used

§Generics

  • L: The number of bits to store.
  • N: The minimum byte array length required to store L.

§Examples

use binvec::*;
 
// A binvec storing 12 bits initialized to 0
let mut binvec = binvec!(12, false);

Implementations§

Source§

impl<const L: usize, const N: usize> Binvec<L, N>

Source

pub const fn len(&self) -> usize

Returns the length in bits of the Binvec.


§Returns

The number of bits stored in the Binvec.


§Examples
use binvec::*;

let binvec = binvec!(12, false);
assert_eq!(binvec.len(), 12);
Source

pub const unsafe fn get_unchecked(&self, index: usize) -> bool

Returns the bit value at the given index without performing bounds checking.


§Safety

Calling this method with an out-of-bounds index is undefined behavior.


§Arguments
  • index: The bit index to retrieve.

§Returns
  • true if the bit at index is 1.
  • false if the bit at index is 0.

§Examples
use binvec::*;

let binvec = binvec!(12, true);
let bit = unsafe { binvec.get_unchecked(5) };
assert_eq!(bit, true);
Source

pub fn get(&self, index: usize) -> Option<bool>

Returns the bit value at the given index with bounds checking.


§Arguments
  • index: The bit index to retrieve.

§Returns
  • Some(true) if the bit at index is 1.
  • Some(false) if the bit at index is 0.
  • None if index is out of bounds.
§Examples
use binvec::*;

let binvec = binvec!(12, true);
assert_eq!(binvec.get(5), Some(true));
assert_eq!(binvec.get(20), None);
Source

pub unsafe fn set_unchecked(&mut self, index: usize, value: bool)

Sets the bit value at the given index without performing bounds checking.


§Safety

Calling this method with an out-of-bounds index is undefined behavior.


§Arguments
  • index: The bit index to set.
  • value: The bit value to set (true for 1, false for 0).

§Examples
use binvec::*;

let mut binvec = binvec!(12, false);
unsafe { binvec.set_unchecked(3, true); }
assert_eq!(binvec.get(3), Some(true));
Source

pub fn set(&mut self, index: usize, value: bool) -> Result<(), IndexOutOfBounds>

Sets the bit value at the given index with bounds checking.


§Arguments
  • index: The bit index to set.
  • value: The bit value to set (true for 1, false for 0).

§Returns
  • Ok(()) if the bit was successfully set.
  • Err(()) if the index is out of bounds.

§Examples
use binvec::*;

let mut binvec = binvec!(12, false);
assert_eq!(binvec.set(5, true), Ok(()));
assert_eq!(binvec.get(5), Some(true));

assert_eq!(binvec.set(20, true), Err(error::IndexOutOfBounds));
Source

pub const fn fill(&mut self, value: bool)

Fills the entire Binvec with the specified bit value.

This method sets all bits in the Binvec to either true (1) or false (0). For bits beyond the length L in the last byte, those bits are cleared to zero.


§Arguments
  • value: The bit value to fill the Binvec with (true for 1, false for 0).

§Examples
use binvec::*;

let mut binvec = binvec!(12, false);
binvec.fill(true);
assert_eq!(binvec.is_all_one(), true);
Source

pub const fn count_ones(&self) -> usize

Counts the number of bits set to 1 in the Binvec.


§Returns

The total count of bits that are set to 1.


§Examples
use binvec::*;

let binvec = binvec!(12, true);
assert_eq!(binvec.count_ones(), 12);

let mut binvec = binvec!(12, false);
binvec.set(3, true).unwrap();
assert_eq!(binvec.count_ones(), 1);
Source

pub const fn count_zeros(&self) -> usize

Counts the number of bits set to 0 in the Binvec.


§Returns

The total count of bits that are set to 0.


§Examples
use binvec::*;

let binvec = binvec!(12, false);
assert_eq!(binvec.count_zeros(), 12);

let mut binvec = binvec!(12, true);
binvec.set(3, false).unwrap();
assert_eq!(binvec.count_zeros(), 1);
Source

pub const fn is_all_one(&self) -> bool

Checks if all bits in the Binvec are set to 1.


§Returns

true if all bits that are using is 1, otherwise false.


§Examples
use binvec::*;

let binvec = binvec!(12, true);
assert_eq!(binvec.is_all_one(), true);

let mut binvec = binvec!(12, false);
assert_eq!(binvec.is_all_one(), false);
binvec.fill(true);
assert_eq!(binvec.is_all_one(), true);
Source

pub const fn is_all_zero(&self) -> bool

Checks if all bits in the Binvec are set to 0.


§Returns

true if all bits that are using is 0, otherwise false.


§Examples
use binvec::*;

let binvec = binvec!(12, false);
assert_eq!(binvec.is_all_zero(), true);

let mut binvec = binvec!(12, true);
assert_eq!(binvec.is_all_zero(), false);
binvec.fill(false);
assert_eq!(binvec.is_all_zero(), true);
Source

pub fn iter(&self) -> BinvecIter<'_, L, N>

Returns an iterator over the bits of the Binvec.


§Returns

A BinvecIter that yields each bit as a bool.


§Examples
use binvec::*;

let binvec = binvec!(12, true);
for bit in binvec.iter() {
    assert_eq!(bit, true);
}

Trait Implementations§

Source§

impl<const L: usize, const N: usize> Clone for Binvec<L, N>

Source§

fn clone(&self) -> Binvec<L, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const L: usize, const N: usize> Debug for Binvec<L, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const L: usize, const N: usize> Display for Binvec<L, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, const L: usize, const N: usize> IntoIterator for &'a Binvec<L, N>

Source§

type Item = bool

The type of the elements being iterated over.
Source§

type IntoIter = BinvecIter<'a, L, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<const L: usize, const N: usize> PartialEq for Binvec<L, N>

Source§

fn eq(&self, other: &Binvec<L, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const L: usize, const N: usize> Eq for Binvec<L, N>

Source§

impl<const L: usize, const N: usize> StructuralPartialEq for Binvec<L, N>

Auto Trait Implementations§

§

impl<const L: usize, const N: usize> Freeze for Binvec<L, N>

§

impl<const L: usize, const N: usize> RefUnwindSafe for Binvec<L, N>

§

impl<const L: usize, const N: usize> Send for Binvec<L, N>

§

impl<const L: usize, const N: usize> Sync for Binvec<L, N>

§

impl<const L: usize, const N: usize> Unpin for Binvec<L, N>

§

impl<const L: usize, const N: usize> UnwindSafe for Binvec<L, N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.