use crate::{
Bit,
Nybble,
};
pub struct IterableNybble<'a> {
nybble: &'a Nybble,
current_index: u8,
}
impl<'a> IterableNybble<'a> {
#[must_use]
pub const fn new(nybble: &'a Nybble) -> Self {
Self {
nybble,
current_index: 0,
}
}
}
impl<'a> Iterator for IterableNybble<'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 > 4 {
self.current_index = 0;
None
} else {
self.current_index = next_index;
Some(self.nybble.get_bit(current_index))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Bit;
#[test]
fn test_iterable_nybble() {
let nybble = Nybble::from(0b1010); let mut iter = nybble.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(), None);
}
}