1use crate::{
2 CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
3};
4use ark_std::{
5 borrow::*,
6 io::{Read, Write},
7 marker::PhantomData,
8 rc::Rc,
9 vec::*,
10};
11
12impl<T: CanonicalSerialize> CanonicalSerialize for Option<T> {
13 #[inline]
14 fn serialize_with_mode<W: Write>(
15 &self,
16 mut writer: W,
17 compress: Compress,
18 ) -> Result<(), SerializationError> {
19 self.is_some().serialize_with_mode(&mut writer, compress)?;
20 if let Some(item) = self {
21 item.serialize_with_mode(&mut writer, compress)?;
22 }
23
24 Ok(())
25 }
26
27 #[inline]
28 fn serialized_size(&self, compress: Compress) -> usize {
29 1 + self
30 .as_ref()
31 .map(|s| s.serialized_size(compress))
32 .unwrap_or(0)
33 }
34}
35
36impl<T: Valid> Valid for Option<T> {
37 const TRIVIAL_CHECK: bool = T::TRIVIAL_CHECK;
38
39 #[inline]
40 fn check(&self) -> Result<(), SerializationError> {
41 match self {
42 Some(v) => v.check(),
43 None => Ok(()),
44 }
45 }
46
47 #[inline]
48 fn batch_check<'a>(
49 batch: impl Iterator<Item = &'a Self> + Send,
50 ) -> Result<(), SerializationError>
51 where
52 Self: 'a,
53 {
54 match Self::TRIVIAL_CHECK {
55 true => Ok(()),
56 false => T::batch_check(batch.map(Self::as_ref).filter(Option::is_some).flatten()),
57 }
58 }
59}
60
61impl<T: CanonicalDeserialize> CanonicalDeserialize for Option<T> {
62 #[inline]
63 fn deserialize_with_mode<R: Read>(
64 mut reader: R,
65 compress: Compress,
66 validate: Validate,
67 ) -> Result<Self, SerializationError> {
68 let is_some = bool::deserialize_with_mode(&mut reader, compress, validate)?;
69 let data = is_some
70 .then(|| T::deserialize_with_mode(&mut reader, compress, validate))
71 .transpose()?;
72
73 Ok(data)
74 }
75}
76
77impl<T> CanonicalSerialize for PhantomData<T> {
79 #[inline]
80 fn serialize_with_mode<W: Write>(
81 &self,
82 _writer: W,
83 _compress: Compress,
84 ) -> Result<(), SerializationError> {
85 Ok(())
86 }
87
88 #[inline]
89 fn serialized_size(&self, _compress: Compress) -> usize {
90 0
91 }
92}
93
94impl<T: Sync> Valid for PhantomData<T> {
95 const TRIVIAL_CHECK: bool = true;
96
97 #[inline]
98 fn check(&self) -> Result<(), SerializationError> {
99 Ok(())
100 }
101
102 #[inline]
103 fn batch_check<'a>(_: impl Iterator<Item = &'a Self> + Send) -> Result<(), SerializationError>
104 where
105 Self: 'a,
106 {
107 Ok(())
108 }
109}
110
111impl<T: Send + Sync> CanonicalDeserialize for PhantomData<T> {
112 #[inline]
113 fn deserialize_with_mode<R: Read>(
114 _reader: R,
115 _compress: Compress,
116 _validate: Validate,
117 ) -> Result<Self, SerializationError> {
118 Ok(Self)
119 }
120}
121
122impl<T: CanonicalSerialize> CanonicalSerialize for &T {
123 fn serialize_with_mode<W: Write>(
124 &self,
125 writer: W,
126 compress: Compress,
127 ) -> Result<(), SerializationError> {
128 (*self).serialize_with_mode(writer, compress)
129 }
130
131 fn serialized_size(&self, compress: Compress) -> usize {
132 (*self).serialized_size(compress)
133 }
134}
135
136impl<T: CanonicalSerialize> CanonicalSerialize for &mut T {
137 fn serialize_with_mode<W: Write>(
138 &self,
139 writer: W,
140 compress: Compress,
141 ) -> Result<(), SerializationError> {
142 (**self).serialize_with_mode(writer, compress)
143 }
144
145 fn serialized_size(&self, compress: Compress) -> usize {
146 (**self).serialized_size(compress)
147 }
148}
149
150impl<T: ?Sized + CanonicalSerialize + ToOwned> CanonicalSerialize for Rc<T> {
151 #[inline]
152 fn serialize_with_mode<W: Write>(
153 &self,
154 mut writer: W,
155 compress: Compress,
156 ) -> Result<(), SerializationError> {
157 self.as_ref().serialize_with_mode(&mut writer, compress)
158 }
159
160 #[inline]
161 fn serialized_size(&self, compress: Compress) -> usize {
162 self.as_ref().serialized_size(compress)
163 }
164}
165
166#[cfg(target_has_atomic = "ptr")]
167impl<T: ?Sized + CanonicalSerialize + ToOwned> CanonicalSerialize for ark_std::sync::Arc<T> {
168 #[inline]
169 fn serialize_with_mode<W: Write>(
170 &self,
171 mut writer: W,
172 compress: Compress,
173 ) -> Result<(), SerializationError> {
174 self.as_ref().serialize_with_mode(&mut writer, compress)
175 }
176
177 #[inline]
178 fn serialized_size(&self, compress: Compress) -> usize {
179 self.as_ref().serialized_size(compress)
180 }
181}
182
183#[cfg(target_has_atomic = "ptr")]
184impl<T: ?Sized + Valid + Sync + Send> Valid for ark_std::sync::Arc<T> {
185 const TRIVIAL_CHECK: bool = T::TRIVIAL_CHECK;
186
187 #[inline]
188 fn check(&self) -> Result<(), SerializationError> {
189 self.as_ref().check()
190 }
191
192 #[inline]
193 fn batch_check<'a>(
194 batch: impl Iterator<Item = &'a Self> + Send,
195 ) -> Result<(), SerializationError>
196 where
197 Self: 'a,
198 {
199 match T::TRIVIAL_CHECK {
200 true => Ok(()),
201 false => T::batch_check(batch.map(|v| v.as_ref())),
202 }
203 }
204}
205
206#[cfg(target_has_atomic = "ptr")]
207impl<T: CanonicalDeserialize + ToOwned + Sync + Send> CanonicalDeserialize
208 for ark_std::sync::Arc<T>
209{
210 #[inline]
211 fn deserialize_with_mode<R: Read>(
212 reader: R,
213 compress: Compress,
214 validate: Validate,
215 ) -> Result<Self, SerializationError> {
216 T::deserialize_with_mode(reader, compress, validate).map(Self::new)
217 }
218}
219
220impl<T: ?Sized + CanonicalSerialize + ToOwned> CanonicalSerialize for Cow<'_, T> {
221 #[inline]
222 fn serialize_with_mode<W: Write>(
223 &self,
224 mut writer: W,
225 compress: Compress,
226 ) -> Result<(), SerializationError> {
227 self.as_ref().serialize_with_mode(&mut writer, compress)
228 }
229
230 #[inline]
231 fn serialized_size(&self, compress: Compress) -> usize {
232 self.as_ref().serialized_size(compress)
233 }
234}
235
236impl<T> Valid for Cow<'_, T>
237where
238 T: ?Sized + ToOwned + Sync + Valid + Send,
239 <T as ToOwned>::Owned: CanonicalDeserialize + Send,
240{
241 const TRIVIAL_CHECK: bool = <<T as ToOwned>::Owned>::TRIVIAL_CHECK;
242 #[inline]
243 fn check(&self) -> Result<(), SerializationError> {
244 match Self::TRIVIAL_CHECK {
245 true => Ok(()),
246 false => <<T as ToOwned>::Owned>::check(&self.as_ref().to_owned()),
247 }
248 }
249
250 #[inline]
251 fn batch_check<'a>(
252 batch: impl Iterator<Item = &'a Self> + Send,
253 ) -> Result<(), SerializationError>
254 where
255 Self: 'a,
256 {
257 match Self::TRIVIAL_CHECK {
258 true => Ok(()),
259 false => {
260 let t: Vec<_> = batch.map(|v| v.as_ref().to_owned()).collect();
261 <<T as ToOwned>::Owned>::batch_check(t.iter())
262 },
263 }
264 }
265}
266
267impl<T> CanonicalDeserialize for Cow<'_, T>
268where
269 T: ?Sized + ToOwned + Valid + Sync + Send,
270 <T as ToOwned>::Owned: CanonicalDeserialize + Valid + Send,
271{
272 #[inline]
273 fn deserialize_with_mode<R: Read>(
274 reader: R,
275 compress: Compress,
276 validate: Validate,
277 ) -> Result<Self, SerializationError> {
278 Ok(Cow::Owned(<T as ToOwned>::Owned::deserialize_with_mode(
279 reader, compress, validate,
280 )?))
281 }
282}