Struct deepmesa_collections::bitvec::bitslice::BitSlice
source · pub struct BitSlice(/* private fields */);
Expand description
A slice of bits backed by a BitVector
.
The BitSlice
is an unsized type and is a view into a range
within a BitVector
. A BitVector
can
produce mutable or immutable slices which in turn can be
subsliced.
The BitSlice
is a wrapper around [u8]
and borrows memory via
reference types &BitSlice
and &mut BitSlice
. The memory in a
BitSlice
is owned and managed by the underlying
BitVector
.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1011_0011, None);
bv.push_u8(0b1011_0011, None);
let slice = &bv[0..16];
assert_eq!(slice.len(), 16);
assert_eq!(slice[0], true);
assert_eq!(slice[1], false);
let slice_mut = &mut bv[9..11];
assert_eq!(slice_mut[0], false);
slice_mut.set(0, true);
assert_eq!(slice_mut[0], true);
Implementations§
source§impl BitSlice
impl BitSlice
pub fn is_empty(&self) -> bool
pub fn repeat(&self, n: usize) -> BitVector
pub fn set_all(&mut self, value: bool)
sourcepub fn fill(&mut self, bit: bool)
pub fn fill(&mut self, bit: bool)
Fills the slice with the specified bit.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u8(0b1000_0001, None);
let s = &mut bv[1..7];
s.fill(true);
assert_eq!(bv.read_u8(0), (0b1111_1111, 8));
sourcepub fn get(&self, index: usize) -> Option<bool>
pub fn get(&self, index: usize) -> Option<bool>
Returns a boolean value indicating whether the bit at the
specified index is set or None
if the index is greater than
or equal to the number of bits in the slice.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u8(0b1010_0011, None);
let s = &bv[1..7];
assert_eq!(s.get(0), Some(false));
assert_eq!(s.get(1), Some(true));
assert_eq!(s.get(7), None);
sourcepub fn get_mut(&mut self, index: usize) -> Option<BitRefMut<'_, bool>>
pub fn get_mut(&mut self, index: usize) -> Option<BitRefMut<'_, bool>>
Returns a mutable reference to the bit at the specified index
or None
if the index is greater than or equal to the number
of bits in the slice.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1011_1100, None);
assert_eq!(bv[0], true);
let s = &mut bv[0..7];
*s.get_mut(0).unwrap() = false;
assert_eq!(bv[0], false);
sourcepub fn any(&self) -> bool
pub fn any(&self) -> bool
Returns true if any bit in the slice is set to 1
and false
otherwise.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1011_0000, None);
let s = &bv[0..4];
assert_eq!(s.any(), true);
let s = &bv[4..8];
assert_eq!(s.any(), false);
sourcepub fn all(&self) -> bool
pub fn all(&self) -> bool
Returns true if all the bits in the slice are set to 1
and
false otherwise.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1011_1111, None);
let s = &bv[0..4];
assert_eq!(s.all(), false);
let s = &bv[4..8];
assert_eq!(s.all(), true);
sourcepub fn set(&mut self, index: usize, value: bool)
pub fn set(&mut self, index: usize, value: bool)
Sets the bit at the specified index with the given bit
value
.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1011_1111, None);
assert_eq!(bv.get(1), Some(false));
let s = &mut bv[0..4];
s.set(1, true);
assert_eq!(bv.get(1), Some(true));
sourcepub fn leading_ones(&self) -> usize
pub fn leading_ones(&self) -> usize
Counts the number of bits from the start of the bitslice to
the first bit set to 0
.
Returns 0
if the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1111_1000, Some(8));
bv.push_u8(0b0011_1101, Some(8));
let s = &bv[2..2];
assert_eq!(s.leading_ones(), 0);
let s = &bv[0..4];
assert_eq!(s.leading_ones(), 4);
let s = &bv[8..];
assert_eq!(s.leading_ones(), 0);
let s = &bv[10..];
assert_eq!(s.leading_ones(), 4);
sourcepub fn leading_zeros(&self) -> usize
pub fn leading_zeros(&self) -> usize
Counts the number of bits from the start of the bitslice to
the first bit set to 1
.
Returns 0
if the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0000_0111, Some(8));
bv.push_u8(0b1100_0010, Some(8));
let s = &bv[2..2];
assert_eq!(s.leading_zeros(), 0);
let s = &bv[0..4];
assert_eq!(s.leading_zeros(), 4);
let s = &bv[8..];
assert_eq!(s.leading_zeros(), 0);
let s = &bv[10..];
assert_eq!(s.leading_zeros(), 4);
sourcepub fn trailing_ones(&self) -> usize
pub fn trailing_ones(&self) -> usize
Counts the number of bits from the end of the bitslice to the
last bit that is set to 0
.
Returns 0 if the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0000_0111, Some(8));
bv.push_u8(0b1100_0111, Some(8));
let s = &bv[2..2];
assert_eq!(s.trailing_ones(), 0);
let s = &bv[0..4];
assert_eq!(s.trailing_ones(), 0);
let s = &bv[4..10];
assert_eq!(s.trailing_ones(), 5);
let s = &bv[10..];
assert_eq!(s.trailing_ones(), 3);
sourcepub fn trailing_zeros(&self) -> usize
pub fn trailing_zeros(&self) -> usize
Counts the number of bits from the end of the bitslice to the
last bit that is set to 1
.
Returns 0 if the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1111_1000, Some(8));
bv.push_u8(0b0011_1000, Some(8));
let s = &bv[2..2];
assert_eq!(s.trailing_zeros(), 0);
let s = &bv[0..4];
assert_eq!(s.trailing_zeros(), 0);
let s = &bv[4..10];
assert_eq!(s.trailing_zeros(), 5);
let s = &bv[10..];
assert_eq!(s.trailing_zeros(), 3);
sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Counts the bits in the slice that are set to 1
.
Returns 0 if the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1111_1000, Some(8));
bv.push_u8(0b0011_1000, Some(8));
let s = &bv[2..2];
assert_eq!(s.count_ones(), 0);
let s = &bv[0..4];
assert_eq!(s.count_ones(), 4);
let s = &bv[4..10];
assert_eq!(s.count_ones(), 1);
let s = &bv[10..];
assert_eq!(s.count_ones(), 3);
sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Counts the bits in the slice that are set to 0
.
Returns 0 if the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0000_0111, Some(8));
bv.push_u8(0b1100_0111, Some(8));
let s = &bv[2..2];
assert_eq!(s.count_zeros(), 0);
let s = &bv[0..4];
assert_eq!(s.count_zeros(), 4);
let s = &bv[4..10];
assert_eq!(s.count_zeros(), 1);
let s = &bv[10..];
assert_eq!(s.count_zeros(), 3);
sourcepub fn first_one(&self) -> Option<usize>
pub fn first_one(&self) -> Option<usize>
Returns the index of the first bit in the BitSlice
that is
set to 1
. Returns None if there are no bits set to 1
or if
the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0000_0111, Some(8));
bv.push_u8(0b1100_0111, Some(8));
let s = &bv[2..2];
assert_eq!(s.first_one(), None);
let s = &bv[0..4];
assert_eq!(s.first_one(), None);
let s = &bv[4..10];
assert_eq!(s.first_one(), Some(1));
let s = &bv[10..];
assert_eq!(s.first_one(), Some(3));
sourcepub fn first_zero(&self) -> Option<usize>
pub fn first_zero(&self) -> Option<usize>
Returns the index of the first bit in the BitSlice
that is
set to 0
. Returns None if there are no bits set to 0
or if
the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1111_1000, Some(8));
bv.push_u8(0b0011_1000, Some(8));
let s = &bv[2..2];
assert_eq!(s.first_zero(), None);
let s = &bv[0..4];
assert_eq!(s.first_zero(), None);
let s = &bv[4..10];
assert_eq!(s.first_zero(), Some(1));
let s = &bv[10..];
assert_eq!(s.first_zero(), Some(3));
sourcepub fn last_one(&self) -> Option<usize>
pub fn last_one(&self) -> Option<usize>
Returns the index of the last bit in the BitSlice
that is
set to 1
. Returns None if there are no bits set to 1
or if
the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0000_0111, Some(8));
bv.push_u8(0b1100_0110, Some(8));
let s = &bv[2..2];
assert_eq!(s.last_one(), None);
let s = &bv[0..4];
assert_eq!(s.last_one(), None);
let s = &bv[4..10];
assert_eq!(s.last_one(), Some(5));
let s = &bv[10..];
assert_eq!(s.last_one(), Some(4));
sourcepub fn last_zero(&self) -> Option<usize>
pub fn last_zero(&self) -> Option<usize>
Returns the index of the last bit in the BitSlice
that is
set to 0
. Returns None if there are no bits set to 0
or if
the slice is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1111_1000, Some(8));
bv.push_u8(0b0011_1001, Some(8));
let s = &bv[2..2];
assert_eq!(s.last_zero(), None);
let s = &bv[0..4];
assert_eq!(s.last_zero(), None);
let s = &bv[4..10];
assert_eq!(s.last_zero(), Some(5));
let s = &bv[10..];
assert_eq!(s.last_zero(), Some(4));
sourcepub fn first(&self) -> Option<BitRef<'_, bool>>
pub fn first(&self) -> Option<BitRef<'_, bool>>
Returns an immutable reference to the first bit in the
BitSlice
or None if the BitSlice
is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0010_0100, Some(8));
let s = &bv[2..2];
assert_eq!(s.first(), None);
let s = &bv[2..5];
assert_eq!(s.first().as_deref(), Some(&true));
sourcepub fn first_mut(&mut self) -> Option<BitRefMut<'_, bool>>
pub fn first_mut(&mut self) -> Option<BitRefMut<'_, bool>>
Returns a mutable reference to the first bit in the
BitSlice
or None if the BitSlice
is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0010_0100, Some(8));
let s = &mut bv[2..2];
assert_eq!(s.first_mut(), None);
let s = &mut bv[2..5];
assert_eq!(s.first_mut().as_deref(), Some(&true));
*s.first_mut().unwrap() = false;
assert_eq!(s.first_mut().as_deref(), Some(&false));
sourcepub fn last(&self) -> Option<BitRef<'_, bool>>
pub fn last(&self) -> Option<BitRef<'_, bool>>
Returns an immutable reference to the last bit in the
BitSlice
or None if the BitSlice
is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0010_0100, Some(8));
let s = &bv[2..2];
assert_eq!(s.last(), None);
let s = &bv[2..5];
assert_eq!(s.last().as_deref(), Some(&false));
sourcepub fn last_mut(&mut self) -> Option<BitRefMut<'_, bool>>
pub fn last_mut(&mut self) -> Option<BitRefMut<'_, bool>>
Returns a mutable reference to the first bit in the
BitSlice
or None if the BitSlice
is empty.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0010_0100, Some(8));
let s = &mut bv[2..2];
assert_eq!(s.last_mut(), None);
let s = &mut bv[2..6];
assert_eq!(s.last_mut().as_deref(), Some(&true));
*s.last_mut().unwrap() = false;
assert_eq!(s.last_mut().as_deref(), Some(&false));
sourcepub fn iter_ones(&self) -> IterOnes<'_> ⓘ
pub fn iter_ones(&self) -> IterOnes<'_> ⓘ
Iterates over all the bits in the BitSlice
that are set to
1
.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b0010_0101, Some(8));
let s = &bv[2..2];
let mut iter = s.iter_ones();
assert_eq!(iter.next(), None);
let s = &bv[2..6];
let mut iter = s.iter_ones();
assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);
sourcepub fn iter_zeros(&self) -> IterZeros<'_> ⓘ
pub fn iter_zeros(&self) -> IterZeros<'_> ⓘ
Iterates over all the bits in the BitSlice
that are set to
0
.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1101_1010, Some(8));
let s = &bv[2..2];
let mut iter = s.iter_zeros();
assert_eq!(iter.next(), None);
let s = &bv[2..6];
let mut iter = s.iter_zeros();
assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);
sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Returns an iterator over the bits of this
BitSlice
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u8(0b1011_0011, None);
let s = &bv[0..3];
let mut iter = s.iter();
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), Some(false));
assert_eq!(iter.next(), Some(true));
assert_eq!(iter.next(), None);
sourcepub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_> ⓘ
Returns a mutable iterator that allows modifying the bits of
this BitSlice
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::with_capacity(20);
bv.push_u8(0b1011_1100, None);
assert_eq!(bv[0], true);
let s = &mut bv[0..7];
let iter = s.iter_mut();
for mut bit in iter {
*bit = true;
}
assert_eq!(bv.read_u8(0), (0b1111_1110, 8));
sourcepub fn iter_u8(&self) -> IterU8<'_> ⓘ
pub fn iter_u8(&self) -> IterU8<'_> ⓘ
Returns an iterator that iterates over the
BitSlice
8 bits at a time. Each invocation
of iter.next
returns a u8 value and the number of bits
read. The bits are read from the lower to the higher index
from the slice and shifted right, so the bit at the lower
index is the MSB of returned value while the bit at the
highest index is the LSB.
The iterator returns None if there are no more bits to return
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u16(0b0101_1101_0011_1010, Some(16));
let s = &bv[..];
let mut iter = s.iter_u8();
assert_eq!(iter.next(), Some((0b0101_1101, 8)));
assert_eq!(iter.next(), Some((0b0011_1010, 8)));
assert_eq!(iter.next(), None);
sourcepub fn iter_u16(&self) -> IterU16<'_> ⓘ
pub fn iter_u16(&self) -> IterU16<'_> ⓘ
Returns an iterator that iterates over the
BitSlice
16 bits at a time. Each invocation
of iter.next
returns a u16 value and the number of bits
read. The bits are read from the lower to the higher index
from the slice and shifted right, so the bit at the lower
index is the MSB of returned value while the bit at the
highest index is the LSB.
The iterator returns None if there are no more bits to return
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u16(0b0101_1101_0011_1010, Some(16));
let s = &bv[..];
let mut iter = s.iter_u16();
assert_eq!(iter.next(), Some((0b0101_1101_0011_1010, 16)));
assert_eq!(iter.next(), None);
sourcepub fn iter_u32(&self) -> IterU32<'_> ⓘ
pub fn iter_u32(&self) -> IterU32<'_> ⓘ
Returns an iterator that iterates over the
BitSlice
32 bits at a time. Each invocation
of iter.next
returns a u32 value and the number of bits
read. The bits are read from the lower to the higher index
from the slice and shifted right, so the bit at the lower
index is the MSB of returned value while the bit at the
highest index is the LSB.
The iterator returns None if there are no more bits to return
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u16(0b0101_1101_0011_1010, Some(16));
bv.push_u16(0b1111_0011_1100_0000, Some(16));
let s = &bv[..];
let mut iter = s.iter_u32();
assert_eq!(iter.next(), Some((0b0101_1101_0011_1010_1111_0011_1100_0000, 32)));
assert_eq!(iter.next(), None);
sourcepub fn iter_u64(&self) -> IterU64<'_> ⓘ
pub fn iter_u64(&self) -> IterU64<'_> ⓘ
Returns an iterator that iterates over the
BitSlice
64 bits at a time. Each invocation
of iter.next
returns a u64 value and the number of bits
read. The bits are read from the lower to the higher index
from the slice and shifted right, so the bit at the lower
index is the MSB of returned value while the bit at the
highest index is the LSB.
The iterator returns None if there are no more bits to return
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u64(u64::MAX, Some(64));
let s = &bv[..];
let mut iter = s.iter_u64();
assert_eq!(iter.next(), Some((u64::MAX, 64)));
assert_eq!(iter.next(), None);
sourcepub fn iter_u128(&self) -> IterU128<'_> ⓘ
pub fn iter_u128(&self) -> IterU128<'_> ⓘ
Returns an iterator that iterates over the
BitSlice
128 bits at a time. Each invocation
of iter.next
returns a u128 value and the number of bits
read. The bits are read from the lower to the higher index
from the slice and shifted right, so the bit at the lower
index is the MSB of returned value while the bit at the
highest index is the LSB.
The iterator returns None if there are no more bits to return
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u64(u64::MAX, Some(64));
bv.push_u64(u64::MAX, Some(64));
let s = &bv[..];
let mut iter = s.iter_u128();
assert_eq!(iter.next(), Some((u128::MAX, 128)));
assert_eq!(iter.next(), None);
sourcepub fn as_u8(&self) -> (u8, usize)
pub fn as_u8(&self) -> (u8, usize)
Returns the contents of this slice as a u8
. This
method will panic if the length of the slice is greater
than or equal to 8. The bits are read from the lower to
the higher index from the slice and shifted right, so the
bit at the lower index is the MSB of returned value while
the bit at the highest index is the LSB.
Returns a u8
value and the number of bits read as a
tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
assert_eq!(s.as_u8(), (0b0011_0110, 8));
If a Result is preferred over a panic, then
using the TryFrom<&BitSlice>
trait may be used.
§Example of TryFrom<&BitSlice> for u8
use deepmesa::collections::BitVector;
use core::convert::TryFrom;
let mut bv = BitVector::new();
bv.push_u8(0b0011_0110, Some(8));
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[0..8];
match u8::try_from(s) {
Ok(val) => assert_eq!(val, 0b0011_0110),
Err(e) => assert!(false, "{}", e),
}
sourcepub fn as_u16(&self) -> (u16, usize)
pub fn as_u16(&self) -> (u16, usize)
Returns the contents of this slice as a u16
. This
method will panic if the length of the slice is greater
than or equal to 16. The bits are read from the lower to
the higher index from the slice and shifted right, so the
bit at the lower index is the MSB of returned value while
the bit at the highest index is the LSB.
Returns a u16
value and the number of bits read as a
tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u8(0b0011_0110, Some(8));
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
assert_eq!(s.as_u16(), (0b0011_0110_0011_0110, 16));
If a Result is preferred over a panic, then
using the TryFrom<&BitSlice>
trait may be used.
§Example of TryFrom<&BitSlice> for u16
use deepmesa::collections::BitVector;
use core::convert::TryFrom;
let mut bv = BitVector::new();
bv.push_u8(0b0011_0110, Some(8));
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
match u16::try_from(s) {
Ok(val) => assert_eq!(val, 0b0011_0110_0011_0110),
Err(e) => assert!(false, "{}", e),
}
sourcepub fn as_u32(&self) -> (u32, usize)
pub fn as_u32(&self) -> (u32, usize)
Returns the contents of this slice as a u32
. This
method will panic if the length of the slice is greater
than or equal to 32. The bits are read from the lower
to the higher index from the slice and shifted right, so
the bit at the lower index is the MSB of returned value
while the bit at the highest index is the LSB.
Returns a u32
value and the number of bits read as a
tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u32(u32::MAX, Some(32));
let s = &bv[..];
assert_eq!(s.as_u32(), (u32::MAX, 32));
If a Result is preferred over a panic, then
using the TryFrom<&BitSlice>
trait may be used.
§Example of TryFrom<&BitSlice> for u32
use deepmesa::collections::BitVector;
use core::convert::TryFrom;
let mut bv = BitVector::new();
bv.push_u32(u32::MAX, Some(32));
let s = &bv[..];
match u32::try_from(s) {
Ok(val) => assert_eq!(val, u32::MAX),
Err(e) => assert!(false, "{}", e),
}
sourcepub fn as_u64(&self) -> (u64, usize)
pub fn as_u64(&self) -> (u64, usize)
Returns the contents of this slice as a u64
. This
method will panic if the length of the slice is greater
than or equal to 64. The bits are read from the lower
to the higher index from the slice and shifted right, so
the bit at the lower index is the MSB of returned value
while the bit at the highest index is the LSB.
Returns a u64
value and the number of bits read as a
tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u64(u64::MAX, Some(64));
let s = &bv[..];
assert_eq!(s.as_u64(), (u64::MAX, 64));
If a Result is preferred over a panic, then
using the TryFrom<&BitSlice>
trait may be used.
§Example of TryFrom<&BitSlice> for u64
use deepmesa::collections::BitVector;
use core::convert::TryFrom;
let mut bv = BitVector::new();
bv.push_u64(u64::MAX, Some(64));
let s = &bv[..];
match u64::try_from(s) {
Ok(val) => assert_eq!(val, u64::MAX),
Err(e) => assert!(false, "{}", e),
}
sourcepub fn as_u128(&self) -> (u128, usize)
pub fn as_u128(&self) -> (u128, usize)
Returns the contents of this slice as a u128
. This
method will panic if the length of the slice is greater
than or equal to 128. The bits are read from the lower
to the higher index from the slice and shifted right, so
the bit at the lower index is the MSB of returned value
while the bit at the highest index is the LSB.
Returns a u128
value and the number of bits read as a
tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u128(u128::MAX, Some(128));
let s = &bv[..];
assert_eq!(s.as_u128(), (u128::MAX, 128));
If a Result is preferred over a panic, then
using the TryFrom<&BitSlice>
trait may be used.
§Example of TryFrom<&BitSlice> for u128
use deepmesa::collections::BitVector;
use core::convert::TryFrom;
let mut bv = BitVector::new();
bv.push_u128(u128::MAX, Some(128));
let s = &bv[..];
match u128::try_from(s) {
Ok(val) => assert_eq!(val, u128::MAX),
Err(e) => assert!(false, "{}", e),
}
sourcepub fn read_u8(&self, start: usize) -> (u8, usize)
pub fn read_u8(&self, start: usize) -> (u8, usize)
Reads upto 8 bits from this BitSlice
into
a u8 starting at the specified start
position. This
method will panic if start
is greater than or equal to
the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
let (val, read) = s.read_u8(0);
assert_eq!(read, 8);
assert_eq!(val, 0b0011_0110);
let (val, read) = s.read_u8(4);
assert_eq!(read, 4);
assert_eq!(val, 0b0000_0110);
sourcepub fn read_u16(&self, start: usize) -> (u16, usize)
pub fn read_u16(&self, start: usize) -> (u16, usize)
Reads upto 16 bits from this BitSlice
into a
u16 starting at the specified start
position. This
method will panic if start
is greater than or equal to
the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u16(0b0011_0110_1100_0011, Some(16));
let s = &bv[..];
let (val, read) = s.read_u16(0);
assert_eq!(read, 16);
assert_eq!(val, 0b0011_0110_1100_0011);
let (val, read) = s.read_u16(4);
assert_eq!(read, 12);
assert_eq!(val, 0b0000_0110_1100_0011);
sourcepub fn read_u32(&self, start: usize) -> (u32, usize)
pub fn read_u32(&self, start: usize) -> (u32, usize)
Reads upto 32 bits from this BitSlice
into
a u32 starting at the specified start
position. This
method will panic if start
is greater than or equal to
the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u16(0b0011_0110_1100_0011, Some(16));
bv.push_u16(0b1100_1010_0100_1100, Some(16));
let s = &bv[..];
let (val, read) = s.read_u32(0);
assert_eq!(read, 32);
assert_eq!(val, 0b0011_0110_1100_0011_1100_1010_0100_1100);
let (val, read) = s.read_u16(16);
assert_eq!(read, 16);
assert_eq!(val, 0b1100_1010_0100_1100);
sourcepub fn read_u64(&self, start: usize) -> (u64, usize)
pub fn read_u64(&self, start: usize) -> (u64, usize)
Reads upto 64 bits from this BitSlice
into
a u64 starting at the specified start
position. This
method will panic if start
is greater than or equal to
the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u16(0b0011_0110_1100_0011, Some(16));
bv.push_u16(0b1100_1010_0100_1100, Some(16));
let s = &bv[..];
let (val, read) = s.read_u64(20);
assert_eq!(read, 12);
assert_eq!(val, 0b1010_0100_1100);
let (val, read) = s.read_u64(16);
assert_eq!(read, 16);
assert_eq!(val, 0b1100_1010_0100_1100);
sourcepub fn read_u128(&self, start: usize) -> (u128, usize)
pub fn read_u128(&self, start: usize) -> (u128, usize)
Reads upto 128 bits from this BitSlice
into a u128 starting at the specified start
position. This method will panic if start
is greater
than or equal to the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
bv.push_u16(0b0011_0110_1100_0011, Some(16));
bv.push_u16(0b1100_1010_0100_1100, Some(16));
let s = &bv[..];
let (val, read) = bv.read_u128(20);
assert_eq!(read, 12);
assert_eq!(val, 0b1010_0100_1100);
let (val, read) = s.read_u128(16);
assert_eq!(read, 16);
assert_eq!(val, 0b1100_1010_0100_1100);
sourcepub fn read_bits_u8(&self, start: usize, max_bits: usize) -> (u8, usize)
pub fn read_bits_u8(&self, start: usize, max_bits: usize) -> (u8, usize)
Reads upto max_bits
bits from this
BitSlice
into a u8 starting at the
specified start
position. This method will panic if
max_bits
is greater than 8 or if start
is greater than
or equal to the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
Here is an illustrative example for a slice with 8 bits.
0 1 2 3 4 5 6 7
[ 0,0,1,1,0,1,1,0 ]
MSB [_______] LSB
^ Start = 2
value read = 0b1101
Reading 4 bits from the start position of 2, results in a u8 value of decimal 13.
This method returns the read value as well as the number of bits read as a tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
// Push 8 bits: 0b0011_0110
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
let (val, read) = s.read_bits_u8(2, 4);
assert_eq!(read,4);
assert_eq!(val, 0b0000_1101);
assert_eq!(val, 13);
sourcepub fn read_bits_u16(&self, start: usize, max_bits: usize) -> (u16, usize)
pub fn read_bits_u16(&self, start: usize, max_bits: usize) -> (u16, usize)
Reads upto max_bits
bits from this
BitSlice
into a u16 starting at the
specified start
position. This method will panic if
max_bits
is greater than 16 or if start
is greater
than or equal to the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
Here is an illustrative example for a slice with 8 bits.
0 1 2 3 4 5 6 7
[ 0,0,1,1,0,1,1,0 ]
MSB [_______] LSB
^ Start = 2
value read = 0b1101
Reading 4 bits from the start position of 2, results in a u16 value of decimal 13.
This method returns the read value as well as the number of bits read as a tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
// Push 8 bits: 0b0011_0110
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
let (val, read) = s.read_bits_u16(2, 4);
assert_eq!(read,4);
assert_eq!(val, 0b0000_1101);
assert_eq!(val, 13);
sourcepub fn read_bits_u32(&self, start: usize, max_bits: usize) -> (u32, usize)
pub fn read_bits_u32(&self, start: usize, max_bits: usize) -> (u32, usize)
Reads upto max_bits
bits from this
BitSlice
into a u32 starting at the
specified start
position. This method will panic if
max_bits
is greater than 32 or if start
is greater
than or equal to the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
Here is an illustrative example for a slice with 8 bits.
0 1 2 3 4 5 6 7
[ 0,0,1,1,0,1,1,0 ]
MSB [_______] LSB
^ Start = 2
value read = 0b1101
Reading 4 bits from the start position of 2, results in a u32 value of decimal 13.
This method returns the read value as well as the number of bits read as a tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
// Push 8 bits: 0b0011_0110
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
let (val, read) = s.read_bits_u32(2, 4);
assert_eq!(read,4);
assert_eq!(val, 0b0000_1101);
assert_eq!(val, 13);
sourcepub fn read_bits_u64(&self, start: usize, max_bits: usize) -> (u64, usize)
pub fn read_bits_u64(&self, start: usize, max_bits: usize) -> (u64, usize)
Reads upto max_bits
bits from this
BitSlice
into a u64 starting at the
specified start
position. This method will panic if
max_bits
is greater than 64 or if start
is greater
than or equal to the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
Here is an illustrative example for a slice with 8 elements.
0 1 2 3 4 5 6 7
[ 0,0,1,1,0,1,1,0 ]
MSB [_______] LSB
^ Start = 2
value read = 0b1101
Reading 4 bits from the start position of 2, results in a u64 value of decimal 13.
This method returns the read value as well as the number of bits read as a tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
// Push 8 bits: 0b0011_0110
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
let (val, read) = s.read_bits_u64(2, 4);
assert_eq!(read,4);
assert_eq!(val, 0b0000_1101);
assert_eq!(val, 13);
sourcepub fn read_bits_u128(&self, start: usize, max_bits: usize) -> (u128, usize)
pub fn read_bits_u128(&self, start: usize, max_bits: usize) -> (u128, usize)
Reads upto max_bits
bits from this
BitSlice
into a u128 starting at the
specified start
position. This method will panic if
max_bits
is greater than 128 or if start
is greater
than or equal to the length of the slice.
The bits are read from the lower to the higher index from the slice and shifted right, so the bit at the lower index is the MSB of returned value while the bit at the highest index is the LSB.
Here is an illustrative example for a slice with 8 bits.
0 1 2 3 4 5 6 7
[ 0,0,1,1,0,1,1,0 ]
MSB [_______] LSB
^ Start = 2
value read = 0b1101
Reading 4 bits from the start position of 2, results in a u128 value of decimal 13.
This method returns the read value as well as the number of bits read as a tuple.
§Examples
use deepmesa::collections::BitVector;
let mut bv = BitVector::new();
// Push 8 bits: 0b0011_0110
bv.push_u8(0b0011_0110, Some(8));
let s = &bv[..];
let (val, read) = s.read_bits_u128(2, 4);
assert_eq!(read,4);
assert_eq!(val, 0b0000_1101);
assert_eq!(val, 13);
Trait Implementations§
source§impl BitAndAssign<&BitSlice> for &mut BitSlice
impl BitAndAssign<&BitSlice> for &mut BitSlice
source§fn bitand_assign(&mut self, rhs: &BitSlice)
fn bitand_assign(&mut self, rhs: &BitSlice)
&=
operation. Read moresource§impl BitAndAssign<&BitSlice> for BitVector
impl BitAndAssign<&BitSlice> for BitVector
source§fn bitand_assign(&mut self, rhs: &BitSlice)
fn bitand_assign(&mut self, rhs: &BitSlice)
&=
operation. Read moresource§impl BitAndAssign<bool> for &mut BitSlice
impl BitAndAssign<bool> for &mut BitSlice
source§fn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
&=
operation. Read moresource§impl BitAndAssign<bool> for BitSlice
impl BitAndAssign<bool> for BitSlice
source§fn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
&=
operation. Read moresource§impl BitOrAssign<&BitSlice> for &mut BitSlice
impl BitOrAssign<&BitSlice> for &mut BitSlice
source§fn bitor_assign(&mut self, rhs: &BitSlice)
fn bitor_assign(&mut self, rhs: &BitSlice)
|=
operation. Read moresource§impl BitOrAssign<&BitSlice> for BitVector
impl BitOrAssign<&BitSlice> for BitVector
source§fn bitor_assign(&mut self, rhs: &BitSlice)
fn bitor_assign(&mut self, rhs: &BitSlice)
|=
operation. Read moresource§impl BitOrAssign<bool> for &mut BitSlice
impl BitOrAssign<bool> for &mut BitSlice
source§fn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
|=
operation. Read moresource§impl BitOrAssign<bool> for BitSlice
impl BitOrAssign<bool> for BitSlice
source§fn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
|=
operation. Read moresource§impl BitXorAssign<&BitSlice> for &mut BitSlice
impl BitXorAssign<&BitSlice> for &mut BitSlice
source§fn bitxor_assign(&mut self, rhs: &BitSlice)
fn bitxor_assign(&mut self, rhs: &BitSlice)
^=
operation. Read moresource§impl BitXorAssign<&BitSlice> for BitVector
impl BitXorAssign<&BitSlice> for BitVector
source§fn bitxor_assign(&mut self, rhs: &BitSlice)
fn bitxor_assign(&mut self, rhs: &BitSlice)
^=
operation. Read moresource§impl BitXorAssign<bool> for &mut BitSlice
impl BitXorAssign<bool> for &mut BitSlice
source§fn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
^=
operation. Read moresource§impl BitXorAssign<bool> for BitSlice
impl BitXorAssign<bool> for BitSlice
source§fn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
^=
operation. Read more