Skip to main content

BitIterator

Trait BitIterator 

Source
pub trait BitIterator {
    // Required methods
    fn bit_iter(&self) -> impl DoubleEndedIterator<Item = bool>;
    fn bit_chunks<T>(&self, n: usize) -> impl Iterator<Item = T>
       where T: TryFrom<u64> + Default;
}
Expand description

Bits iterator implementation on [u8]

Required Methods§

Source

fn bit_iter(&self) -> impl DoubleEndedIterator<Item = bool>

Iterator bits

§Examples
assert_eq!(
  [0b1111_0000_u8].bit_iter().collect::<Vec<bool>>(),
  vec![true, true, true, true, false, false, false, false]
);
Source

fn bit_chunks<T>(&self, n: usize) -> impl Iterator<Item = T>
where T: TryFrom<u64> + Default,

Returns the bits in the buffer grouped by n

§Parameters
  • T: the type to contains the grouped bits
  • n: the number of bits to group
  • 1 <= n <= T::BITS <= 32
§Examples
assert_eq!(
    vec![0b1111_1111, 0b1111_1111].bit_chunks(6).collect::<Vec<u8>>(),
    vec![0b11_1111, 0b11_1111, 0b11_1100]
);
assert_eq!(
    vec![0b1111_1111; 3].bit_chunks(11).collect::<Vec<u16>>(),
    vec![0b111_1111_1111, 0b111_1111_1111, 0b110_0000_0000]
);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl BitIterator for [u8]

Source§

fn bit_iter(&self) -> impl DoubleEndedIterator<Item = bool>

Source§

fn bit_chunks<T>(&self, n: usize) -> impl Iterator<Item = T>
where T: TryFrom<u64> + Default,

Implementors§