use crate::binary::Binary;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ones<T: Binary> {
n: T,
}
impl<T: Binary> Ones<T> {
pub const fn new(n: T) -> Self {
Self { n }
}
}
impl<T: Binary> Iterator for Ones<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.n.has_one().then(|| {
let b = self.n.lowest_one();
self.n ^= b;
b
})
}
}
impl<T: Binary> DoubleEndedIterator for Ones<T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.n.has_one().then(|| {
let b = self.n.highest_one();
self.n ^= b;
b
})
}
}