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

source

pub fn len(&self) -> usize

Returns the number of bits in the BitSlice

§Examples
use deepmesa::collections::BitVector;

let mut bv = BitVector::with_capacity(22);
bv.push_u8(0b1001_1011, None);
let s = &bv[2..4];
assert_eq!(s.len(), 2);
source

pub fn is_empty(&self) -> bool

source

pub fn repeat(&self, n: usize) -> BitVector

source

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

source

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));
source

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);
source

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);
source

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);
source

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);
source

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));
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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));
source

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));
source

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));
source

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));
source

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));
source

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));
source

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));
source

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));
source

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);
source

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);
source

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);
source

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));
source

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);
source

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);
source

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);
source

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);
source

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);
source

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),
}
source

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),
}
source

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),
}
source

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),
}
source

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),
}
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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 AsMut<BitSlice> for BitVector

source§

fn as_mut(&mut self) -> &mut BitSlice

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsRef<BitSlice> for BitVector

source§

fn as_ref(&self) -> &BitSlice

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl BitAnd<&BitSlice> for BitVector

§

type Output = BitVector

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: &BitSlice) -> Self::Output

Performs the & operation. Read more
source§

impl BitAndAssign<&BitSlice> for &mut BitSlice

source§

fn bitand_assign(&mut self, rhs: &BitSlice)

Performs the &= operation. Read more
source§

impl BitAndAssign<&BitSlice> for BitVector

source§

fn bitand_assign(&mut self, rhs: &BitSlice)

Performs the &= operation. Read more
source§

impl BitAndAssign<bool> for &mut BitSlice

source§

fn bitand_assign(&mut self, rhs: bool)

Performs the &= operation. Read more
source§

impl BitAndAssign<bool> for BitSlice

source§

fn bitand_assign(&mut self, rhs: bool)

Performs the &= operation. Read more
source§

impl BitOr<&BitSlice> for BitVector

§

type Output = BitVector

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: &BitSlice) -> Self::Output

Performs the | operation. Read more
source§

impl BitOrAssign<&BitSlice> for &mut BitSlice

source§

fn bitor_assign(&mut self, rhs: &BitSlice)

Performs the |= operation. Read more
source§

impl BitOrAssign<&BitSlice> for BitVector

source§

fn bitor_assign(&mut self, rhs: &BitSlice)

Performs the |= operation. Read more
source§

impl BitOrAssign<bool> for &mut BitSlice

source§

fn bitor_assign(&mut self, rhs: bool)

Performs the |= operation. Read more
source§

impl BitOrAssign<bool> for BitSlice

source§

fn bitor_assign(&mut self, rhs: bool)

Performs the |= operation. Read more
source§

impl BitXor<&BitSlice> for BitVector

§

type Output = BitVector

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: &BitSlice) -> Self::Output

Performs the ^ operation. Read more
source§

impl BitXorAssign<&BitSlice> for &mut BitSlice

source§

fn bitxor_assign(&mut self, rhs: &BitSlice)

Performs the ^= operation. Read more
source§

impl BitXorAssign<&BitSlice> for BitVector

source§

fn bitxor_assign(&mut self, rhs: &BitSlice)

Performs the ^= operation. Read more
source§

impl BitXorAssign<bool> for &mut BitSlice

source§

fn bitxor_assign(&mut self, rhs: bool)

Performs the ^= operation. Read more
source§

impl BitXorAssign<bool> for BitSlice

source§

fn bitxor_assign(&mut self, rhs: bool)

Performs the ^= operation. Read more
source§

impl Debug for BitSlice

source§

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

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

impl Index<Range<usize>> for &BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: Range<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<Range<usize>> for &mut BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: Range<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeFrom<usize>> for &BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeFrom<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeFrom<usize>> for &mut BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeFrom<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeFull> for &BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, _: RangeFull) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeFull> for &mut BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, _: RangeFull) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeInclusive<usize>> for &BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeInclusive<usize>> for &mut BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeTo<usize>> for &BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeTo<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeTo<usize>> for &mut BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeTo<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeToInclusive<usize>> for &BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeToInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeToInclusive<usize>> for &mut BitSlice

§

type Output = BitSlice

The returned type after indexing.
source§

fn index(&self, range: RangeToInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<usize> for &BitSlice

§

type Output = bool

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<usize> for &mut BitSlice

§

type Output = bool

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl IndexMut<Range<usize>> for &mut BitSlice

source§

fn index_mut(&mut self, range: Range<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeFrom<usize>> for &mut BitSlice

source§

fn index_mut(&mut self, range: RangeFrom<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeFull> for &mut BitSlice

source§

fn index_mut(&mut self, _: RangeFull) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeInclusive<usize>> for &mut BitSlice

source§

fn index_mut(&mut self, range: RangeInclusive<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeTo<usize>> for &mut BitSlice

source§

fn index_mut(&mut self, range: RangeTo<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeToInclusive<usize>> for &mut BitSlice

source§

fn index_mut(&mut self, range: RangeToInclusive<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a> IntoIterator for &'a BitSlice

§

type Item = bool

The type of the elements being iterated over.
§

type IntoIter = Iter<'a>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl Not for &mut BitSlice

§

type Output = &mut BitSlice

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl TryFrom<&BitSlice> for u128

§

type Error = String

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

fn try_from(bitslice: &BitSlice) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&BitSlice> for u16

§

type Error = String

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

fn try_from(bitslice: &BitSlice) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&BitSlice> for u32

§

type Error = String

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

fn try_from(bitslice: &BitSlice) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&BitSlice> for u64

§

type Error = String

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

fn try_from(bitslice: &BitSlice) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&BitSlice> for u8

§

type Error = String

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

fn try_from(bitslice: &BitSlice) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more