use crate::{
Bit,
Byte,
};
pub struct IterableByte<'a> {
byte: &'a Byte,
current_index: u8,
}
impl<'a> IterableByte<'a> {
#[must_use]
pub const fn new(byte: &'a Byte) -> Self {
Self {
byte,
current_index: 0,
}
}
}
impl<'a> Iterator for IterableByte<'a> {
type Item = Bit;
fn next(&mut self) -> Option<Self::Item> {
let current_index = self.current_index;
let next_index = current_index + 1;
if next_index > 8 {
self.current_index = 0;
None
} else {
self.current_index = next_index;
Some(self.byte.get_bit(current_index))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iterable_nybble() {
let byte = Byte::from(0b11001010); let mut iter = byte.iter();
assert_eq!(iter.next(), Some(Bit::zero()));
assert_eq!(iter.next(), Some(Bit::one()));
assert_eq!(iter.next(), Some(Bit::zero()));
assert_eq!(iter.next(), Some(Bit::one()));
assert_eq!(iter.next(), Some(Bit::zero()));
assert_eq!(iter.next(), Some(Bit::zero()));
assert_eq!(iter.next(), Some(Bit::one()));
assert_eq!(iter.next(), Some(Bit::one()));
assert_eq!(iter.next(), None);
}
}