Struct BoolVec

Source
pub struct BoolVec { /* private fields */ }
Expand description

A vector that can store 8 booleans in a single byte

You can use the BoolVec::new(), BoolVec::default() and BoolVec::with_size() functions to initialize a new BoolVec.

Or you can use the bool_vec::boolvec![] macro, which works exactly like the vec![] macro.


§Capacity

Capacity is always a multiple of 8, check BoolVec.capacity() docs for more infos.

§Length

Length works like the length of a normal Vec.


§Formatting specifiers

§You can debug print and pretty print:

use bool_vec::boolvec;

let bv = boolvec![true; 16];

// Debug print the vector
println!("{bv:?}");

// Pretty-print the vector. Prints 8 values for each line
println!("{bv:#?}");

§You can print the underlying bytes of the BoolVec if you need them

use bool_vec::boolvec;

let mut bv = boolvec![true; 9];
bv.set(2, false).unwrap();

assert_eq!(format!("{bv:b}"), "[11011111, 10000000]")
// You can also apply padding and pretty printing. See formatting specifiers.

Implementations§

Source§

impl BoolVec

Source

pub fn new() -> Self

Allocate empty BoolVec with capacity: 0 and len: 0

use bool_vec::{boolvec, BoolVec};
let bv = BoolVec::new();

assert_eq!(bv, boolvec![]);
Source

pub fn with_capacity(capacity: usize) -> Self

Allocate empty BoolVec with specified capacity and len: 0

use bool_vec::{boolvec, BoolVec};
let bv1 = BoolVec::with_capacity(3);
let mut bv2 = boolvec![true; 3];
bv2.pop();
bv2.pop();
bv2.pop();

assert_eq!(bv1.capacity(), 8);
assert_eq!(bv1.capacity(), bv2.capacity());

To see why capacity in this case is 8 please do check BoolVec::capacity() documentation

Source

pub fn from<S: AsRef<[bool]>>(slice: S) -> Self

Create BoolVec from a slice or vector of booleans

use bool_vec::{boolvec, BoolVec};
let bv = BoolVec::from([true, false, true]);

assert_eq!(bv, boolvec![true, false, true]);
Source

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

Get bool value from a BoolVec. Returns None if index overflows BoolVec.len()

use bool_vec::boolvec;

let bv = boolvec![true, false, true];

assert_eq!(bv.get(0), Some(true));
assert_eq!(bv.get(4), None);
Source

pub fn set(&mut self, int_index: usize, value: bool) -> Option<()>

Set bool value in vector. Returns None if index overflows BoolVec.len()

use bool_vec::boolvec;

let mut bv = boolvec![true, false, true];
bv.set(0, false);

assert_eq!(bv.get(0), Some(false));
assert_eq!(bv.set(4, true), None);
Source

pub fn negate(&mut self, int_index: usize) -> Option<bool>

Alters BoolVec by negating the value at the specified index. Returns None if index overflows BoolVec.len(), otherwise returns Some(negated value)

use bool_vec::boolvec;

let mut bv = boolvec![true, false, true];

assert_eq!(bv.negate(0), Some(false));
assert_eq!(bv.get(0), Some(false));
Source

pub fn push(&mut self, value: bool)

Appends a bool to the back of a BoolVec

use bool_vec::boolvec;

let mut bv = boolvec![true; 8];

assert_eq!(bv.capacity(), bv.bytes_capacity()*8);
assert_eq!(bv.len(), 8);
assert_eq!(bv.bytes_len(), 1);

bv.push(false);

assert_eq!(bv.capacity(), bv.bytes_capacity()*8);
assert_eq!(bv.len(), 9);
assert_eq!(bv.bytes_len(), 2);

assert_eq!(bv.get(8), Some(false));
Source

pub fn pop(&mut self) -> Option<bool>

Removes the last element from a BoolVec and returns it, or None if it is empty

use bool_vec::boolvec;

let mut bv = boolvec![true; 8];

assert_eq!(bv.capacity(), 8);
assert_eq!(bv.len(), 8);

assert_eq!(bv.pop(), Some(true));

assert_eq!(bv.capacity(), 8);
assert_eq!(bv.len(), 7);

use bool_vec::boolvec;
let mut bv1 = boolvec![true];
let bv2 = boolvec![];

bv1.pop();

assert_eq!(bv1, bv2);
Source

pub fn capacity(&self) -> usize

Returns vector capacity (how many booleans the BoolVec can hold without reallocating). Capacity will always be a power of 8 since a single boolean takes 1 bit of space and you can only allocate a minimum of 1 byte at a time.

For example 8 booleans here take a single byte of space.

use bool_vec::boolvec;


let bv = boolvec![true; 10];

assert_eq!(bv.capacity(), 16);
assert_eq!(bv.bytes_len(), 2);

In the example 2 bytes are currently allocated, please read BoolVec::bytes_len() documentation for further details.

Source

pub fn len(&self) -> usize

Returns BoolVec’s length (the number of booleans in the BoolVec)

use bool_vec::boolvec;


let bv = boolvec![true; 10];

assert_eq!(bv.len(), 10);
Source

pub fn is_empty(&self) -> bool

Returns true if the BoolVec contains no booleans

use bool_vec::boolvec;

let bv = boolvec![];

assert!(bv.is_empty());

use bool_vec::boolvec;

let mut bv = boolvec![true];
bv.pop();

assert!(bv.is_empty());
Source

pub fn bytes_capacity(&self) -> usize

Returns the capacity of the underlying vector that stores the values. This function is here just for testing purposes

This capacity is expressed in bytes, while the normal capacity of BoolVec is expressed in bits.

use bool_vec::boolvec;

let mut bv = boolvec![true; 8];
bv.push(true);

assert_eq!(bv.capacity(), bv.bytes_capacity()*8);
Source

pub fn bytes_len(&self) -> usize

Returns the number of elements of the underlying vector that stores the values. This function is here just for testing purposes

This length is expressed in bytes, while the normal length of BoolVec is expressed in bits.

use bool_vec::boolvec;

let mut bv = boolvec![true; 8];

assert_eq!(bv.len(), 8);
assert_eq!(bv.bytes_len(), 1);

bv.push(true);

assert_eq!(bv.len(), 9);
assert_eq!(bv.bytes_len(), 2);
Source

pub fn into_vec(&self) -> Vec<bool>

Copies BoolVec data into a Vec<bool>

use bool_vec::boolvec;
 
let bv = boolvec![true, false, true];

let vector = bv.into_vec();

assert_eq!(vector, vec![true, false, true]);

To achieve the opposite (getting a BoolVec from a Vec<bool> or a &[bool]), please see BoolVec::from()

WARNING: This might require a non indifferent amount of memory if you are working with a large amount of data, please consider trying to work with BoolVec directly if you can

Trait Implementations§

Source§

impl Binary for BoolVec

Source§

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

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

impl Clone for BoolVec

Source§

fn clone(&self) -> BoolVec

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 Debug for BoolVec

Source§

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

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

impl Default for BoolVec

Source§

fn default() -> BoolVec

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

impl<'a> IntoIterator for &'a BoolVec

Source§

fn into_iter(self) -> Self::IntoIter

Convert BoolVec into an iterator

Source§

type Item = bool

The type of the elements being iterated over.
Source§

type IntoIter = BoolVecIter<'a>

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

impl PartialEq for BoolVec

Source§

fn eq(&self, other: &Self) -> 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.

Auto Trait Implementations§

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, 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.