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
use crate::encoding::get_length;

/// Decodes according to [Plain strings](https://github.com/apache/parquet-format/blob/master/Encodings.md#plain-plain--0),
/// prefixes, lengths and values
/// # Implementation
/// This struct does not allocate on the heap.
#[derive(Debug)]
pub struct Decoder<'a> {
    values: &'a [u8],
    remaining: usize,
}

impl<'a> Decoder<'a> {
    #[inline]
    pub fn new(values: &'a [u8], length: usize) -> Self {
        Self {
            values,
            remaining: length,
        }
    }
}

impl<'a> Iterator for Decoder<'a> {
    type Item = &'a [u8];

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let values = self.values;
        if values.len() >= 4 {
            let next_len = get_length(values) as usize;
            let values = &values[4..];

            let result = Some(&values[0..next_len]);
            self.values = &values[next_len..];
            self.remaining -= 1;

            result
        } else {
            None
        }
    }

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