bytes_kman/std/collections/
hash_map.rs

1use ::std::collections::hash_map::HashMap;
2
3use crate::{TBuffer, TBytes};
4
5impl<T, A> TBytes for HashMap<T, A>
6where
7    T: TBytes + Eq + ::std::hash::Hash,
8    A: TBytes,
9{
10    fn size(&self) -> usize {
11        let mut size = 0usize.size();
12
13        for (key, value) in self.iter() {
14            size += key.size();
15            size += value.size();
16        }
17
18        size
19    }
20
21    fn to_bytes(&self) -> Vec<u8> {
22        let mut buffer = Vec::with_capacity(self.size());
23
24        buffer.append(&mut self.len().to_bytes());
25
26        for (key, value) in self.iter() {
27            buffer.append(&mut key.to_bytes());
28            buffer.append(&mut value.to_bytes());
29        }
30
31        buffer
32    }
33
34    fn from_bytes(buffer: &mut TBuffer) -> Option<Self>
35    where
36        Self: Sized,
37    {
38        if buffer.len() < Self::default().size() {
39            return None;
40        }
41        let len = usize::from_bytes(buffer)?;
42
43        let mut res = Vec::new();
44
45        for _ in 0..len {
46            let key = T::from_bytes(buffer);
47            let value = A::from_bytes(buffer);
48            if let Some(key) = key {
49                if let Some(value) = value {
50                    res.push((key, value));
51                } else {
52                    let mut bytes = key.to_bytes();
53                    while let Some(byte) = bytes.pop() {
54                        buffer.insert(0, byte)
55                    }
56                    while let Some(element) = res.pop() {
57                        let mut bytes = element.1.to_bytes();
58                        while let Some(byte) = bytes.pop() {
59                            buffer.insert(0, byte)
60                        }
61                        let mut bytes = element.0.to_bytes();
62                        while let Some(byte) = bytes.pop() {
63                            buffer.insert(0, byte)
64                        }
65                    }
66                    let mut bytes = len.to_bytes();
67                    while let Some(byte) = bytes.pop() {
68                        buffer.insert(0, byte)
69                    }
70                    return None;
71                }
72            } else {
73                while let Some(element) = res.pop() {
74                    let mut bytes = element.1.to_bytes();
75                    while let Some(byte) = bytes.pop() {
76                        buffer.insert(0, byte)
77                    }
78                    let mut bytes = element.0.to_bytes();
79                    while let Some(byte) = bytes.pop() {
80                        buffer.insert(0, byte)
81                    }
82                }
83                let mut bytes = len.to_bytes();
84                while let Some(byte) = bytes.pop() {
85                    buffer.insert(0, byte)
86                }
87                return None;
88            }
89        }
90
91        let mut hashmap = HashMap::new();
92
93        for (key, value) in res {
94            hashmap.insert(key, value);
95        }
96
97        Some(hashmap)
98    }
99}
100
101#[cfg(test)]
102mod test {
103    use crate::TBytes;
104    use std::collections::HashMap;
105
106    #[test]
107    fn hash_map() {
108        let mut a = HashMap::new();
109        a.insert("QQQL".to_string(), "Maniac".to_string());
110        a.insert("Content-Length".to_string(), "21".to_string());
111
112        let mut bytes = a.to_bytes();
113
114        let other = <HashMap<String, String>>::from_bytes(&mut bytes).unwrap();
115
116        assert_eq!(a, other)
117    }
118
119    #[test]
120    fn incomplete() {
121        let mut buffer = Vec::new();
122        buffer.append(&mut 2usize.to_bytes());
123        buffer.append(&mut String::from("Foo").to_bytes());
124        buffer.append(&mut String::from("Bar").to_bytes());
125
126        let clone_buffer = buffer.clone();
127        let other_buffer = HashMap::<String, String>::from_bytes(&mut buffer);
128        if let Some(other_buffer) = other_buffer {
129            panic!("This should be possible! Other buffer: {other_buffer:?}");
130        }
131
132        assert_eq!(buffer, clone_buffer);
133        buffer.append(&mut String::from("James").to_bytes());
134        buffer.append(&mut String::from("21").to_bytes());
135
136        let value = HashMap::<String, String>::from_bytes(&mut buffer).unwrap();
137        let mut hashmap = HashMap::<String, String>::new();
138        hashmap.insert("Foo".into(), "Bar".into());
139        hashmap.insert("James".into(), "21".into());
140        assert_eq!(value, hashmap)
141    }
142}