xxai_msgpacker/pack/
collections.rs

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::{Format, Packable};
use core::{borrow::Borrow, iter};

/// Packs an array into the extendable buffer, returning the amount of written bytes.
#[allow(unreachable_code)]
pub fn pack_array<T, A, I, V>(buf: &mut T, iter: A) -> usize
where
    T: Extend<u8>,
    A: IntoIterator<IntoIter = I>,
    I: Iterator<Item = V> + ExactSizeIterator,
    V: Packable,
{
    let values = iter.into_iter();
    let len = values.len();
    let n = if len <= 15 {
        buf.extend(iter::once(((len & 0x0f) as u8) | 0x90));
        1
    } else if len <= u16::MAX as usize {
        buf.extend(iter::once(Format::ARRAY16).chain((len as u16).to_be_bytes()));
        3
    } else if len <= u32::MAX as usize {
        buf.extend(iter::once(Format::ARRAY32).chain((len as u32).to_be_bytes()));
        5
    } else {
        #[cfg(feature = "strict")]
        panic!("strict serialization enabled; the buffer is too large");
        return 0;
    };
    n + values.map(|v| v.pack(buf)).sum::<usize>()
}

/// Packs a map into the extendable buffer, returning the amount of written bytes.
#[allow(unreachable_code)]
pub fn pack_map<T, A, I, B, K, V>(buf: &mut T, iter: A) -> usize
where
    T: Extend<u8>,
    A: IntoIterator<IntoIter = I>,
    B: Borrow<(K, V)>,
    I: Iterator<Item = B> + ExactSizeIterator,
    K: Packable,
    V: Packable,
{
    let map = iter.into_iter();
    let len = map.len();
    let n = if len <= 15 {
        buf.extend(iter::once(((len & 0x0f) as u8) | 0x80));
        1
    } else if len <= u16::MAX as usize {
        buf.extend(iter::once(Format::MAP16).chain((len as u16).to_be_bytes()));
        3
    } else if len <= u32::MAX as usize {
        buf.extend(iter::once(Format::MAP32).chain((len as u32).to_be_bytes()));
        5
    } else {
        #[cfg(feature = "strict")]
        panic!("strict serialization enabled; the buffer is too large");
        return 0;
    };
    n + map
        .map(|b| {
            let (k, v) = b.borrow();
            k.pack(buf) + v.pack(buf)
        })
        .sum::<usize>()
}

#[cfg(feature = "alloc")]
mod alloc {
    use super::*;
    use ::alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};

    impl<X> Packable for BTreeSet<X>
    where
        X: Packable,
    {
        fn pack<T>(&self, buf: &mut T) -> usize
        where
            T: Extend<u8>,
        {
            pack_array(buf, self)
        }
    }

    impl<X> Packable for BinaryHeap<X>
    where
        X: Packable,
    {
        fn pack<T>(&self, buf: &mut T) -> usize
        where
            T: Extend<u8>,
        {
            pack_array(buf, self)
        }
    }

    impl<X> Packable for LinkedList<X>
    where
        X: Packable,
    {
        fn pack<T>(&self, buf: &mut T) -> usize
        where
            T: Extend<u8>,
        {
            pack_array(buf, self)
        }
    }

    impl<X> Packable for VecDeque<X>
    where
        X: Packable,
    {
        fn pack<T>(&self, buf: &mut T) -> usize
        where
            T: Extend<u8>,
        {
            pack_array(buf, self)
        }
    }

    impl<K, V> Packable for BTreeMap<K, V>
    where
        K: Packable,
        V: Packable,
    {
        fn pack<T>(&self, buf: &mut T) -> usize
        where
            T: Extend<u8>,
        {
            pack_map(buf, self)
        }
    }
}

#[cfg(feature = "std")]
mod std {
    use super::*;
    use ::std::collections::{HashMap, HashSet};

    impl<X> Packable for HashSet<X>
    where
        X: Packable,
    {
        fn pack<T>(&self, buf: &mut T) -> usize
        where
            T: Extend<u8>,
        {
            pack_array(buf, self)
        }
    }

    impl<K, V> Packable for HashMap<K, V>
    where
        K: Packable,
        V: Packable,
    {
        fn pack<T>(&self, buf: &mut T) -> usize
        where
            T: Extend<u8>,
        {
            pack_map(buf, self)
        }
    }
}