1#![allow(clippy::question_mark)]
2use crate::{TBuffer, TBytes};
3
4macro_rules! impl_tuple {
5 ($($T: ident | $t: ident | ($($t1: ident),*)),+; $($N: tt),+) => {
6 impl<$($T: TBytes),*> TBytes for ($($T),*){
7 fn size(&self) -> usize{
8 $(self.$N.size() +)* 0
9 }
10
11 fn to_bytes(&self) -> Vec<u8>{
12 let mut buffer = Vec::with_capacity(self.size());
13
14 $(buffer.append(&mut self.$N.to_bytes());)*
15
16 buffer
17 }
18
19 fn from_bytes(buffer: &mut TBuffer) -> Option<Self>{
20 $(let $t = if let Some(value) = $T::from_bytes(buffer) {value}else{
21 $(
22 let mut bytes = $t1.to_bytes();
23 while let Some(byte) = bytes.pop(){
24 buffer.insert(0, byte);
25 }
26 )*
27 return None;
28 };)*
29 Some(($($t),*))
30 }
31
32 }
33 };
34}
35
36impl_tuple!(T1 | t1 | (), T2 | t2 | (t1); 0, 1);
37impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1); 0, 1, 2);
38impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1); 0, 1, 2, 3);
39impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1); 0, 1, 2, 3, 4);
40impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5);
41impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6);
42impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7);
43impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1), T9 | t9 | (t8, t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7, 8);
44impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1), T9 | t9 | (t8, t7, t6, t5, t4, t3, t2, t1), T10 | t10 | (t9, t8, t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
45impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1), T9 | t9 | (t8, t7, t6, t5, t4, t3, t2, t1), T10 | t10 | (t9, t8, t7, t6, t5, t4, t3, t2, t1), T11 | t11 | (t10, t9, t8, t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
46impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1), T9 | t9 | (t8, t7, t6, t5, t4, t3, t2, t1), T10 | t10 | (t9, t8, t7, t6, t5, t4, t3, t2, t1), T11 | t11 | (t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T12 | t12 | (t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
47impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1), T9 | t9 | (t8, t7, t6, t5, t4, t3, t2, t1), T10 | t10 | (t9, t8, t7, t6, t5, t4, t3, t2, t1), T11 | t11 | (t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T12 | t12 | (t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T13 | t13 | (t12, t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
48impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1), T9 | t9 | (t8, t7, t6, t5, t4, t3, t2, t1), T10 | t10 | (t9, t8, t7, t6, t5, t4, t3, t2, t1), T11 | t11 | (t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T12 | t12 | (t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T13 | t13 | (t12, t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T14 | t14 | (t13, t12, t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
49impl_tuple!(T1 | t1 | (), T2 | t2 | (t1), T3 | t3 | (t2, t1), T4 | t4 | (t3, t2, t1), T5 | t5 | (t4, t3, t2, t1), T6 | t6 | (t5, t4, t3, t2, t1), T7 | t7 | (t6, t5, t4, t3, t2, t1), T8 | t8 | (t7, t6, t5, t4, t3, t2, t1), T9 | t9 | (t8, t7, t6, t5, t4, t3, t2, t1), T10 | t10 | (t9, t8, t7, t6, t5, t4, t3, t2, t1), T11 | t11 | (t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T12 | t12 | (t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T13 | t13 | (t12, t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T14 | t14 | (t13, t12, t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1), T15 | t15 | ( t14, t13, t12, t11, t10, t9, t8, t7, t6, t5, t4, t3, t2, t1); 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
50
51#[cfg(test)]
52mod test {
53 use crate::TBytes;
54
55 #[test]
56 fn i32_i64() {
57 let a = (1i32, 53i64);
58
59 let mut bytes = a.to_bytes();
60
61 let b = <(i32, i64)>::from_bytes(&mut bytes).unwrap();
62
63 assert_eq!(a, b);
64 }
65
66 #[test]
67 fn result_i32_i64() {
68 type R = Result<(i32, i64), ()>;
69
70 let a: R = Ok((42i32, 21i64));
71
72 let mut bytes = a.to_bytes();
73
74 let b = R::from_bytes(&mut bytes).unwrap();
75
76 assert_eq!(a, b);
77 }
78
79 #[test]
80 fn incomplete_t2() {
81 let mut buffer = Vec::new();
82 buffer.append(&mut String::from("Lomm").to_bytes());
83 let clone_buffer = buffer.clone();
84
85 let other_buffer = <(String, String)>::from_bytes(&mut buffer);
86 if let Some(other_buffer) = other_buffer {
87 panic!("This should be possible! Other buffer: {other_buffer:?}");
88 }
89
90 assert_eq!(buffer, clone_buffer);
91 buffer.append(&mut String::from("LOP").to_bytes());
92
93 let value = <(String, String)>::from_bytes(&mut buffer).unwrap();
94 assert_eq!(value, (String::from("Lomm"), String::from("LOP")))
95 }
96 #[test]
97 fn incomplete_t15() {
98 let mut buffer = Vec::new();
99 let words = [
100 "Lomm",
101 "Doom",
102 "Work",
103 "I",
104 "Hate",
105 "Regular",
106 "Expression",
107 "I",
108 "Want",
109 "To",
110 "Sleep",
111 "I",
112 "Don't",
113 "Know",
114 "What",
115 "To",
116 "Say",
117 ];
118 let strings = words.iter().map(|w| w.to_string()).collect::<Vec<String>>();
119 for string in &strings[0..14] {
120 buffer.append(&mut string.to_bytes());
121 }
122
123 let clone_buffer = buffer.clone();
124
125 let other_buffer = <(
126 String,
127 String,
128 String,
129 String,
130 String,
131 String,
132 String,
133 String,
134 String,
135 String,
136 String,
137 String,
138 String,
139 String,
140 String,
141 )>::from_bytes(&mut buffer);
142 if other_buffer.is_some() {
143 panic!("This should be possible! Other buffer: {clone_buffer:?}");
144 }
145
146 assert_eq!(buffer, clone_buffer);
147 buffer.append(&mut strings[14].to_bytes());
148
149 let value = <(
150 String,
151 String,
152 String,
153 String,
154 String,
155 String,
156 String,
157 String,
158 String,
159 String,
160 String,
161 String,
162 String,
163 String,
164 String,
165 )>::from_bytes(&mut buffer)
166 .unwrap();
167
168 if value.0 != strings[0] {
169 panic!("Some thing went rong!")
170 }
171 if value.1 != strings[1] {
172 panic!("Some thing went rong!")
173 }
174 if value.2 != strings[2] {
175 panic!("Some thing went rong!")
176 }
177 if value.3 != strings[3] {
178 panic!("Some thing went rong!")
179 }
180 if value.4 != strings[4] {
181 panic!("Some thing went rong!")
182 }
183 if value.5 != strings[5] {
184 panic!("Some thing went rong!")
185 }
186 if value.6 != strings[6] {
187 panic!("Some thing went rong!")
188 }
189 if value.7 != strings[7] {
190 panic!("Some thing went rong!")
191 }
192 if value.8 != strings[8] {
193 panic!("Some thing went rong!")
194 }
195 if value.9 != strings[9] {
196 panic!("Some thing went rong!")
197 }
198 if value.10 != strings[10] {
199 panic!("Some thing went rong!")
200 }
201 if value.11 != strings[11] {
202 panic!("Some thing went rong!")
203 }
204 if value.12 != strings[12] {
205 panic!("Some thing went rong!")
206 }
207 if value.13 != strings[13] {
208 panic!("Some thing went rong!")
209 }
210 if value.14 != strings[14] {
211 panic!("Some thing went rong!")
212 }
213 }
214}