ark_serialize/impls/
tuples.rs1use crate::{
2 CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
3};
4use ark_std::io::{Read, Write};
5
6macro_rules! impl_tuple {
7 ($( $ty: ident : $no: tt, )*) => {
8 impl<$($ty, )*> Valid for ($($ty,)*) where
9 $($ty: Valid,)*
10 {
11 const TRIVIAL_CHECK: bool = (
12 $( < $ty as Valid >::TRIVIAL_CHECK ) & *
13 );
14
15 #[inline]
16 fn check(&self) -> Result<(), SerializationError> {
17 if Self::TRIVIAL_CHECK {
18 Ok(())
19 } else {
20 $( self.$no.check()?; )*
21 Ok(())
22 }
23 }
24 }
25
26 #[allow(unused)]
27 impl<$($ty, )*> CanonicalSerialize for ($($ty,)*) where
28 $($ty: CanonicalSerialize,)*
29 {
30 #[inline]
31 fn serialize_with_mode<W: Write>(&self, mut writer: W, compress: Compress) -> Result<(), SerializationError> {
32 $(self.$no.serialize_with_mode(&mut writer, compress)?;)*
33 Ok(())
34 }
35
36 #[inline]
37 fn serialized_size(&self, compress: Compress) -> usize {
38 [$(
39 self.$no.serialized_size(compress),
40 )*].iter().sum()
41 }
42 }
43
44 impl<$($ty, )*> CanonicalDeserialize for ($($ty,)*) where
45 $($ty: CanonicalDeserialize,)*
46 {
47 #[inline]
48 #[allow(unused)]
49 fn deserialize_with_mode<R: Read>(mut reader: R, compress: Compress, validate: Validate) -> Result<Self, SerializationError> {
50 Ok(($(
51 $ty::deserialize_with_mode(&mut reader, compress, validate)?,
52 )*))
53 }
54 }
55 }
56}
57
58impl Valid for () {
59 const TRIVIAL_CHECK: bool = true;
60
61 #[inline]
62 fn check(&self) -> Result<(), SerializationError> {
63 Ok(())
64 }
65
66 fn batch_check<'a>(_: impl Iterator<Item = &'a Self> + Send) -> Result<(), SerializationError>
67 where
68 Self: 'a,
69 {
70 Ok(())
71 }
72}
73
74impl CanonicalSerialize for () {
75 #[inline]
76 fn serialize_with_mode<W: Write>(
77 &self,
78 _writer: W,
79 _compress: Compress,
80 ) -> Result<(), SerializationError> {
81 Ok(())
82 }
83
84 #[inline]
85 fn serialized_size(&self, _compress: Compress) -> usize {
86 0
87 }
88}
89
90impl CanonicalDeserialize for () {
91 #[inline]
92 fn deserialize_with_mode<R: Read>(
93 _reader: R,
94 _compress: Compress,
95 _validate: Validate,
96 ) -> Result<Self, SerializationError> {
97 Ok(())
98 }
99}
100
101impl_tuple!(A:0,);
102impl_tuple!(A:0, B:1,);
103impl_tuple!(A:0, B:1, C:2,);
104impl_tuple!(A:0, B:1, C:2, D:3,);
105impl_tuple!(A:0, B:1, C:2, D:3, E:4,);