redb_32bit/
complex_types.rs

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