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
use super::super::utils::Pushable;

/// A [`Pushable`] for fixed sized binary data
#[derive(Debug)]
pub struct FixedSizeBinary {
    pub values: Vec<u8>,
    pub size: usize,
}

impl FixedSizeBinary {
    #[inline]
    pub fn with_capacity(capacity: usize, size: usize) -> Self {
        Self {
            values: Vec::with_capacity(capacity * size),
            size,
        }
    }

    #[inline]
    pub fn push(&mut self, value: &[u8]) {
        debug_assert_eq!(value.len(), self.size);
        self.values.extend(value);
    }

    #[inline]
    pub fn extend_constant(&mut self, additional: usize) {
        self.values
            .resize(self.values.len() + additional * self.size, 0);
    }
}

impl<'a> Pushable<&'a [u8]> for FixedSizeBinary {
    #[inline]
    fn reserve(&mut self, additional: usize) {
        self.values.reserve(additional * self.size);
    }
    #[inline]
    fn push(&mut self, value: &[u8]) {
        debug_assert_eq!(value.len(), self.size);
        self.push(value);
    }

    #[inline]
    fn push_null(&mut self) {
        self.values.extend(std::iter::repeat(0).take(self.size))
    }

    #[inline]
    fn extend_constant(&mut self, additional: usize, value: &[u8]) {
        assert_eq!(value.len(), 0);
        self.extend_constant(additional)
    }

    #[inline]
    fn len(&self) -> usize {
        self.values.len() / self.size
    }
}