Skip to main content

redb/
complex_types.rs

1use crate::types::{TypeName, Value};
2use alloc::format;
3use alloc::vec::Vec;
4
5// Encode len as a varint and store it at the end of output
6pub(super) fn encode_varint_len(len: usize, output: &mut Vec<u8>) {
7    if len < 254 {
8        output.push(len.try_into().unwrap());
9    } else if len <= u16::MAX.into() {
10        let u16_len: u16 = len.try_into().unwrap();
11        output.push(254);
12        output.extend_from_slice(&u16_len.to_le_bytes());
13    } else {
14        let u32_len: u32 = len.try_into().unwrap();
15        output.push(255);
16        output.extend_from_slice(&u32_len.to_le_bytes());
17    }
18}
19
20// Decode a variable length int starting at the beginning of data
21// Returns (decoded length, length consumed of `data`)
22pub(super) fn decode_varint_len(data: &[u8]) -> (usize, usize) {
23    match data[0] {
24        0..=253 => (data[0] as usize, 1),
25        254 => (
26            u16::from_le_bytes(data[1..3].try_into().unwrap()) as usize,
27            3,
28        ),
29        255 => (
30            u32::from_le_bytes(data[1..5].try_into().unwrap()) as usize,
31            5,
32        ),
33    }
34}
35
36impl<T: Value> Value for Vec<T> {
37    type SelfType<'a>
38        = Vec<T::SelfType<'a>>
39    where
40        Self: 'a;
41    type AsBytes<'a>
42        = Vec<u8>
43    where
44        Self: 'a;
45
46    fn fixed_width() -> Option<usize> {
47        None
48    }
49
50    fn from_bytes<'a>(data: &'a [u8]) -> Vec<T::SelfType<'a>>
51    where
52        Self: 'a,
53    {
54        let (elements, mut offset) = decode_varint_len(data);
55        let mut result = Vec::with_capacity(elements);
56        for _ in 0..elements {
57            let element_len = if let Some(len) = T::fixed_width() {
58                len
59            } else {
60                let (len, consumed) = decode_varint_len(&data[offset..]);
61                offset += consumed;
62                len
63            };
64            result.push(T::from_bytes(&data[offset..(offset + element_len)]));
65            offset += element_len;
66        }
67        assert_eq!(offset, data.len());
68        result
69    }
70
71    fn as_bytes<'a, 'b: 'a>(value: &'a Vec<T::SelfType<'b>>) -> Vec<u8>
72    where
73        Self: 'b,
74    {
75        let mut result = if let Some(width) = T::fixed_width() {
76            Vec::with_capacity(value.len() * width + 5)
77        } else {
78            Vec::with_capacity(value.len() * 2 + 5)
79        };
80        encode_varint_len(value.len(), &mut result);
81
82        for element in value {
83            let serialized = T::as_bytes(element);
84            if T::fixed_width().is_none() {
85                encode_varint_len(serialized.as_ref().len(), &mut result);
86            }
87            result.extend_from_slice(serialized.as_ref());
88        }
89        result
90    }
91
92    fn type_name() -> TypeName {
93        let inner = T::type_name();
94        TypeName::internal(&format!("Vec<{}>", inner.name()))
95            .into_composite(inner.is_user_defined())
96    }
97}