1use fusio::{Error, SeqRead, Write};
2
3use super::{Decode, Encode};
4
5impl<T> Decode for Vec<T>
6where
7 T: Decode + Send + Sync,
8{
9 async fn decode<R>(reader: &mut R) -> Result<Self, Error>
10 where
11 R: SeqRead,
12 {
13 let len = u32::decode(reader).await? as usize;
14 let mut data = Vec::with_capacity(len);
15 for _ in 0..len {
16 data.push(T::decode(reader).await?);
17 }
18 Ok(data)
19 }
20}
21
22impl<T> Encode for Vec<T>
23where
24 T: Encode + Send + Sync,
25{
26 async fn encode<W>(&self, writer: &mut W) -> Result<(), Error>
27 where
28 W: Write,
29 {
30 (self.len() as u32).encode(writer).await?;
31 for item in self.iter() {
32 item.encode(writer).await?;
33 }
34
35 Ok(())
36 }
37
38 fn size(&self) -> usize {
39 self.iter()
40 .fold(size_of::<u32>(), |acc, item| acc + item.size())
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use std::io::Cursor;
47
48 use tokio::io::AsyncSeekExt;
49
50 use crate::serdes::{Decode, Encode};
51
52 #[tokio::test]
53 async fn test_u8_encode_decode() {
54 let source = b"hello! Tonbo".to_vec();
55
56 let mut bytes = Vec::new();
57 let mut cursor = Cursor::new(&mut bytes);
58
59 source.encode(&mut cursor).await.unwrap();
60
61 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
62 let decoded = Vec::<u8>::decode(&mut cursor).await.unwrap();
63
64 assert_eq!(source, decoded);
65 }
66
67 #[tokio::test]
68 async fn test_num_encode_decode() {
69 {
70 let source = vec![1_u32, 1237654, 456, 123456789];
71
72 let mut bytes = Vec::new();
73 let mut cursor = Cursor::new(&mut bytes);
74
75 source.encode(&mut cursor).await.unwrap();
76
77 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
78 let decoded = Vec::<u32>::decode(&mut cursor).await.unwrap();
79
80 assert_eq!(source, decoded);
81 }
82 {
83 let source = vec![1_i64, 1237654, 456, 123456789];
84
85 let mut bytes = Vec::new();
86 let mut cursor = Cursor::new(&mut bytes);
87
88 source.encode(&mut cursor).await.unwrap();
89
90 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
91 let decoded = Vec::<i64>::decode(&mut cursor).await.unwrap();
92
93 assert_eq!(source, decoded);
94 }
95 }
96
97 #[tokio::test]
98 async fn test_bool_encode_decode() {
99 let source = vec![true, false, false, true];
100
101 let mut bytes = Vec::new();
102 let mut cursor = Cursor::new(&mut bytes);
103
104 source.encode(&mut cursor).await.unwrap();
105
106 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
107 let decoded = Vec::<bool>::decode(&mut cursor).await.unwrap();
108
109 assert_eq!(source, decoded);
110 }
111
112 #[tokio::test]
113 async fn test_string_encode_decode() {
114 {
115 let source = vec!["hello", "", "tonbo", "fusio", "!! @tonbo.io"];
116
117 let mut bytes = Vec::new();
118 let mut cursor = Cursor::new(&mut bytes);
119
120 source.encode(&mut cursor).await.unwrap();
121
122 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
123 let decoded = Vec::<String>::decode(&mut cursor).await.unwrap();
124
125 assert_eq!(source, decoded);
126 }
127
128 {
129 let source = vec![
130 "hello".to_string(),
131 "".to_string(),
132 "tonbo".to_string(),
133 "fusio".to_string(),
134 "!! @tonbo.io".to_string(),
135 ];
136
137 let mut bytes = Vec::new();
138 let mut cursor = Cursor::new(&mut bytes);
139
140 source.encode(&mut cursor).await.unwrap();
141
142 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
143 let decoded = Vec::<String>::decode(&mut cursor).await.unwrap();
144
145 assert_eq!(source, decoded);
146 }
147 }
148
149 #[tokio::test]
150 async fn test_list_encode_decode() {
151 let source = vec![
152 vec![1_u32],
153 vec![1237654, 123],
154 vec![456, 1, 3, 5],
155 vec![123456789],
156 ];
157
158 let mut bytes = Vec::new();
159 let mut cursor = Cursor::new(&mut bytes);
160
161 source.encode(&mut cursor).await.unwrap();
162
163 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
164 let decoded = Vec::<Vec<u32>>::decode(&mut cursor).await.unwrap();
165
166 assert_eq!(source, decoded);
167 }
168
169 #[tokio::test]
170 async fn test_list_opt_encode_decode() {
171 {
172 let source = vec![Some(1_i64), Some(1237654), None, Some(123456789)];
173
174 let mut bytes = Vec::new();
175 let mut cursor = Cursor::new(&mut bytes);
176
177 source.encode(&mut cursor).await.unwrap();
178
179 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
180 let decoded = Vec::<Option<i64>>::decode(&mut cursor).await.unwrap();
181
182 assert_eq!(source, decoded);
183 }
184 {
185 let source = vec![Some("hello"), Some(""), None, None, Some("!! @tonbo.io")];
186
187 let mut bytes = Vec::new();
188 let mut cursor = Cursor::new(&mut bytes);
189
190 source.encode(&mut cursor).await.unwrap();
191
192 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
193 let decoded = Vec::<Option<String>>::decode(&mut cursor).await.unwrap();
194
195 assert_eq!(
196 source,
197 decoded
198 .iter()
199 .map(|v| v.as_deref())
200 .collect::<Vec<Option<&str>>>()
201 );
202 }
203 }
204
205 #[tokio::test]
206 async fn test_encode_decode_empty() {
207 {
208 let source = Vec::<u16>::new();
209
210 let mut bytes = Vec::new();
211 let mut cursor = Cursor::new(&mut bytes);
212
213 source.encode(&mut cursor).await.unwrap();
214
215 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
216 let decoded = Vec::<u16>::decode(&mut cursor).await.unwrap();
217
218 assert_eq!(source, decoded);
219 }
220
221 {
222 let source = Vec::<i32>::new();
223
224 let mut bytes = Vec::new();
225 let mut cursor = Cursor::new(&mut bytes);
226
227 source.encode(&mut cursor).await.unwrap();
228
229 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
230 let decoded = Vec::<i32>::decode(&mut cursor).await.unwrap();
231
232 assert_eq!(source, decoded);
233 }
234
235 {
236 let source = Vec::<String>::new();
237
238 let mut bytes = Vec::new();
239 let mut cursor = Cursor::new(&mut bytes);
240
241 source.encode(&mut cursor).await.unwrap();
242
243 cursor.seek(std::io::SeekFrom::Start(0)).await.unwrap();
244 let decoded = Vec::<String>::decode(&mut cursor).await.unwrap();
245
246 assert_eq!(source, decoded);
247 }
248 }
249}