pub type NodeFeatures = BitVec<u8, Lsb0>;
Expand description
Bit indices in the HostConfiguration.node_features
that correspond to different node features.
Aliased Type§
struct NodeFeatures { /* private fields */ }
Implementations
§impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
Constructors.
pub const EMPTY: BitVec<T, O> = _
pub const EMPTY: BitVec<T, O> = _
An empty bit-vector with no backing allocation.
pub fn repeat(bit: bool, len: usize) -> BitVec<T, O> ⓘ
pub fn repeat(bit: bool, len: usize) -> BitVec<T, O> ⓘ
Creates a new bit-vector by repeating a bit for the desired length.
§Examples
use bitvec::prelude::*;
let zeros = BitVec::<u8, Msb0>::repeat(false, 50);
let ones = BitVec::<u16, Lsb0>::repeat(true, 50);
pub fn from_bitslice(slice: &BitSlice<T, O>) -> BitVec<T, O> ⓘ
pub fn from_bitslice(slice: &BitSlice<T, O>) -> BitVec<T, O> ⓘ
Copies the contents of a bit-slice into a new heap allocation.
This copies the raw underlying elements into a new allocation, and sets
the produced bit-vector to use the same memory layout as the originating
bit-slice. This means that it may begin at any bit in the first element,
not just the zeroth bit. If you require this property, call
.force_align()
.
Dead bits in the copied memory elements are guaranteed to be zeroed.
§Examples
use bitvec::prelude::*;
let bits = bits![0, 1, 0, 0, 1];
let bv = BitVec::from_bitslice(bits);
assert_eq!(bv, bits);
pub fn from_element(elem: T) -> BitVec<T, O> ⓘ
pub fn from_element(elem: T) -> BitVec<T, O> ⓘ
Constructs a new bit-vector from a single element.
This copies elem
into a new heap allocation, and sets the bit-vector
to cover it entirely.
§Examples
use bitvec::prelude::*;
let bv = BitVec::<_, Msb0>::from_element(1u8);
assert!(bv[7]);
pub fn from_slice(slice: &[T]) -> BitVec<T, O> ⓘ
pub fn from_slice(slice: &[T]) -> BitVec<T, O> ⓘ
Constructs a new bit-vector from a slice of memory elements.
This copies slice
into a new heap allocation, and sets the bit-vector
to cover it entirely.
§Panics
This panics if slice
exceeds bit-vector capacity.
§Examples
use bitvec::prelude::*;
let slice = &[0u8, 1, 2, 3];
let bv = BitVec::<_, Lsb0>::from_slice(slice);
assert_eq!(bv.len(), 32);
pub fn try_from_slice(slice: &[T]) -> Result<BitVec<T, O>, BitSpanError<T>>
pub fn try_from_slice(slice: &[T]) -> Result<BitVec<T, O>, BitSpanError<T>>
Fallibly constructs a new bit-vector from a slice of memory elements.
This fails early if slice
exceeds bit-vector capacity. If it is not,
then slice
is copied into a new heap allocation and fully spanned by
the returned bit-vector.
§Examples
use bitvec::prelude::*;
let slice = &[0u8, 1, 2, 3];
let bv = BitVec::<_, Lsb0>::try_from_slice(slice).unwrap();
assert_eq!(bv.len(), 32);
pub fn from_vec(vec: Vec<T>) -> BitVec<T, O> ⓘ
pub fn from_vec(vec: Vec<T>) -> BitVec<T, O> ⓘ
Converts a regular vector in-place into a bit-vector.
The produced bit-vector spans every bit in the original vector. No reällocation occurs; this is purely a transform of the handle.
§Panics
This panics if the source vector is too long to view as a bit-slice.
§Examples
use bitvec::prelude::*;
let v = vec![0u8, 1, 2, 3];
let bv = BitVec::<_, Msb0>::from_vec(v);
assert_eq!(bv.len(), 32);
pub fn try_from_vec(vec: Vec<T>) -> Result<BitVec<T, O>, Vec<T>>
pub fn try_from_vec(vec: Vec<T>) -> Result<BitVec<T, O>, Vec<T>>
Attempts to convert a regular vector in-place into a bit-vector.
This fails if the source vector is too long to view as a bit-slice. On success, the produced bit-vector spans every bit in the original vector. No reällocation occurs; this is purely a transform of the handle.
§Examples
use bitvec::prelude::*;
let v = vec![0u8; 20];
assert_eq!(BitVec::<_, Msb0>::try_from_vec(v).unwrap().len(), 160);
It is not practical to allocate a vector that will fail this conversion.
pub fn extend_from_bitslice<T2, O2>(&mut self, other: &BitSlice<T2, O2>)where
T2: BitStore,
O2: BitOrder,
pub fn extend_from_bitslice<T2, O2>(&mut self, other: &BitSlice<T2, O2>)where
T2: BitStore,
O2: BitOrder,
Appends the contents of a bit-slice to a bit-vector.
This can extend from a bit-slice of any type parameters; it is not
restricted to using the same parameters as self
. However, when the
type parameters do match, it is possible for this to use a batch-copy
optimization to go faster than the individual-bit crawl that is
necessary when they differ.
Until Rust provides extensive support for specialization in trait
implementations, you should use this method whenever you are extending
from a BitSlice
proper, and only use the general .extend()
implementation if you are required to use a generic bool
source.
§Original
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1];
bv.extend_from_bitslice(bits![0, 1, 0, 0, 1]);
assert_eq!(bv, bits![0, 1, 0, 1, 0, 0, 1]);
pub fn extend_from_raw_slice(&mut self, slice: &[T])
pub fn extend_from_raw_slice(&mut self, slice: &[T])
Appends a slice of T
elements to a bit-vector.
The slice is viewed as a BitSlice<T, O>
, then appended directly to the
bit-vector.
§Original
§impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
Converters.
pub fn as_bitslice(&self) -> &BitSlice<T, O> ⓘ
pub fn as_bitslice(&self) -> &BitSlice<T, O> ⓘ
Explicitly views the bit-vector as a bit-slice.
pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<T, O> ⓘ
pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<T, O> ⓘ
Explicitly views the bit-vector as a mutable bit-slice.
pub fn as_raw_slice(&self) -> &[T]
pub fn as_raw_slice(&self) -> &[T]
Views the bit-vector as a slice of its underlying memory elements.
pub fn as_raw_mut_slice(&mut self) -> &mut [T]
pub fn as_raw_mut_slice(&mut self) -> &mut [T]
Views the bit-vector as a mutable slice of its underlying memory elements.
pub fn as_bitptr(&self) -> BitPtr<Const, T, O>
pub fn as_bitptr(&self) -> BitPtr<Const, T, O>
pub fn as_mut_bitptr(&mut self) -> BitPtr<Mut, T, O>
pub fn as_mut_bitptr(&mut self) -> BitPtr<Mut, T, O>
pub fn into_boxed_bitslice(self) -> BitBox<T, O>
pub fn into_boxed_bitslice(self) -> BitBox<T, O>
pub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Converts a bit-vector into a Vec
of its underlying storage.
The produced vector contains all elements that contained live bits. Dead
bits have an unspecified value; you should call .set_uninitialized()
before converting into a vector.
This does not affect the allocated memory; it is purely a conversion of the handle.
§Examples
use bitvec::prelude::*;
let bv = bitvec![u8, Msb0; 0, 1, 0, 0, 1];
let v = bv.into_vec();
assert_eq!(v[0] & 0xF8, 0b01001_000);
§impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
Port of the Vec<T>
inherent API.
pub fn new() -> BitVec<T, O> ⓘ
pub fn new() -> BitVec<T, O> ⓘ
Constructs a new, empty, bit-vector.
This does not allocate until bits are .push()
ed into it, or space is
explicitly .reserve()
d.
§Original
§Examples
use bitvec::prelude::*;
let bv = BitVec::<u8, Msb0>::new();
assert!(bv.is_empty());
pub fn with_capacity(capacity: usize) -> BitVec<T, O> ⓘ
pub fn with_capacity(capacity: usize) -> BitVec<T, O> ⓘ
Allocates a new, empty, bit-vector with space for at least capacity
bits before reallocating.
§Original
§Panics
This panics if the requested capacity is longer than what the bit-vector
can represent. See BitSlice::MAX_BITS
.
§Examples
use bitvec::prelude::*;
let mut bv: BitVec = BitVec::with_capacity(128);
assert!(bv.is_empty());
assert!(bv.capacity() >= 128);
for i in 0 .. 128 {
bv.push(i & 0xC0 == i);
}
assert_eq!(bv.len(), 128);
assert!(bv.capacity() >= 128);
bv.push(false);
assert_eq!(bv.len(), 129);
assert!(bv.capacity() >= 129);
pub unsafe fn from_raw_parts(
bitptr: BitPtr<Mut, T, O>,
length: usize,
capacity: usize,
) -> BitVec<T, O> ⓘ
pub unsafe fn from_raw_parts( bitptr: BitPtr<Mut, T, O>, length: usize, capacity: usize, ) -> BitVec<T, O> ⓘ
Constructs a bit-vector handle from its constituent fields.
§Original
§Safety
The only acceptable argument values for this function are those that
were previously produced by calling .into_raw_parts()
. Furthermore,
you may only call this at most once on any set of arguments. Using
the same arguments in more than one call to this function will result in
a double- or use-after free error.
Attempting to conjure your own values and pass them into this function will break the allocator state.
§Examples
use bitvec::prelude::*;
let bv = bitvec![0, 1, 0, 0, 1];
let (bitptr, len, capa) = bv.into_raw_parts();
let bv2 = unsafe {
BitVec::from_raw_parts(bitptr, len, capa)
};
assert_eq!(bv2, bits![0, 1, 0, 0, 1]);
pub fn into_raw_parts(self) -> (BitPtr<Mut, T, O>, usize, usize)
pub fn into_raw_parts(self) -> (BitPtr<Mut, T, O>, usize, usize)
Decomposes a bit-vector into its constituent member fields.
This disarms the destructor. In order to prevent a memory leak, you must
pass these exact values back into ::from_raw_parts()
.
§Original
§API Differences
This method is still unstable as of 1.54. It is provided here as a convenience, under the expectation that the standard-library method will stabilize as-is.
pub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Gets the allocation capacity, measured in bits.
This counts how many total bits the bit-vector can store before it must perform a reällocation to acquire more memory.
If the capacity is not a multiple of 8, you should call
.force_align()
.
§Original
§Examples
use bitvec::prelude::*;
let bv = bitvec![0, 1, 0, 0, 1];
pub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Ensures that the bit-vector has allocation capacity for at least
additional
more bits to be appended to it.
For convenience, this method guarantees that the underlying memory for
self[.. self.len() + additional]
is initialized, and may be safely
accessed directly without requiring use of .push()
or .extend()
to
initialize it.
Newly-allocated memory is always initialized to zero. It is still dead
until the bit-vector is grown (by .push()
, .extend()
, or
.set_len()
), but direct access will not trigger UB.
§Original
§Panics
This panics if the new capacity exceeds the bit-vector’s maximum.
§Examples
use bitvec::prelude::*;
let mut bv: BitVec = BitVec::with_capacity(80);
assert!(bv.capacity() >= 80);
bv.reserve(800);
assert!(bv.capacity() >= 800);
pub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Ensures that the bit-vector has allocation capacity for at least
additional
more bits to be appended to it.
This differs from .reserve()
by requesting that the allocator
provide the minimum capacity necessary, rather than a potentially larger
amount that the allocator may find more convenient.
Remember that this is a request: the allocator provides what it
provides, and you cannot rely on the new capacity to be exactly minimal.
You should still prefer .reserve()
, especially if you expect to append
to the bit-vector in the future.
§Original
§Panics
This panics if the new capacity exceeds the bit-vector’s maximum.
§Examples
use bitvec::prelude::*;
let mut bv: BitVec = BitVec::with_capacity(80);
assert!(bv.capacity() >= 80);
bv.reserve_exact(800);
assert!(bv.capacity() >= 800);
pub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Releases excess capacity back to the allocator.
Like .reserve_exact()
, this is a request to the allocator, not a
command. The allocator may reclaim excess memory or may not.
§Original
§Examples
use bitvec::prelude::*;
let mut bv: BitVec = BitVec::with_capacity(1000);
bv.push(true);
bv.shrink_to_fit();
pub fn into_boxed_slice(self) -> BitBox<T, O>
pub fn truncate(&mut self, new_len: usize)
pub fn truncate(&mut self, new_len: usize)
Shortens the bit-vector, keeping the first new_len
bits and discarding
the rest.
If len
is greater than the bit-vector’s current length, this has no
effect.
The .drain()
method can emulate .truncate()
, except that it yields
the excess bits rather than discarding them.
Note that this has no effect on the allocated capacity of the
bit-vector, nor does it erase truncated memory. Bits in the
allocated memory that are outside of the .as_bitslice()
view are
always considered to have initialized, but unspecified, values,
and you cannot rely on them to be zero.
§Original
§Examples
Truncating a five-bit vector to two bits:
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 0, 0, 1];
bv.truncate(2);
assert_eq!(bv.len(), 2);
assert!(bv.as_raw_slice()[0].count_ones() >= 2);
No truncation occurs when len
is greater than the bit-vector’s current
length:
pub fn as_slice(&self) -> &BitSlice<T, O> ⓘ
.as_bitslice()
insteadpub fn as_mut_slice(&mut self) -> &mut BitSlice<T, O> ⓘ
.as_mut_bitslice()
insteadpub fn as_ptr(&self) -> BitPtr<Const, T, O>
.as_bitptr()
insteadpub fn as_mut_ptr(&mut self) -> BitPtr<Mut, T, O>
.as_mut_bitptr()
insteadpub unsafe fn set_len(&mut self, new_len: usize)
pub unsafe fn set_len(&mut self, new_len: usize)
Resizes a bit-vector to a new length.
§Original
§Safety
NOT ALL MEMORY IN THE ALLOCATION IS INITIALIZED!
Memory in a bit-vector’s allocation is only initialized when the
bit-vector grows into it normally (through .push()
or one of the
various .extend*()
methods). Setting the length to a value beyond what
was previously initialized, but still within the allocation, is
undefined behavior.
The caller is responsible for ensuring that all memory up to (but not including) the new length has already been initialized.
§Panics
This panics if new_len
exceeds the capacity as reported by
.capacity()
.
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 0, 0, 1];
unsafe {
// The default storage type, `usize`, is at least 32 bits.
bv.set_len(32);
}
assert_eq!(bv, bits![
0, 1, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
]);
// `BitVec` guarantees that newly-initialized memory is zeroed.
pub fn swap_remove(&mut self, index: usize) -> bool
pub fn swap_remove(&mut self, index: usize) -> bool
Takes a bit out of the bit-vector.
The empty slot is filled with the last bit in the bit-vector, rather
than shunting index + 1 .. self.len()
down by one.
§Original
§Panics
This panics if index
is out of bounds (self.len()
or greater).
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 0, 0, 1];
assert!(!bv.swap_remove(2));
assert_eq!(bv, bits![0, 1, 1, 0]);
pub fn insert(&mut self, index: usize, value: bool)
pub fn insert(&mut self, index: usize, value: bool)
pub fn remove(&mut self, index: usize) -> bool
pub fn remove(&mut self, index: usize) -> bool
pub fn retain<F>(&mut self, func: F)
pub fn retain<F>(&mut self, func: F)
Retains only the bits that the predicate allows.
Bits are deleted from the vector when the predicate function returns
false. This function is linear in self.len()
.
§Original
§API Differences
The predicate receives both the index of the bit as well as its value, in order to allow the predicate to have more than one bit of keep/discard information.
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 0, 0, 1];
bv.retain(|idx, _| idx % 2 == 0);
assert_eq!(bv, bits![0, 0, 1]);
pub fn append<T2, O2>(&mut self, other: &mut BitVec<T2, O2>)where
T2: BitStore,
O2: BitOrder,
pub fn append<T2, O2>(&mut self, other: &mut BitVec<T2, O2>)where
T2: BitStore,
O2: BitOrder,
Moves all the bits out of other
into the back of self
.
The other
bit-vector is emptied after this occurs.
§Original
§API Differences
This permits other
to have different type parameters than self
, and
does not require that it be literally Self
.
§Panics
This panics if self.len() + other.len()
exceeds the maximum capacity
of a bit-vector.
§Examples
use bitvec::prelude::*;
let mut bv1 = bitvec![u16, Msb0; 0; 10];
let mut bv2 = bitvec![u32, Lsb0; 1; 10];
bv1.append(&mut bv2);
assert_eq!(bv1.count_ones(), 10);
assert_eq!(bv1.count_zeros(), 10);
assert!(bv2.is_empty());
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, O>where
R: RangeBounds<usize>,
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, O>where
R: RangeBounds<usize>,
Iterates over a portion of the bit-vector, removing all yielded bits from it.
When the iterator drops, all bits in its coverage are removed from
self
, even if the iterator did not yield them. If the iterator is
leaked or otherwise forgotten, and its destructor never runs, then the
amount of un-yielded bits removed from the bit-vector is not specified.
§Original
§Panics
This panics if range
departs 0 .. self.len()
.
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 0, 0, 1];
let bv2 = bv.drain(1 ..= 3).collect::<BitVec>();
assert_eq!(bv, bits![0, 1]);
assert_eq!(bv2, bits![1, 0, 0]);
// A full range clears the bit-vector.
bv.drain(..);
assert!(bv.is_empty());
pub fn clear(&mut self)
pub fn clear(&mut self)
pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Tests if the bit-vector is empty.
This is equivalent to BitSlice::is_empty
; it is provided as an
inherent method here rather than relying on Deref
forwarding so that
you can write BitVec::is_empty
as a named function item.
§Original
pub fn split_off(&mut self, at: usize) -> BitVec<T, O> ⓘ
pub fn split_off(&mut self, at: usize) -> BitVec<T, O> ⓘ
pub fn resize_with<F>(&mut self, new_len: usize, func: F)
pub fn resize_with<F>(&mut self, new_len: usize, func: F)
Resizes the bit-vector to a new length, using a function to produce each inserted bit.
If new_len
is less than self.len()
, this is a truncate operation; if
it is greater, then self
is extended by repeatedly pushing func()
.
§Original
§API Differences
The generator function receives the index into which its bit will be placed.
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![1; 2];
bv.resize_with(5, |idx| idx % 2 == 1);
assert_eq!(bv, bits![1, 1, 0, 1, 0]);
pub fn leak<'a>(self) -> &'a mut BitSlice<T, O> ⓘ
pub fn leak<'a>(self) -> &'a mut BitSlice<T, O> ⓘ
Destroys the BitVec
handle without destroying the bit-vector
allocation. The allocation is returned as an &mut BitSlice
that lasts
for the remaining program lifetime.
You may call [BitBox::from_raw
] on this slice handle exactly once in
order to reap the allocation before program exit. That function takes a
mutable pointer, not a mutable reference, so you must ensure that the
returned reference is never used again after restoring the allocation
handle.
§Original
§Examples
use bitvec::prelude::*;
let bv = bitvec![0, 0, 1];
let static_bits: &'static mut BitSlice = bv.leak();
static_bits.set(0, true);
assert_eq!(static_bits, bits![1, 0, 1]);
let bb = unsafe { BitBox::from_raw(static_bits) };
// static_bits may no longer be used.
drop(bb); // explicitly reap memory before program exit
pub fn resize(&mut self, new_len: usize, value: bool)
pub fn resize(&mut self, new_len: usize, value: bool)
pub fn extend_from_slice<T2, O2>(&mut self, other: &BitSlice<T2, O2>)where
T2: BitStore,
O2: BitOrder,
.extend_from_bitslice()
or .extend_from_raw_slice()
insteadpub fn extend_from_within<R>(&mut self, src: R)where
R: RangeExt<usize>,
pub fn extend_from_within<R>(&mut self, src: R)where
R: RangeExt<usize>,
Extends self
by copying an internal range of its bit-slice as the
region to append.
§Original
§Panics
This panics if src
is not within 0 .. self.len()
.
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 0, 0, 1];
bv.extend_from_within(1 .. 4);
assert_eq!(bv, bits![0, 1, 0, 0, 1, 1, 0, 0]);
pub fn splice<R, I>(
&mut self,
range: R,
replace_with: I,
) -> Splice<'_, T, O, <I as IntoIterator>::IntoIter>
pub fn splice<R, I>( &mut self, range: R, replace_with: I, ) -> Splice<'_, T, O, <I as IntoIterator>::IntoIter>
Modifies self.drain()
so that the removed bit-slice is instead
replaced with the contents of another bit-stream.
As with .drain()
, the specified range is always removed from the
bit-vector even if the splicer is not fully consumed, and the splicer
does not specify how many bits are removed if it leaks.
The replacement source is only consumed when the splicer drops; however, it may be pulled before then. The replacement source cannot assume that there will be a delay between creation of the splicer and when it must begin producing bits.
This copies the Vec::splice
implementation; see its documentation for
more details about how the replacement should act.
§Original
§Panics
This panics if range
departs 0 .. self.len()
.
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 1];
// a b c
let mut yank = bv.splice(
.. 2,
bits![static 1, 1, 0].iter().by_vals(),
// d e f
);
assert!(!yank.next().unwrap()); // a
assert!(yank.next().unwrap()); // b
drop(yank);
assert_eq!(bv, bits![1, 1, 0, 1]);
// d e f c
§impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> BitVec<T, O>where
T: BitStore,
O: BitOrder,
Utilities.
pub fn set_elements(&mut self, element: <T as BitStore>::Mem)
pub fn set_elements(&mut self, element: <T as BitStore>::Mem)
Overwrites each element (visible in .as_raw_mut_slice()
) with a new
bit-pattern.
This unconditionally writes element
into each element in the backing
slice, without altering the bit-vector’s length or capacity.
This guarantees that dead bits visible in .as_raw_slice()
but not
.as_bitslice()
are initialized according to the bit-pattern of
element.
The elements not visible in the raw slice, but present in the
allocation, do not specify a value. You may not rely on them being
zeroed or being set to the element
bit-pattern.
§Parameters
&mut self
element
: The bit-pattern with which each live element in the backing store is initialized.
§Examples
use bitvec::prelude::*;
let mut bv = bitvec![u8, Msb0; 0; 20];
assert_eq!(bv.as_raw_slice(), [0; 3]);
bv.set_elements(0xA5);
assert_eq!(bv.as_raw_slice(), [0xA5; 3]);
pub fn set_uninitialized(&mut self, value: bool)
pub fn set_uninitialized(&mut self, value: bool)
Sets the uninitialized bits of a bit-vector to a known value.
This method modifies all bits that are observable in .as_raw_slice()
but not observable in .as_bitslice()
to a known value.
Memory beyond the raw-slice view, but still within the allocation, is
considered fully dead and will never be seen.
This can be used to zero the unused memory so that when viewed as a raw slice, unused bits have a consistent and predictable value.
§Examples
use bitvec::prelude::*;
let mut bv = 0b1101_1100u8.view_bits::<Lsb0>().to_bitvec();
assert_eq!(bv.as_raw_slice()[0], 0b1101_1100u8);
bv.truncate(4);
assert_eq!(bv.count_ones(), 2);
assert_eq!(bv.as_raw_slice()[0], 0b1101_1100u8);
bv.set_uninitialized(false);
assert_eq!(bv.as_raw_slice()[0], 0b0000_1100u8);
bv.set_uninitialized(true);
assert_eq!(bv.as_raw_slice()[0], 0b1111_1100u8);
pub fn force_align(&mut self)
pub fn force_align(&mut self)
Ensures that the live region of the bit-vector’s contents begin at the front edge of the buffer.
BitVec
has performance optimizations where it moves its view of its
buffer contents in order to avoid needless moves of its data within the
buffer. This can lead to unexpected contents of the raw memory values,
so this method ensures that the semantic contents of the bit-vector
match its in-memory storage.
§Examples
use bitvec::prelude::*;
let data = 0b00_1111_00u8;
let bits = data.view_bits::<Msb0>();
let mut bv = bits[2 .. 6].to_bitvec();
assert_eq!(bv, bits![1; 4]);
assert_eq!(bv.as_raw_slice()[0], data);
bv.force_align();
assert_eq!(bv, bits![1; 4]);
// BitVec does not specify the value of dead bits in its buffer.
assert_eq!(bv.as_raw_slice()[0] & 0xF0, 0xF0);
Trait Implementations
§impl<T, O, Rhs> BitAnd<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitAndAssign<Rhs>,
impl<T, O, Rhs> BitAnd<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitAndAssign<Rhs>,
§impl<T, O, Rhs> BitAndAssign<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitAndAssign<Rhs>,
impl<T, O, Rhs> BitAndAssign<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitAndAssign<Rhs>,
§fn bitand_assign(&mut self, rhs: Rhs)
fn bitand_assign(&mut self, rhs: Rhs)
&=
operation. Read more§impl<T, O> BitField for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
impl<T, O> BitField for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
§impl<T, O, Rhs> BitOr<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitOrAssign<Rhs>,
impl<T, O, Rhs> BitOr<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitOrAssign<Rhs>,
§impl<T, O, Rhs> BitOrAssign<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitOrAssign<Rhs>,
impl<T, O, Rhs> BitOrAssign<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitOrAssign<Rhs>,
§fn bitor_assign(&mut self, rhs: Rhs)
fn bitor_assign(&mut self, rhs: Rhs)
|=
operation. Read more§impl<T, O, Rhs> BitXor<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitXorAssign<Rhs>,
impl<T, O, Rhs> BitXor<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitXorAssign<Rhs>,
§impl<T, O, Rhs> BitXorAssign<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitXorAssign<Rhs>,
impl<T, O, Rhs> BitXorAssign<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitXorAssign<Rhs>,
§fn bitxor_assign(&mut self, rhs: Rhs)
fn bitxor_assign(&mut self, rhs: Rhs)
^=
operation. Read more§impl<T, O> BorrowMut<BitSlice<T, O>> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> BorrowMut<BitSlice<T, O>> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§fn borrow_mut(&mut self) -> &mut BitSlice<T, O> ⓘ
fn borrow_mut(&mut self) -> &mut BitSlice<T, O> ⓘ
§impl<O, T> Decode for BitVec<T, O>where
O: BitOrder,
T: BitStore + Decode,
impl<O, T> Decode for BitVec<T, O>where
O: BitOrder,
T: BitStore + Decode,
§fn decode<I>(input: &mut I) -> Result<BitVec<T, O>, Error>where
I: Input,
fn decode<I>(input: &mut I) -> Result<BitVec<T, O>, Error>where
I: Input,
§fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>,
) -> Result<DecodeFinished, Error>where
I: Input,
fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>,
) -> Result<DecodeFinished, Error>where
I: Input,
§fn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
fn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
§fn encoded_fixed_size() -> Option<usize>
fn encoded_fixed_size() -> Option<usize>
§impl<'de, T, O> Deserialize<'de> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
Vec<T>: Deserialize<'de>,
impl<'de, T, O> Deserialize<'de> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
Vec<T>: Deserialize<'de>,
§fn deserialize<D>(
deserializer: D,
) -> Result<BitVec<T, O>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<BitVec<T, O>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl<O, T> Encode for BitVec<T, O>where
O: BitOrder,
T: BitStore + Encode,
impl<O, T> Encode for BitVec<T, O>where
O: BitOrder,
T: BitStore + Encode,
§fn encode_to<W>(&self, dest: &mut W)where
W: Output + ?Sized,
fn encode_to<W>(&self, dest: &mut W)where
W: Output + ?Sized,
§fn using_encoded<R, F>(&self, f: F) -> R
fn using_encoded<R, F>(&self, f: F) -> R
§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
§impl<'a, T, O> Extend<&'a T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<'a, T, O> Extend<&'a T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl<'a, T, O> Extend<&'a bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<'a, T, O> Extend<&'a bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a bool>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a bool>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl<'a, M, T1, T2, O1, O2> Extend<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where
M: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'a, M, T1, T2, O1, O2> Extend<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where
M: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
§Bit-Vector Extension by Proxy References
DO NOT use this. You clearly have a bit-slice. Use
.extend_from_bitslice()
instead!
Iterating over a bit-slice requires loading from memory and constructing a proxy reference for each bit. This is needlessly slow; the specialized method is able to avoid this per-bit cost and possibly even use batched operations.
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = BitRef<'a, M, T2, O2>>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = BitRef<'a, M, T2, O2>>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl<T, O> Extend<T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> Extend<T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl<T, O> Extend<bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> Extend<bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§Bit-Vector Extension
This extends a bit-vector from anything that produces individual bits.
§Original
§Notes
This .extend()
call is the second-slowest possible way to append bits into a
bit-vector, faster only than calling iter.for_each(|bit| bv.push(bit))
.
DO NOT use this if you have any other choice.
If you are extending a bit-vector from the contents of a bit-slice, then you
should use .extend_from_bitslice()
instead. That method is specialized to
perform upfront allocation and, where possible, use a batch copy rather than
copying each bit individually from the source into the bit-vector.
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = bool>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = bool>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl<A, O> From<BitArray<A, O>> for BitVec<<A as BitView>::Store, O>where
O: BitOrder,
A: BitViewSized,
impl<A, O> From<BitArray<A, O>> for BitVec<<A as BitView>::Store, O>where
O: BitOrder,
A: BitViewSized,
§impl<'a, T, O> FromIterator<&'a T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<'a, T, O> FromIterator<&'a T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§impl<'a, T, O> FromIterator<&'a bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<'a, T, O> FromIterator<&'a bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§impl<'a, M, T1, T2, O1, O2> FromIterator<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where
M: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'a, M, T1, T2, O1, O2> FromIterator<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where
M: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
§Bit-Vector Collection from Proxy References
DO NOT use this. You clearly have a bit-slice. Use
::from_bitslice()
instead!
Iterating over a bit-slice requires loading from memory and constructing a proxy reference for each bit. This is needlessly slow; the specialized method is able to avoid this per-bit cost and possibly even use batched operations.
§fn from_iter<I>(iter: I) -> BitVec<T1, O1> ⓘwhere
I: IntoIterator<Item = BitRef<'a, M, T2, O2>>,
fn from_iter<I>(iter: I) -> BitVec<T1, O1> ⓘwhere
I: IntoIterator<Item = BitRef<'a, M, T2, O2>>,
§impl<T, O> FromIterator<T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> FromIterator<T> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§fn from_iter<I>(iter: I) -> BitVec<T, O> ⓘwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> BitVec<T, O> ⓘwhere
I: IntoIterator<Item = T>,
§impl<T, O> FromIterator<bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> FromIterator<bool> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§Bit-Vector Collection
This collects a bit-vector from anything that produces individual bits.
§Original
impl<T> FromIterator<T> for Vec<T>
§Notes
This .collect()
call is the second-slowest possible way to collect bits into a
bit-vector, faster only than calling iter.for_each(|bit| bv.push(bit))
.
DO NOT use this if you have any other choice.
If you are collecting a bit-vector from the contents of a bit-slice, then you
should use ::from_bitslice()
instead. That method is specialized to
perform upfront allocation and, where possible, use a batch copy rather than
copying each bit individually from the source into the bit-vector.
§impl<T, O, Idx> Index<Idx> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: Index<Idx>,
impl<T, O, Idx> Index<Idx> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: Index<Idx>,
§impl<T, O, Idx> IndexMut<Idx> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: IndexMut<Idx>,
impl<T, O, Idx> IndexMut<Idx> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: IndexMut<Idx>,
§impl<T, O> IntoIterator for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> IntoIterator for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§Bit-Vector Iteration
Bit-vectors have the advantage that iteration consumes the whole structure, so they can simply freeze the allocation into a bit-box, then use its iteration and destructor.
§Original
§type IntoIter = <BitBox<T, O> as IntoIterator>::IntoIter
type IntoIter = <BitBox<T, O> as IntoIterator>::IntoIter
§type Item = <BitBox<T, O> as IntoIterator>::Item
type Item = <BitBox<T, O> as IntoIterator>::Item
§impl<T, O> Not for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> Not for BitVec<T, O>where
T: BitStore,
O: BitOrder,
This implementation inverts all elements in the live buffer. You cannot rely
on the value of bits in the buffer that are outside the domain of
[BitVec::as_mut_bitslice
].
§impl<T, O> Ord for BitVec<T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> Ord for BitVec<T, O>where
T: BitStore,
O: BitOrder,
§impl<T, O, Rhs> PartialOrd<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
Rhs: PartialOrd<BitSlice<T, O>> + ?Sized,
impl<T, O, Rhs> PartialOrd<Rhs> for BitVec<T, O>where
T: BitStore,
O: BitOrder,
Rhs: PartialOrd<BitSlice<T, O>> + ?Sized,
§impl<T, O> Read for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
impl<T, O> Read for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
§Reading From a Bit-Vector
The implementation loads bytes out of the reference bit-vector until either the
destination buffer is filled or the source has no more bytes to provide. When
.read()
returns, the provided bit-vector will have its contents shifted down
so that it begins at the first bit after the last byte copied out into buf
.
Note that the return value of .read()
is always the number of bytes of buf
filled!
§API Differences
The standard library does not impl Read for Vec<u8>
. It is provided here as a
courtesy.
§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more§impl<T, O> Serialize for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: Serialize,
impl<T, O> Serialize for BitVec<T, O>where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: Serialize,
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
§impl<T, O> TypeInfo for BitVec<T, O>where
T: BitStore + TypeInfo + 'static,
O: BitOrder + TypeInfo + 'static,
impl<T, O> TypeInfo for BitVec<T, O>where
T: BitStore + TypeInfo + 'static,
O: BitOrder + TypeInfo + 'static,
§impl<T, O> Write for BitVec<T, O>where
O: BitOrder,
T: BitStore,
BitSlice<T, O>: BitField,
impl<T, O> Write for BitVec<T, O>where
O: BitOrder,
T: BitStore,
BitSlice<T, O>: BitField,
§Writing Into a Bit-Vector
The implementation appends bytes to the referenced bit-vector until the source buffer is exhausted.
Note that the return value of .write()
is always the number of bytes of
buf
consumed!
The implementation uses BitField::store_be
to fill bytes. Note that unlike
the standard library, it is implemented on bit-vectors of any underlying
element type. However, using a BitVec<_, u8>
is still likely to be fastest.
§Original
§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)