wire_framed_core/
common_impls.rs

1use std::hash::Hash;
2use std::collections::HashSet;
3use bytes::BufMut;
4
5use super::*;
6
7impl FromFrame for bool {
8	type Error = std::io::Error;
9
10	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
11		utils::get_bool(frame, "bool")
12	}
13}
14
15impl FromFrame for u8 {
16	type Error = std::io::Error;
17
18	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
19		utils::get_u8(frame, "u8")
20	}
21}
22
23impl FromFrame for u16 {
24	type Error = std::io::Error;
25
26	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
27		utils::get_u16(frame, "u16")
28	}
29}
30
31impl FromFrame for u32 {
32	type Error = std::io::Error;
33
34	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
35		utils::get_u32(frame, "u32")
36	}
37}
38
39impl FromFrame for u64 {
40	type Error = std::io::Error;
41
42	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
43		utils::get_u64(frame, "u64")
44	}
45}
46
47impl FromFrame for i8 {
48	type Error = std::io::Error;
49
50	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
51		utils::get_i8(frame, "i8")
52	}
53}
54
55impl FromFrame for i16 {
56	type Error = std::io::Error;
57
58	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
59		utils::get_i16(frame, "i16")
60	}
61}
62
63impl FromFrame for i32 {
64	type Error = std::io::Error;
65
66	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
67		utils::get_i32(frame, "i32")
68	}
69}
70
71impl FromFrame for i64 {
72	type Error = std::io::Error;
73
74	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
75		utils::get_i64(frame, "i64")
76	}
77}
78
79
80impl FromFrame for String {
81	type Error = std::io::Error;
82
83	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
84		utils::get_string(frame, "string")
85	}
86}
87
88impl<T> FromFrame for Option<T>
89where
90	T: FromFrame,
91	<T as FromFrame>::Error: Into<std::io::Error>,
92{
93	type Error = std::io::Error;
94
95	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
96		utils::get_option(frame, "option", |frame| <T as FromFrame>::parse_frame(frame).map_err(Into::into))
97	}
98}
99
100impl<T> FromFrame for Vec<T>
101where
102	T: FromFrame,
103	<T as FromFrame>::Error: Into<std::io::Error>,
104{
105	type Error = std::io::Error;
106
107	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
108		utils::get_array(frame, "array", |frame| <T as FromFrame>::parse_frame(frame).map_err(Into::into))
109	}
110}
111
112impl<T> FromFrame for HashSet<T>
113where
114	T: FromFrame + PartialEq + Eq + Hash,
115	<T as FromFrame>::Error: Into<std::io::Error>,
116{
117	type Error = std::io::Error;
118
119	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
120		utils::get_hashset(frame, "hashset", |frame| <T as FromFrame>::parse_frame(frame).map_err(Into::into))
121	}
122}
123
124impl IntoFrame for bool {
125	fn extend_frame(&self, frame: &mut BytesMut) {
126		frame.put_u8(*self as u8)
127	}
128}
129
130impl IntoFrame for u8 {
131	fn extend_frame(&self, frame: &mut BytesMut) {
132		frame.put_u8(*self)
133	}
134}
135
136impl IntoFrame for u16 {
137	fn extend_frame(&self, frame: &mut BytesMut) {
138		frame.put_u16(*self)
139	}
140}
141
142impl IntoFrame for u32 {
143	fn extend_frame(&self, frame: &mut BytesMut) {
144		frame.put_u32(*self)
145	}
146}
147
148impl IntoFrame for u64 {
149	fn extend_frame(&self, frame: &mut BytesMut) {
150		frame.put_u64(*self)
151	}
152}
153
154impl IntoFrame for i8 {
155	fn extend_frame(&self, frame: &mut BytesMut) {
156		frame.put_i8(*self)
157	}
158}
159
160impl IntoFrame for i16 {
161	fn extend_frame(&self, frame: &mut BytesMut) {
162		frame.put_i16(*self)
163	}
164}
165
166impl IntoFrame for i32 {
167	fn extend_frame(&self, frame: &mut BytesMut) {
168		frame.put_i32(*self)
169	}
170}
171
172impl IntoFrame for i64 {
173	fn extend_frame(&self, frame: &mut BytesMut) {
174		frame.put_i64(*self)
175	}
176}
177
178impl IntoFrame for &str {
179	fn extend_frame(&self, frame: &mut BytesMut) {
180		frame.put_u32(self.len() as u32);
181		frame.put_slice(self.as_bytes());
182	}
183
184	fn size_hint(&self) -> usize {
185		4 + self.len()
186	}
187}
188
189impl IntoFrame for String {
190	fn extend_frame(&self, frame: &mut BytesMut) {
191		<&str as IntoFrame>::extend_frame(&self.as_str(), frame)
192	}
193
194	fn size_hint(&self) -> usize {
195		<&str as IntoFrame>::size_hint(&self.as_str())
196	}
197}
198
199impl<T: IntoFrame> IntoFrame for Option<T> {
200	fn extend_frame(&self, frame: &mut BytesMut) {
201		utils::put_option(frame, self, |frame, value| <T as IntoFrame>::extend_frame(value, frame));
202	}
203
204	fn size_hint(&self) -> usize {
205		1 + self.as_ref().map(|value| value.size_hint()).unwrap_or(0)
206	}
207}
208
209impl<T: IntoFrame> IntoFrame for Vec<T> {
210	fn extend_frame(&self, frame: &mut BytesMut) {
211		utils::put_array(frame, self, |frame, value| <T as IntoFrame>::extend_frame(value, frame));
212	}
213
214	fn size_hint(&self) -> usize {
215		4 + self.iter().map(|value| value.size_hint()).sum::<usize>()
216	}
217}
218
219impl<T: IntoFrame + PartialEq + Eq + Hash> IntoFrame for HashSet<T> {
220	fn extend_frame(&self, frame: &mut BytesMut) {
221		utils::put_hashset(frame, self, |frame, value| <T as IntoFrame>::extend_frame(value, frame));
222	}
223
224	fn size_hint(&self) -> usize {
225		4 + self.iter().map(|value| value.size_hint()).sum::<usize>()
226	}
227}
228
229impl<T1, T2> IntoFrame for (T1, T2)
230where
231	T1: IntoFrame,
232	T2: IntoFrame,
233{
234	fn extend_frame(&self, frame: &mut BytesMut) {
235		self.0.extend_frame(frame);
236		self.1.extend_frame(frame);
237	}
238
239	fn size_hint(&self) -> usize {
240		self.0.size_hint() + self.1.size_hint()
241	}
242}
243
244impl<T1, T2, T3> IntoFrame for (T1, T2, T3)
245where
246	T1: IntoFrame,
247	T2: IntoFrame,
248	T3: IntoFrame,
249{
250	fn extend_frame(&self, frame: &mut BytesMut) {
251		self.0.extend_frame(frame);
252		self.1.extend_frame(frame);
253		self.2.extend_frame(frame);
254	}
255
256	fn size_hint(&self) -> usize {
257		self.0.size_hint() + self.1.size_hint() + self.2.size_hint()
258	}
259}
260
261impl<T1, T2, T3, T4> IntoFrame for (T1, T2, T3, T4)
262where
263	T1: IntoFrame,
264	T2: IntoFrame,
265	T3: IntoFrame,
266	T4: IntoFrame,
267{
268	fn extend_frame(&self, frame: &mut BytesMut) {
269		self.0.extend_frame(frame);
270		self.1.extend_frame(frame);
271		self.2.extend_frame(frame);
272		self.3.extend_frame(frame);
273	}
274
275	fn size_hint(&self) -> usize {
276		self.0.size_hint() + self.1.size_hint() + self.2.size_hint() + self.3.size_hint()
277	}
278}
279
280impl<T1, T2, T3, T4, T5> IntoFrame for (T1, T2, T3, T4, T5)
281where
282	T1: IntoFrame,
283	T2: IntoFrame,
284	T3: IntoFrame,
285	T4: IntoFrame,
286	T5: IntoFrame,
287{
288	fn extend_frame(&self, frame: &mut BytesMut) {
289		self.0.extend_frame(frame);
290		self.1.extend_frame(frame);
291		self.2.extend_frame(frame);
292		self.3.extend_frame(frame);
293		self.4.extend_frame(frame);
294	}
295
296	fn size_hint(&self) -> usize {
297		self.0.size_hint() + self.1.size_hint() + self.2.size_hint() + self.3.size_hint() + self.4.size_hint()
298	}
299}
300
301impl<T1, T2, T3, T4, T5, T6> IntoFrame for (T1, T2, T3, T4, T5, T6)
302where
303	T1: IntoFrame,
304	T2: IntoFrame,
305	T3: IntoFrame,
306	T4: IntoFrame,
307	T5: IntoFrame,
308	T6: IntoFrame,
309{
310	fn extend_frame(&self, frame: &mut BytesMut) {
311		self.0.extend_frame(frame);
312		self.1.extend_frame(frame);
313		self.2.extend_frame(frame);
314		self.3.extend_frame(frame);
315		self.4.extend_frame(frame);
316		self.5.extend_frame(frame);
317	}
318
319	fn size_hint(&self) -> usize {
320		self.0.size_hint() + self.1.size_hint() + self.2.size_hint() + self.3.size_hint() + self.4.size_hint() + self.5.size_hint()
321	}
322}
323
324impl<T1, T2, T3, T4, T5, T6, T7> IntoFrame for (T1, T2, T3, T4, T5, T6, T7)
325where
326	T1: IntoFrame,
327	T2: IntoFrame,
328	T3: IntoFrame,
329	T4: IntoFrame,
330	T5: IntoFrame,
331	T6: IntoFrame,
332	T7: IntoFrame,
333{
334	fn extend_frame(&self, frame: &mut BytesMut) {
335		self.0.extend_frame(frame);
336		self.1.extend_frame(frame);
337		self.2.extend_frame(frame);
338		self.3.extend_frame(frame);
339		self.4.extend_frame(frame);
340		self.5.extend_frame(frame);
341		self.6.extend_frame(frame);
342	}
343
344	fn size_hint(&self) -> usize {
345		self.0.size_hint() + self.1.size_hint() + self.2.size_hint() + self.3.size_hint() + self.4.size_hint() + self.5.size_hint() + self.6.size_hint()
346	}
347}
348
349impl<T1, T2, T3, T4, T5, T6, T7, T8> IntoFrame for (T1, T2, T3, T4, T5, T6, T7, T8)
350where
351	T1: IntoFrame,
352	T2: IntoFrame,
353	T3: IntoFrame,
354	T4: IntoFrame,
355	T5: IntoFrame,
356	T6: IntoFrame,
357	T7: IntoFrame,
358	T8: IntoFrame,
359{
360	fn extend_frame(&self, frame: &mut BytesMut) {
361		self.0.extend_frame(frame);
362		self.1.extend_frame(frame);
363		self.2.extend_frame(frame);
364		self.3.extend_frame(frame);
365		self.4.extend_frame(frame);
366		self.5.extend_frame(frame);
367		self.6.extend_frame(frame);
368		self.7.extend_frame(frame);
369	}
370
371	fn size_hint(&self) -> usize {
372		self.0.size_hint() + self.1.size_hint() + self.2.size_hint() + self.3.size_hint() + self.4.size_hint() + self.5.size_hint() + self.6.size_hint() + self.7.size_hint()
373	}
374}
375
376impl<T1, T2> FromFrame for (T1, T2)
377where
378	T1: FromFrame,
379	T2: FromFrame,
380	<T1 as FromFrame>::Error: Into<std::io::Error>,
381	<T2 as FromFrame>::Error: Into<std::io::Error>,
382{
383	type Error = std::io::Error;
384
385	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
386		Ok((T1::parse_frame(frame).map_err(Into::into)?, T2::parse_frame(frame).map_err(Into::into)?))
387	}
388}
389
390impl<T1, T2, T3> FromFrame for (T1, T2, T3)
391where
392	T1: FromFrame,
393	T2: FromFrame,
394	T3: FromFrame,
395	<T1 as FromFrame>::Error: Into<std::io::Error>,
396	<T2 as FromFrame>::Error: Into<std::io::Error>,
397	<T3 as FromFrame>::Error: Into<std::io::Error>,
398{
399	type Error = std::io::Error;
400
401	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
402		Ok((T1::parse_frame(frame).map_err(Into::into)?, T2::parse_frame(frame).map_err(Into::into)?, T3::parse_frame(frame).map_err(Into::into)?))
403	}
404}
405
406impl<T1, T2, T3, T4> FromFrame for (T1, T2, T3, T4)
407where
408	T1: FromFrame,
409	T2: FromFrame,
410	T3: FromFrame,
411	T4: FromFrame,
412	<T1 as FromFrame>::Error: Into<std::io::Error>,
413	<T2 as FromFrame>::Error: Into<std::io::Error>,
414	<T3 as FromFrame>::Error: Into<std::io::Error>,
415	<T4 as FromFrame>::Error: Into<std::io::Error>,
416{
417	type Error = std::io::Error;
418
419	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
420		Ok((T1::parse_frame(frame).map_err(Into::into)?, T2::parse_frame(frame).map_err(Into::into)?, T3::parse_frame(frame).map_err(Into::into)?, T4::parse_frame(frame).map_err(Into::into)?))
421	}
422}
423
424impl<T1, T2, T3, T4, T5> FromFrame for (T1, T2, T3, T4, T5)
425where
426	T1: FromFrame,
427	T2: FromFrame,
428	T3: FromFrame,
429	T4: FromFrame,
430	T5: FromFrame,
431	<T1 as FromFrame>::Error: Into<std::io::Error>,
432	<T2 as FromFrame>::Error: Into<std::io::Error>,
433	<T3 as FromFrame>::Error: Into<std::io::Error>,
434	<T4 as FromFrame>::Error: Into<std::io::Error>,
435	<T5 as FromFrame>::Error: Into<std::io::Error>,
436{
437	type Error = std::io::Error;
438
439	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
440		Ok((T1::parse_frame(frame).map_err(Into::into)?, T2::parse_frame(frame).map_err(Into::into)?, T3::parse_frame(frame).map_err(Into::into)?, T4::parse_frame(frame).map_err(Into::into)?, T5::parse_frame(frame).map_err(Into::into)?))
441	}
442}
443
444impl<T1, T2, T3, T4, T5, T6> FromFrame for (T1, T2, T3, T4, T5, T6)
445where
446	T1: FromFrame,
447	T2: FromFrame,
448	T3: FromFrame,
449	T4: FromFrame,
450	T5: FromFrame,
451	T6: FromFrame,
452	<T1 as FromFrame>::Error: Into<std::io::Error>,
453	<T2 as FromFrame>::Error: Into<std::io::Error>,
454	<T3 as FromFrame>::Error: Into<std::io::Error>,
455	<T4 as FromFrame>::Error: Into<std::io::Error>,
456	<T5 as FromFrame>::Error: Into<std::io::Error>,
457	<T6 as FromFrame>::Error: Into<std::io::Error>,
458{
459	type Error = std::io::Error;
460
461	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
462		Ok((T1::parse_frame(frame).map_err(Into::into)?, T2::parse_frame(frame).map_err(Into::into)?, T3::parse_frame(frame).map_err(Into::into)?, T4::parse_frame(frame).map_err(Into::into)?, T5::parse_frame(frame).map_err(Into::into)?, T6::parse_frame(frame).map_err(Into::into)?))
463	}
464}
465
466impl<T1, T2, T3, T4, T5, T6, T7> FromFrame for (T1, T2, T3, T4, T5, T6, T7)
467where
468	T1: FromFrame,
469	T2: FromFrame,
470	T3: FromFrame,
471	T4: FromFrame,
472	T5: FromFrame,
473	T6: FromFrame,
474	T7: FromFrame,
475	<T1 as FromFrame>::Error: Into<std::io::Error>,
476	<T2 as FromFrame>::Error: Into<std::io::Error>,
477	<T3 as FromFrame>::Error: Into<std::io::Error>,
478	<T4 as FromFrame>::Error: Into<std::io::Error>,
479	<T5 as FromFrame>::Error: Into<std::io::Error>,
480	<T6 as FromFrame>::Error: Into<std::io::Error>,
481	<T7 as FromFrame>::Error: Into<std::io::Error>,
482{
483	type Error = std::io::Error;
484
485	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
486		Ok((T1::parse_frame(frame).map_err(Into::into)?, T2::parse_frame(frame).map_err(Into::into)?, T3::parse_frame(frame).map_err(Into::into)?, T4::parse_frame(frame).map_err(Into::into)?, T5::parse_frame(frame).map_err(Into::into)?, T6::parse_frame(frame).map_err(Into::into)?, T7::parse_frame(frame).map_err(Into::into)?))
487	}
488}
489
490impl<T1, T2, T3, T4, T5, T6, T7, T8> FromFrame for (T1, T2, T3, T4, T5, T6, T7, T8)
491where
492	T1: FromFrame,
493	T2: FromFrame,
494	T3: FromFrame,
495	T4: FromFrame,
496	T5: FromFrame,
497	T6: FromFrame,
498	T7: FromFrame,
499	T8: FromFrame,
500	<T1 as FromFrame>::Error: Into<std::io::Error>,
501	<T2 as FromFrame>::Error: Into<std::io::Error>,
502	<T3 as FromFrame>::Error: Into<std::io::Error>,
503	<T4 as FromFrame>::Error: Into<std::io::Error>,
504	<T5 as FromFrame>::Error: Into<std::io::Error>,
505	<T6 as FromFrame>::Error: Into<std::io::Error>,
506	<T7 as FromFrame>::Error: Into<std::io::Error>,
507	<T8 as FromFrame>::Error: Into<std::io::Error>,
508{
509	type Error = std::io::Error;
510
511	fn parse_frame(frame: &mut Bytes) -> Result<Self, Self::Error> {
512		Ok((T1::parse_frame(frame).map_err(Into::into)?, T2::parse_frame(frame).map_err(Into::into)?, T3::parse_frame(frame).map_err(Into::into)?, T4::parse_frame(frame).map_err(Into::into)?, T5::parse_frame(frame).map_err(Into::into)?, T6::parse_frame(frame).map_err(Into::into)?, T7::parse_frame(frame).map_err(Into::into)?, T8::parse_frame(frame).map_err(Into::into)?))
513	}
514}