Struct bitvec::BitVec [−][src]
A compact Vec
of bits, whose cursor and storage type can be customized.
BitVec
is a newtype wrapper over Vec
, and as such is exactly three words in
size on the stack.
IMPORTANT NOTE: It is wildly unsafe to use mem::transmute
between
Vec<T>
and BitVec<_, T>
, because BitVec
achieves its size by using the
length field of the underlying Vec
to count bits, rather than elements. This
means that it has a fixed maximum bit width regardless of element type, and the
length field will always be horrifically wrong to be treated as a Vec
. Safe
methods exist to move between Vec
and BitVec
– USE THEM.
BitVec
takes two type parameters.
E: Endian
must be an implementor of theEndian
trait.BitVec
takes aPhantomData
marker for access to the associated functions, and will never make use of an instance of the trait. The default implementations,LittleEndian
andBigEndian
, are zero-sized, and any further implementations should be as well, as the invoked functions will never receive state.T: Bits
must be a primitive type. Rust decided long ago to not provide a unifying trait over the primitives, soBits
provides access to just enough properties of the primitives forBitVec
to use. This trait is sealed against downstream implementation, and can only be implemented in this crate.
Methods
impl<E, T> BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> BitVec<E, T> where
E: Endian,
T: Bits,
pub fn new() -> Self
[src]
pub fn new() -> Self
Construct a new, empty, BitVec<E, T>
.
The vector will not allocate until bits are pushed onto it.
Examples
use bitvec::*; let bv: BitVec = BitVec::new(); assert!(bv.is_empty()); assert_eq!(bv.capacity(), 0);
pub fn with_capacity(capacity: usize) -> Self
[src]
pub fn with_capacity(capacity: usize) -> Self
Construct a new, empty BitVec<T>
with the specified capacity.
The vector will be able to hold exactly capacity
elements without
reallocating. If capacity
is 0, the vector will not allocate.
Examples
use bitvec::*; let bv: BitVec = BitVec::with_capacity(10); assert!(bv.is_empty()); assert!(bv.capacity() >= 2);
pub fn capacity(&self) -> usize
[src]
pub fn capacity(&self) -> usize
Return the number of bits the vector can hold without reallocating.
Examples
use bitvec::*; let bv: BitVec = BitVec::with_capacity(10); assert!(bv.is_empty()); assert!(bv.capacity() >= 2);
pub fn push(&mut self, value: bool)
[src]
pub fn push(&mut self, value: bool)
Append a bit to the collection.
Examples
use bitvec::*; let mut bv: BitVec = BitVec::new(); assert!(bv.is_empty()); bv.push(true); assert_eq!(bv.len(), 1); assert!(bv[0]);
pub fn pop(&mut self) -> Option<bool>
[src]
pub fn pop(&mut self) -> Option<bool>
Remove the last bit from the collection.
Returns None
if the collection is empty.
Examples
use bitvec::*; let mut bv: BitVec = BitVec::new(); assert!(bv.is_empty()); bv.push(true); assert_eq!(bv.len(), 1); assert!(bv[0]); assert!(bv.pop().unwrap()); assert!(bv.is_empty()); assert!(bv.pop().is_none());
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
Empty out the BitVec
, resetting it to length zero.
This does not affect the memory store! It will not zero the raw memory nor will it deallocate.
Examples
use bitvec::*; let mut bv = bitvec![1; 30]; assert_eq!(bv.len(), 30); assert!(bv.iter().all(|b| b)); bv.clear(); assert!(bv.is_empty());
After clear()
, bv
will no longer show raw memory, so the above test
cannot show that the underlying memory is untouched. This is also an
implementation detail on which you should not rely.
pub fn reserve(&mut self, additional: usize)
[src]
pub fn reserve(&mut self, additional: usize)
Reserve capacity for additional bits.
Examples
use bitvec::*; let mut bv = bitvec![1; 5]; let cap = bv.capacity(); bv.reserve(10); assert!(bv.capacity() >= cap + 10);
pub fn shrink_to_fit(&mut self)
[src]
pub fn shrink_to_fit(&mut self)
Shrink the capacity to fit at least as much as is needed, but with as little or as much excess as the allocator chooses.
This may or may not deallocate tail space, as the allocator sees fit. This does not zero the abandoned memory.
pub fn truncate(&mut self, len: usize)
[src]
pub fn truncate(&mut self, len: usize)
Shrinks the BitVec
to the given size, dropping all excess storage.
This does not affect the memory store! It will not zero the raw memory nor will it deallocate.
Examples
use bitvec::*; let mut bv = bitvec![1; 30]; assert_eq!(bv.len(), 30); let cap = bv.capacity(); bv.truncate(10); assert_eq!(bv.len(), 10); assert_eq!(bv.capacity(), cap);
pub fn into_boxed_slice(self) -> Box<[T]>
[src]
pub fn into_boxed_slice(self) -> Box<[T]>
Convert the BitVec
into a boxed slice of storage elements. This drops
all BitVec
management semantics, including partial fill status of the
trailing element or endianness, and gives ownership the raw storage.
Examples
use bitvec::*; let bv: BitVec<BigEndian, u8> = bitvec![1; 64]; let bytes: Box<[u8]> = bv.into_boxed_slice(); assert_eq!(bytes.len(), 8); for byte in bytes.iter() { assert_eq!(*byte, !0); }
Methods from Deref<Target = BitSlice<E, T>>
pub fn get(&self, index: usize) -> bool
[src]
pub fn get(&self, index: usize) -> bool
Gets the bit value at the given position.
The index value is a semantic count, not a bit address. It converts to a bit position internally to this function.
Examples
use bitvec::*; let bv = bitvec![0, 0, 1, 0, 0]; let bits: &BitSlice = &bv; assert!(bits.get(2));
pub fn set(&mut self, index: usize, value: bool)
[src]
pub fn set(&mut self, index: usize, value: bool)
Sets the bit value at the given position.
The index value is a semantic count, not a bit address. It converts to a bit position internally to this function.
Examples
use bitvec::*; let mut bv = bitvec![0; 5]; let bits: &mut BitSlice = &mut bv; bits.set(2, true); assert!(bits.get(2));
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
Returns the number of bits contained in the BitSlice
.
Examples
use bitvec::*; let bv = bitvec![1; 10]; let bits: &BitSlice = &bv; assert_eq!(bits.len(), 10);
pub fn elts(&self) -> usize
[src]
pub fn elts(&self) -> usize
Counts how many whole storage elements are in the BitSlice
.
If the BitSlice
length is not an even multiple of the width of T
,
then the slice under this BitSlice
is one element longer than this
method reports, and the number of bits in it are reported by bits()
.
Examples
use bitvec::*; let bv = bitvec![1; 10]; let bits: &BitSlice = &bv; assert_eq!(bits.elts(), 1);
use bitvec::*; let bv = bitvec![1; 16]; let bits: &BitSlice = &bv; assert_eq!(bits.elts(), 2);
pub fn bits(&self) -> u8
[src]
pub fn bits(&self) -> u8
Counts how many bits are in the trailing partial storage element.
If the BitSlice
length is an even multiple of the width of T
, then
this returns 0 and the BitSlice
does not consider its final element to
be partially used.
Examples
use bitvec::*; let bv = bitvec![1; 10]; let bits: &BitSlice = &bv; assert_eq!(bits.bits(), 2);
use bitvec::*; let bv = bitvec![1; 16]; let bits: &BitSlice = &bv; assert_eq!(bits.bits(), 0);
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
Returns true
if the slice contains no bits.
Examples
use bitvec::*; let bv = bitvec![]; let bits: &BitSlice = &bv; assert!(bits.is_empty());
use bitvec::*; let bv = bitvec![0; 5]; let bits: &BitSlice = &bv; assert!(!bits.is_empty());
pub fn iter<'a>(&'a self) -> Iter<'a, E, T>
[src]
pub fn iter<'a>(&'a self) -> Iter<'a, E, T>
Provide read-only iteration across the collection.
The iterator returned from this method implements ExactSizeIterator
and DoubleEndedIterator
just as the consuming .into_iter()
method’s
iterator does.
pub fn for_each<'a, F>(&'a mut self, op: F) where
F: Fn(usize, bool) -> bool,
[src]
pub fn for_each<'a, F>(&'a mut self, op: F) where
F: Fn(usize, bool) -> bool,
Provide mutable traversal of the collection.
It is impossible to implement IndexMut
on BitSlice
because bits do
not have addresses, so there can be no &mut u1
. This method allows the
client to receive an enumerated bit, and provide a new bit to set at
each index.
Examples
use bitvec::*; let mut bv = bitvec![1; 8]; let bref: &mut BitSlice = &mut bv; bref.for_each(|idx, bit| { if idx % 2 == 0 { !bit } else { bit } }); assert_eq!(&[0b01010101], bref.as_ref());
Trait Implementations
impl<E, T> AsMut<[T]> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> AsMut<[T]> for BitVec<E, T> where
E: Endian,
T: Bits,
Give write access to all live elements in the underlying storage, including the partially-filled tail.
fn as_mut(&mut self) -> &mut [T]
[src]
fn as_mut(&mut self) -> &mut [T]
Access the underlying store.
Examples
use bitvec::*; let mut bv: BitVec = bitvec![0, 0, 0, 0, 0, 0, 0, 0, 1]; for elt in bv.as_mut() { *elt += 2; } assert_eq!(&[2, 0b1000_0010], bv.as_ref());
impl<E, T> AsRef<[T]> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> AsRef<[T]> for BitVec<E, T> where
E: Endian,
T: Bits,
Give read access to all live elements in the underlying storage, including the partially-filled tail.
fn as_ref(&self) -> &[T]
[src]
fn as_ref(&self) -> &[T]
Access the underlying store.
Examples
use bitvec::*; let bv = bitvec![0, 0, 0, 0, 0, 0, 0, 0, 1]; assert_eq!(&[0, 0b1000_0000], bv.as_ref());
impl<E, T, I> BitAnd<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
impl<E, T, I> BitAnd<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
Perform the Boolean AND operation between each element of a BitVec
and
anything that can provide a stream of bool
values (such as another
BitVec
, or any bool
generator of your choice). The BitVec
emitted will
have the length of the shorter sequence of bits -- if one is longer than the
other, the extra bits will be ignored.
type Output = Self
The resulting type after applying the &
operator.
fn bitand(self, rhs: I) -> Self::Output
[src]
fn bitand(self, rhs: I) -> Self::Output
AND a vector and a bitstream, producing a new vector.
Examples
use bitvec::*; let lhs = bitvec![BigEndian, u8; 0, 1, 0, 1]; let rhs = bitvec![BigEndian, u8; 0, 0, 1, 1]; let and = lhs & rhs; assert_eq!("0001", &format!("{}", and));
impl<E, T, I> BitAndAssign<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
impl<E, T, I> BitAndAssign<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
Perform the Boolean AND operation in place on a BitVec
, using a stream of
bool
values as the other bit for each operation. If the other stream is
shorter than self
, self
will be truncated when the other stream expires.
fn bitand_assign(&mut self, rhs: I)
[src]
fn bitand_assign(&mut self, rhs: I)
AND another bitstream into a vector.
Examples
use bitvec::*; let mut src = bitvec![BigEndian, u8; 0, 1, 0, 1]; src &= bitvec![BigEndian, u8; 0, 0, 1, 1]; assert_eq!("0001", &format!("{}", src));
impl<E, T, I> BitOr<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
impl<E, T, I> BitOr<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
Perform the Boolean OR operation between each element of a BitVec
and
anything that can provide a stream of bool
values (such as another
BitVec
, or any bool
generator of your choice). The BitVec
emitted will
have the length of the shorter sequence of bits -- if one is longer than the
other, the extra bits will be ignored.
type Output = Self
The resulting type after applying the |
operator.
fn bitor(self, rhs: I) -> Self::Output
[src]
fn bitor(self, rhs: I) -> Self::Output
OR a vector and a bitstream, producing a new vector.
Examples
use bitvec::*; let lhs = bitvec![BigEndian, u8; 0, 1, 0, 1]; let rhs = bitvec![BigEndian, u8; 0, 0, 1, 1]; let or = lhs | rhs; assert_eq!("0111", &format!("{}", or));
impl<E, T, I> BitOrAssign<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
impl<E, T, I> BitOrAssign<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
Perform the Boolean OR operation in place on a BitVec
, using a stream of
bool
values as the other bit for each operation. If the other stream is
shorter than self
, self
will be truncated when the other stream expires.
fn bitor_assign(&mut self, rhs: I)
[src]
fn bitor_assign(&mut self, rhs: I)
OR another bitstream into a vector.
Examples
use bitvec::*; let mut src = bitvec![BigEndian, u8; 0, 1, 0, 1]; src |= bitvec![BigEndian, u8; 0, 0, 1, 1]; assert_eq!("0111", &format!("{}", src));
impl<E, T, I> BitXor<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
impl<E, T, I> BitXor<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
Perform the Boolean XOR operation between each element of a BitVec
and
anything that can provide a stream of bool
values (such as another
BitVec
, or any bool
generator of your choice). The BitVec
emitted will
have the length of the shorter sequence of bits -- if one is longer than the
other, the extra bits will be ignored.
type Output = Self
The resulting type after applying the ^
operator.
fn bitxor(self, rhs: I) -> Self::Output
[src]
fn bitxor(self, rhs: I) -> Self::Output
XOR a vector and a bitstream, producing a new vector.
Examples
use bitvec::*; let lhs = bitvec![BigEndian, u8; 0, 1, 0, 1]; let rhs = bitvec![BigEndian, u8; 0, 0, 1, 1]; let xor = lhs ^ rhs; assert_eq!("0110", &format!("{}", xor));
impl<E, T, I> BitXorAssign<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
impl<E, T, I> BitXorAssign<I> for BitVec<E, T> where
E: Endian,
T: Bits,
I: IntoIterator<Item = bool>,
Perform the Boolean XOR operation in place on a BitVec
, using a stream of
bool
values as the other bit for each operation. If the other stream is
shorter than self
, self
will be truncated when the other stream expires.
fn bitxor_assign(&mut self, rhs: I)
[src]
fn bitxor_assign(&mut self, rhs: I)
XOR another bitstream into a vector.
Examples
use bitvec::*; let mut src = bitvec![BigEndian, u8; 0, 1, 0, 1]; src ^= bitvec![BigEndian, u8; 0, 0, 1, 1]; assert_eq!("0110", &format!("{}", src));
impl<E, T> Borrow<BitSlice<E, T>> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Borrow<BitSlice<E, T>> for BitVec<E, T> where
E: Endian,
T: Bits,
Signify that BitSlice
is the borrowed form of BitVec
.
fn borrow(&self) -> &BitSlice<E, T>
[src]
fn borrow(&self) -> &BitSlice<E, T>
Borrow the BitVec
as a BitSlice
.
Examples
use bitvec::*; use std::borrow::Borrow; let bv = bitvec![0; 8]; let bref: &BitSlice = bv.borrow(); assert!(!bref.get(7));
impl<E, T> BorrowMut<BitSlice<E, T>> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> BorrowMut<BitSlice<E, T>> for BitVec<E, T> where
E: Endian,
T: Bits,
Signify that BitSlice
is the borrowed form of BitVec
.
fn borrow_mut(&mut self) -> &mut BitSlice<E, T>
[src]
fn borrow_mut(&mut self) -> &mut BitSlice<E, T>
Mutably borow the BitVec
as a BitSlice
.
Examples
use bitvec::*; use std::borrow::BorrowMut; let mut bv = bitvec![0; 8]; let bref: &mut BitSlice = bv.borrow_mut(); assert!(!bref.get(7)); bref.set(7, true); assert!(bref.get(7));
impl<E, T> Clone for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Clone for BitVec<E, T> where
E: Endian,
T: Bits,
fn clone(&self) -> Self
[src]
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, other: &Self)
[src]
fn clone_from(&mut self, other: &Self)
Performs copy-assignment from source
. Read more
impl<E, T> Debug for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Debug for BitVec<E, T> where
E: Endian,
T: Bits,
Print the BitVec
for debugging.
The output is of the form BitVec<E, T> [ELT, *]
, where <E, T>
is the
endianness and element type, with square brackets on each end of the bits
and all the live elements in the vector printed in binary. The printout is
always in semantic order, and may not reflect the underlying store. To see
the underlying store, use format!("{:?}", self.as_ref());
instead.
The alternate character {:#?}
prints each element on its own line, rather
than separated by a space.
fn fmt(&self, fmt: &mut Formatter) -> Result
[src]
fn fmt(&self, fmt: &mut Formatter) -> Result
Render the BitVec
type header and contents for debug.
Examples
use bitvec::*; let bv = bitvec![LittleEndian, u16; 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1 ]; assert_eq!( "BitVec<LittleEndian, u16> [0101000011110101]", &format!("{:?}", bv) );
impl<E, T> Deref for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Deref for BitVec<E, T> where
E: Endian,
T: Bits,
Reborrow the BitVec
as a BitSlice
.
This mimics the separation between Vec<T>
and [T]
.
type Target = BitSlice<E, T>
The resulting type after dereferencing.
fn deref(&self) -> &Self::Target
[src]
fn deref(&self) -> &Self::Target
Dereference &BitVec
down to &BitSlice
.
Examples
use bitvec::*; let bv: BitVec = bitvec![1; 4]; let bref: &BitSlice = &bv; assert!(bref.get(2));
impl<E, T> DerefMut for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> DerefMut for BitVec<E, T> where
E: Endian,
T: Bits,
Reborrow the BitVec
as a BitSlice
.
This mimics the separation between Vec<T>
and [T]
.
fn deref_mut(&mut self) -> &mut Self::Target
[src]
fn deref_mut(&mut self) -> &mut Self::Target
Dereference &mut BitVec
down to &mut BitSlice
.
Examples
use bitvec::*; let mut bv: BitVec = bitvec![0; 6]; let bref: &mut BitSlice = &mut bv; assert!(!bref.get(5)); bref.set(5, true); assert!(bref.get(5));
impl<E, T> Display for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Display for BitVec<E, T> where
E: Endian,
T: Bits,
Print the BitVec
for displaying.
This prints each element in turn, formatted in binary in semantic order (so the first bit seen is printed first and the last bit seen printed last). Each element of storage is separated by a space for ease of reading.
The alternate character {:#}
prints each element on its own line.
To see the in-memory representation, use AsRef
to get access to the raw
elements and print that slice instead.
fn fmt(&self, fmt: &mut Formatter) -> Result
[src]
fn fmt(&self, fmt: &mut Formatter) -> Result
Render the BitVec
contents for display.
Examples
use bitvec::*; let bv = bitvec![BigEndian, u8; 0, 1, 0, 0, 1, 0, 1, 1, 0, 1]; assert_eq!("01001011 01", &format!("{}", bv));
impl<E, T> Drop for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Drop for BitVec<E, T> where
E: Endian,
T: Bits,
Ready the underlying storage for Drop.
impl<E, T> Extend<bool> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Extend<bool> for BitVec<E, T> where
E: Endian,
T: Bits,
Extend a BitVec
with the contents of another bitstream.
At present, this just calls .push()
in a loop. When specialization becomes
available, it will be able to more intelligently perform bulk moves from the
source into self
when the source is BitSlice
-compatible.
fn extend<I>(&mut self, src: I) where
I: IntoIterator<Item = bool>,
[src]
fn extend<I>(&mut self, src: I) where
I: IntoIterator<Item = bool>,
Extend a BitVec
from another bitstream.
Examples
use bitvec::*; let mut bv = bitvec![0; 4]; bv.extend(bitvec![1; 4]); assert_eq!("00001111", &format!("{}", bv));
impl<'a, E, T> From<&'a BitSlice<E, T>> for BitVec<E, T> where
E: Endian,
T: 'a + Bits,
[src]
impl<'a, E, T> From<&'a BitSlice<E, T>> for BitVec<E, T> where
E: Endian,
T: 'a + Bits,
Clone a BitSlice
into an owned BitVec
.
The idiomatic BitSlice
to BitVec
conversion is BitSlice::to_owned
, but
just as &[T].into()
yields a Vec
, &BitSlice.into()
yields a BitVec
.
impl<'a, E, T> From<&'a [bool]> for BitVec<E, T> where
E: Endian,
T: 'a + Bits,
[src]
impl<'a, E, T> From<&'a [bool]> for BitVec<E, T> where
E: Endian,
T: 'a + Bits,
Build a BitVec
out of a slice of bool
.
This is primarily for the bitvec!
macro; it is not recommended for general
use.
impl<'a, E, T> From<&'a [T]> for BitVec<E, T> where
E: Endian,
T: 'a + Bits,
[src]
impl<'a, E, T> From<&'a [T]> for BitVec<E, T> where
E: Endian,
T: 'a + Bits,
Build a BitVec
out of a borrowed slice of elements.
This copies the memory as-is from the source buffer into the new BitVec
.
The source buffer will be unchanged by this operation, so you don't need to
worry about using the correct cursor type for the read.
This operation does a copy from the source buffer into a new allocation, as it can only borrow the source and not take ownership.
fn from(src: &'a [T]) -> Self
[src]
fn from(src: &'a [T]) -> Self
Build a BitVec<E: Endian, T: Bits>
from a borrowed &[T]
.
Examples
use bitvec::*; let src: &[u8] = &[5, 10]; let bv: BitVec = src.into(); assert_eq!("00000101 00001010", &format!("{}", bv));
impl<E, T> From<Box<[T]>> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> From<Box<[T]>> for BitVec<E, T> where
E: Endian,
T: Bits,
Build a BitVec
out of an owned slice of elements.
This moves the memory as-is from the source buffer into the new BitVec
.
The source buffer will be unchanged by this operation, so you don't need to
worry about using the correct cursor type.
fn from(src: Box<[T]>) -> Self
[src]
fn from(src: Box<[T]>) -> Self
Consume a Box<[T: Bits]>
and creates a BitVec<E: Endian, T>
from it.
Examples
use bitvec::*; let src: Box<[u8]> = Box::new([3, 6, 9, 12, 15]); let bv: BitVec = src.into(); assert_eq!("00000011 00000110 00001001 00001100 00001111", &format!("{}", bv));
impl<E, T> From<Vec<T>> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> From<Vec<T>> for BitVec<E, T> where
E: Endian,
T: Bits,
Build a BitVec
out of a Vec
of elements.
This moves the memory as-is from the source buffer into the new BitVec
.
The source buffer will be unchanged by this operation, so you don't need to
worry about using the correct cursor type.
fn from(src: Vec<T>) -> Self
[src]
fn from(src: Vec<T>) -> Self
Consume a Vec<T: Bits>
and creates a BitVec<E: Endian, T>
from it.
Examples
use bitvec::*; let src: Vec<u8> = vec![1, 2, 4, 8]; let bv: BitVec = src.into(); assert_eq!("00000001 00000010 00000100 00001000", &format!("{}", bv));
impl<T: Bits> From<BitVec<LittleEndian, T>> for BitVec<BigEndian, T>
[src]
impl<T: Bits> From<BitVec<LittleEndian, T>> for BitVec<BigEndian, T>
Change cursors on a BitVec
without mutating the underlying data.
I don't know why this would be useful at the time of writing, as the From
implementations on collections crawl the collection elements in the order
requested and so the source and destination storage collections will be
bitwise identical, but here's the option anyway.
If the tail element is partially filled, then this operation will shift the tail element so that the edge of the filled section is on the correct edge of the tail element.
fn from(src: BitVec<LittleEndian, T>) -> Self
[src]
fn from(src: BitVec<LittleEndian, T>) -> Self
Performs the conversion.
impl<T: Bits> From<BitVec<BigEndian, T>> for BitVec<LittleEndian, T>
[src]
impl<T: Bits> From<BitVec<BigEndian, T>> for BitVec<LittleEndian, T>
Change cursors on a BitVec
without mutating the underlying data.
I don't know why this would be useful at the time of writing, as the From
implementations on collections crawl the collection elements in the order
requested and so the source and destination storage collections will be
bitwise identical, but here's the option anyway.
If the tail element is partially filled, then this operation will shift the tail element so that the edge of the filled section is on the correct edge of the tail element.
impl<E, T> FromIterator<bool> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> FromIterator<bool> for BitVec<E, T> where
E: Endian,
T: Bits,
Permit the construction of a BitVec
by using .collect()
on an iterator
of bool
.
fn from_iter<I: IntoIterator<Item = bool>>(src: I) -> Self
[src]
fn from_iter<I: IntoIterator<Item = bool>>(src: I) -> Self
Collect an iterator of bool
into a vector.
Examples
use bitvec::*; use std::iter::repeat; let bv: BitVec = repeat(true).take(4).chain(repeat(false).take(4)).collect(); assert_eq!("11110000", &format!("{}", bv));
impl<E, T> Index<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Index<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
Get the bit at a specific index. The index must be less than the length of
the BitVec
.
type Output = bool
The returned type after indexing.
fn index(&self, cursor: usize) -> &Self::Output
[src]
fn index(&self, cursor: usize) -> &Self::Output
Look up a single bit by semantic count.
Examples
use bitvec::*; let bv = bitvec![BigEndian, u8; 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]; assert!(!bv[7]); // ---------------------------------^ | | assert!( bv[8]); //-------------------------------------^ | assert!(!bv[9]); // ---------------------------------------^
If the index is greater than or equal to the length, indexing will panic.
The below test will panic when accessing index 1, as only index 0 is valid.
use bitvec::*; let mut bv: BitVec = BitVec::new(); bv.push(true); bv[1];
impl<E, T> Index<(usize, u8)> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Index<(usize, u8)> for BitVec<E, T> where
E: Endian,
T: Bits,
Get the bit in a specific element. The element index must be less than or
equal to the value returned by elts()
, and the bit index must be less
than the width of the storage type.
If the BitVec
has a partially-filled tail, then the value returned by
elts()
is a valid index.
The element and bit indices are combined using Bits::join
for the storage
type.
This index is not recommended for public use.
type Output = bool
The returned type after indexing.
fn index(&self, (elt, bit): (usize, u8)) -> &Self::Output
[src]
fn index(&self, (elt, bit): (usize, u8)) -> &Self::Output
Index into a BitVec
using a known element index and a count into that
element. The count must not be converted for endianness outside the
call.
Examples
use bitvec::*; let bv = bitvec![BigEndian, u8; 1, 1, 1, 1, 0, 0, 0, 0, 0, 1]; assert!(bv[(1, 1)]); // -----------------------------------^
impl<E, T> IntoIterator for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> IntoIterator for BitVec<E, T> where
E: Endian,
T: Bits,
Produce an iterator over all the bits in the vector.
This iterator follows the ordering in the vector type, and implements
ExactSizeIterator
, since BitVec
s always know exactly how large they are,
and DoubleEndedIterator
, since they have known ends.
type Item = bool
The type of the elements being iterated over.
fn into_iter(self) -> Self::IntoIter
[src]
fn into_iter(self) -> Self::IntoIter
Iterate over the vector.
Examples
use bitvec::*; let bv = bitvec![BigEndian, u8; 1, 1, 1, 1, 0, 0, 0, 0]; let mut count = 0; for bit in bv { if bit { count += 1; } } assert_eq!(count, 4);
type IntoIter: Iterator
Which kind of iterator are we turning this into?
impl<E, T> Not for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Not for BitVec<E, T> where
E: Endian,
T: Bits,
Flip all bits in the vector.
This invokes the !
operator on each element of the borrowed storage, and
so it will also flip bits in the tail that are outside the BitVec
length
if any. Use ^= repeat(true)
to flip only the bits actually inside the
BitVec
purview. ^=
also has the advantage of being a borrowing operator
rather than a consuming/returning operator.
type Output = Self
The resulting type after applying the !
operator.
fn not(self) -> Self::Output
[src]
fn not(self) -> Self::Output
Invert all bits in the vector.
Examples
use bitvec::*; let bv: BitVec<BigEndian, u32> = BitVec::from(&[0u32] as &[u32]); let flip = !bv; assert_eq!(!0u32, flip.as_ref()[0]);
impl<A, B, C, D> PartialEq<BitVec<C, D>> for BitVec<A, B> where
A: Endian,
B: Bits,
C: Endian,
D: Bits,
[src]
impl<A, B, C, D> PartialEq<BitVec<C, D>> for BitVec<A, B> where
A: Endian,
B: Bits,
C: Endian,
D: Bits,
Test if two BitVec
s are semantically — not bitwise — equal.
It is valid to compare two vectors of different endianness or element types.
The equality condition requires that they have the same number of stored bits and that each pair of bits in semantic order are identical.
fn eq(&self, rhs: &BitVec<C, D>) -> bool
[src]
fn eq(&self, rhs: &BitVec<C, D>) -> bool
Perform a comparison by ==
.
Examples
use bitvec::*; let l: BitVec<LittleEndian, u16> = bitvec![LittleEndian, u16; 0, 1, 0, 1]; let r: BitVec<BigEndian, u32> = bitvec![BigEndian, u32; 0, 1, 0, 1]; assert!(l == r);
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl<E, T> Eq for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Eq for BitVec<E, T> where
E: Endian,
T: Bits,
impl<A, B, C, D> PartialOrd<BitVec<C, D>> for BitVec<A, B> where
A: Endian,
B: Bits,
C: Endian,
D: Bits,
[src]
impl<A, B, C, D> PartialOrd<BitVec<C, D>> for BitVec<A, B> where
A: Endian,
B: Bits,
C: Endian,
D: Bits,
Compare two BitVec
s by semantic — not bitwise — ordering.
The comparison sorts by testing each index for one vector to have a set bit where the other vector has an unset bit. If the vectors are different, the vector with the set bit sorts greater than the vector with the unset bit.
If one of the vectors is exhausted before they differ, the longer vector is greater.
fn partial_cmp(&self, rhs: &BitVec<C, D>) -> Option<Ordering>
[src]
fn partial_cmp(&self, rhs: &BitVec<C, D>) -> Option<Ordering>
Perform a comparison by <
or >
.
Examples
use bitvec::*; use bitvec::*; let a = bitvec![0, 1, 0, 0]; let b = bitvec![0, 1, 0, 1]; let c = bitvec![0, 1, 0, 1, 1]; assert!(a < b); assert!(b < c);
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
fn gt(&self, other: &Rhs) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
fn ge(&self, other: &Rhs) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<E, T> Ord for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Ord for BitVec<E, T> where
E: Endian,
T: Bits,
fn cmp(&self, rhs: &Self) -> Ordering
[src]
fn cmp(&self, rhs: &Self) -> Ordering
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
impl<E, T> Shl<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Shl<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
Shift all bits in the vector to the left – DOWN AND TOWARDS THE FRONT.
On primitives, the left-shift operator <<
moves bits away from origin and
towards the ceiling. This is because we label the bits in a primitive with
the minimum on the right and the maximum on the left, which is big-endian
bit order. This increases the value of the primitive being shifted.
THAT IS NOT HOW BITVEC
WORKS!
BitVec
defines its layout with the minimum on the left and the maximum on
the right! Thus, left-shifting moves bits towards the minimum.
In BigEndian order, the effect in memory will be what you expect the <<
operator to do.
In LittleEndian order, the effect will be equivalent to using >>
on
the primitives in memory!
Notes
In order to preserve the effects in memory that this operator traditionally expects, the bits that are emptied by this operation are zeroed rather than left to their old value.
The length of the vector is decreased by the shift amount.
If the shift amount is greater than the length, the vector calls clear()
and zeroes its memory. This is not an error.
type Output = Self
The resulting type after applying the <<
operator.
fn shl(self, shamt: usize) -> Self::Output
[src]
fn shl(self, shamt: usize) -> Self::Output
Shift a BitVec
to the left, shortening it.
Examples
use bitvec::*; let bv = bitvec![BigEndian, u8; 0, 0, 0, 1, 1, 1]; assert_eq!("000111", &format!("{}", bv)); assert_eq!(0b0001_1100, bv.as_ref()[0]); assert_eq!(bv.len(), 6); let ls = bv << 2usize; assert_eq!("0111", &format!("{}", ls)); assert_eq!(0b0111_0000, ls.as_ref()[0]); assert_eq!(ls.len(), 4);
impl<E, T> ShlAssign<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> ShlAssign<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
Shift all bits in the vector to the left – DOWN AND TOWARDS THE FRONT.
On primitives, the left-shift operator <<
moves bits away from origin and
towards the ceiling. This is because we label the bits in a primitive with
the minimum on the right and the maximum on the left, which is big-endian
bit order. This increases the value of the primitive being shifted.
THAT IS NOT HOW BITVEC
WORKS!
BitVec
defines its layout with the minimum on the left and the maximum on
the right! Thus, left-shifting moves bits towards the minimum.
In BigEndian order, the effect in memory will be what you expect the <<
operator to do.
In LittleEndian order, the effect will be equivalent to using >>
on
the primitives in memory!
Notes
In order to preserve the effects in memory that this operator traditionally expects, the bits that are emptied by this operation are zeroed rather than left to their old value.
The length of the vector is decreased by the shift amount.
If the shift amount is greater than the length, the vector calls clear()
and zeroes its memory. This is not an error.
fn shl_assign(&mut self, shamt: usize)
[src]
fn shl_assign(&mut self, shamt: usize)
Shift a BitVec
to the left in place, shortening it.
Examples
use bitvec::*; let mut bv = bitvec![LittleEndian, u8; 0, 0, 0, 1, 1, 1]; assert_eq!("000111", &format!("{}", bv)); assert_eq!(0b0011_1000, bv.as_ref()[0]); assert_eq!(bv.len(), 6); bv <<= 2; assert_eq!("0111", &format!("{}", bv)); assert_eq!(0b0000_1110, bv.as_ref()[0]); assert_eq!(bv.len(), 4);
impl<E, T> Shr<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> Shr<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
Shift all bits in the vector to the right – UP AND TOWARDS THE BACK.
On primitives, the right-shift operator >>
moves bits towards the origin
and away from the ceiling. This is because we label the bits in a primitive
with the minimum on the right and the maximum on the left, which is
big-endian bit order. This decreases the value of the primitive being
shifted.
THAT IS NOT HOW BITVEC
WORKS!
BitVec
defines its layout with the minimum on the left and the maximum on
the right! Thus, right-shifting moves bits towards the maximum.
In BigEndian order, the effect in memory will be what you expect the >>
operator to do.
In LittleEndian order, the effect will be equivalent to using <<
on
the primitives in memory!
Notes
In order to preserve the effects in memory that this operator traditionally expects, the bits that are emptied by this operation are zeroed rather than left to their old value.
The length of the vector is increased by the shift amount.
If the new length of the vector would overflow, a panic occurs. This is an error.
type Output = Self
The resulting type after applying the >>
operator.
fn shr(self, shamt: usize) -> Self::Output
[src]
fn shr(self, shamt: usize) -> Self::Output
Shift a BitVec
to the right, lengthening it and filling the front with 0.
Examples
use bitvec::*; let bv = bitvec![BigEndian, u8; 0, 0, 0, 1, 1, 1]; assert_eq!("000111", &format!("{}", bv)); assert_eq!(0b0001_1100, bv.as_ref()[0]); assert_eq!(bv.len(), 6); let rs = bv >> 2usize; assert_eq!("00000111", &format!("{}", rs)); assert_eq!(0b0000_0111, rs.as_ref()[0]); assert_eq!(rs.len(), 8);
impl<E, T> ShrAssign<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
[src]
impl<E, T> ShrAssign<usize> for BitVec<E, T> where
E: Endian,
T: Bits,
Shift all bits in the vector to the right – UP AND TOWARDS THE BACK.
On primitives, the right-shift operator >>
moves bits towards the origin
and away from the ceiling. This is because we label the bits in a primitive
with the minimum on the right and the maximum on the left, which is
big-endian bit order. This decreases the value of the primitive being
shifted.
THAT IS NOT HOW BITVEC
WORKS!
BitVec
defines its layout with the minimum on the left and the maximum on
the right! Thus, right-shifting moves bits towards the maximum.
In BigEndian order, the effect in memory will be what you expect the >>
operator to do.
In LittleEndian order, the effect will be equivalent to using <<
on
the primitives in memory!
Notes
In order to preserve the effects in memory that this operator traditionally expects, the bits that are emptied by this operation are zeroed rather than left to their old value.
The length of the vector is increased by the shift amount.
If the new length of the vector would overflow, a panic occurs. This is an error.
fn shr_assign(&mut self, shamt: usize)
[src]
fn shr_assign(&mut self, shamt: usize)
Shift a BitVec
to the right in place, lengthening it and filling the
front with 0.
Examples
use bitvec::*; let mut bv = bitvec![LittleEndian, u8; 0, 0, 0, 1, 1, 1]; assert_eq!("000111", &format!("{}", bv)); assert_eq!(0b0011_1000, bv.as_ref()[0]); assert_eq!(bv.len(), 6); bv >>= 2; assert_eq!("00000111", &format!("{}", bv)); assert_eq!(0b1110_0000, bv.as_ref()[0]); assert_eq!(bv.len(), 8);