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
#[derive(Debug, Clone)]
pub struct BytesIndices<'a, T> {
    offset: usize,

    bytes: &'a [T],
}

impl<'a, T> BytesIndices<'a, T> {
    pub fn new(bytes: &'a [T]) -> Self {
        Self { offset: 0, bytes }
    }
}

impl<'a, T> Iterator for BytesIndices<'a, T>
where
    T: Copy,
{
    type Item = (usize, T);

    fn next(&mut self) -> Option<Self::Item> {
        if self.offset < self.bytes.len() {
            let offset = self.offset;

            self.offset += 1;
            Some((offset, self.bytes[offset]))
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.bytes.len(), Some(self.bytes.len()))
    }
}

impl<'a, T> ExactSizeIterator for BytesIndices<'a, T> where T: Copy {}