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