1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::bits::BitIter;
use crate::svec::{SVector, StoreMut, VectorMask};
use std::mem;

/// Iterate over the non-zero (mutable) elements of a vector
pub struct DrainIter<'a, S>
where
    S: StoreMut,
{
    pub(crate) vec_ptr: *mut SVector<S>,
    pub(crate) iterator: BitIter<&'a VectorMask>,
    pub(crate) store: &'a mut S,
}

impl<'a, S> Iterator for DrainIter<'a, S>
where
    S: 'a + StoreMut,
{
    type Item = (usize, &'a mut S::Item);

    fn next(&mut self) -> Option<Self::Item> {
        self.iterator
            .next()
            .map(|idx| (idx, unsafe { mem::transmute(self.store.get_mut(idx)) })) // GAT
    }
}

/*
impl<'a> Iterator for DrainIter<'a, ()> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        self.iterator.next()
    }
}*/

impl<'a, S> Drop for DrainIter<'a, S>
where
    S: 'a + StoreMut,
{
    fn drop(&mut self) {
        let vec = unsafe { &mut *self.vec_ptr };
        vec.clear();
    }
}