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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::{
    RawBitVecIter,
    RawBitVecDrain,
    TypedBitElem,
    PhantomData
};

pub struct TypedBitVecIter<TYPE: TypedBitElem>(pub(crate) RawBitVecIter, pub(crate) PhantomData<TYPE>);

impl<TYPE: TypedBitElem> Iterator for TypedBitVecIter<TYPE> {
    type Item = TYPE::Base;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        match unsafe {self.0.next(TYPE::PROTO)} {
            Some(bits) => Some(TYPE::bits_to_val(bits)),
            None => None,
        }
    }

    #[inline(always)]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.0.len();
        (len, Some(len))
    }
}

impl<TYPE: TypedBitElem> DoubleEndedIterator for TypedBitVecIter<TYPE> {
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        match unsafe {self.0.next_back(TYPE::PROTO)} {
            Some(bits) => Some(TYPE::bits_to_val(bits)),
            None => None,
        }
    }
}

impl<TYPE: TypedBitElem> ExactSizeIterator for TypedBitVecIter<TYPE> {
    #[inline(always)]
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<TYPE: TypedBitElem> Drop for TypedBitVecIter<TYPE>  {
    #[inline(always)]
    fn drop(&mut self) {/* RawBitVecIter will handle the deallocation */}
}

pub struct TypedBitVecDrain<'vec, TYPE: TypedBitElem>(pub(crate) RawBitVecDrain<'vec>, pub(crate) PhantomData<TYPE>);

impl<'vec, TYPE: TypedBitElem> Iterator for TypedBitVecDrain<'vec, TYPE> {
    type Item = TYPE::Base;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        match unsafe {self.0.next(TYPE::PROTO)} {
            Some(bits) => Some(TYPE::bits_to_val(bits)),
            None => None,
        }
    }

    #[inline(always)]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.0.len();
        (len, Some(len))
    }
}

impl<'vec, TYPE: TypedBitElem> DoubleEndedIterator for TypedBitVecDrain<'vec, TYPE> {

    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        match unsafe {self.0.next_back(TYPE::PROTO)} {
            Some(bits) => Some(TYPE::bits_to_val(bits)),
            None => None,
        }
    }
}

impl<'vec, TYPE: TypedBitElem> ExactSizeIterator for TypedBitVecDrain<'vec, TYPE> {
    #[inline(always)]
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<'vec, TYPE: TypedBitElem> Drop for TypedBitVecDrain<'vec, TYPE>  {
    #[inline(always)]
    fn drop(&mut self) {/* RawBitVecIter will handle the deallocation */}
}