use super::Items;
#[derive(Debug)]
pub struct Drain<'a> {
data: &'a mut Vec<u64>,
index: usize,
}
impl Items {
#[inline]
pub fn drain(&mut self) -> Drain<'_> {
Drain { data: &mut self.data, index: 0 }
}
}
impl Iterator for Drain<'_> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
loop {
let block = self.data[self.index];
if block != 0 {
let num = block.trailing_zeros() as usize;
self.data[self.index] = block & (block - 1);
return Some(self.index << 6 | num);
}
self.index += 1;
if self.index >= self.data.len() {
return None;
}
}
}
}
impl Drop for Drain<'_> {
fn drop(&mut self) {
self.data[self.index..].fill(0);
}
}