1 | use core::convert::TryFrom; |
2 | use core::marker::PhantomData; |
3 | |
4 | use crate::__private::maybestd::{ |
5 | borrow::{Cow, ToOwned}, |
6 | boxed::Box, |
7 | collections::{BTreeMap, BTreeSet, LinkedList, VecDeque}, |
8 | string::String, |
9 | vec::Vec, |
10 | }; |
11 | use crate::error::check_zst; |
12 | use crate::io::{Error, ErrorKind, Result, Write}; |
13 | |
14 | pub(crate) mod helpers; |
15 | |
16 | const FLOAT_NAN_ERR: &str = "For portability reasons we do not allow to serialize NaNs." ; |
17 | |
18 | /// A data-structure that can be serialized into binary format by NBOR. |
19 | /// |
20 | /// ``` |
21 | /// use borsh::BorshSerialize; |
22 | /// |
23 | /// /// derive is only available if borsh is built with `features = ["derive"]` |
24 | /// # #[cfg (feature = "derive" )] |
25 | /// #[derive(BorshSerialize)] |
26 | /// struct MyBorshSerializableStruct { |
27 | /// value: String, |
28 | /// } |
29 | /// |
30 | /// |
31 | /// # #[cfg (feature = "derive" )] |
32 | /// let x = MyBorshSerializableStruct { value: "hello" .to_owned() }; |
33 | /// let mut buffer: Vec<u8> = Vec::new(); |
34 | /// # #[cfg (feature = "derive" )] |
35 | /// x.serialize(&mut buffer).unwrap(); |
36 | /// # #[cfg (feature = "derive" )] |
37 | /// let single_serialized_buffer_len = buffer.len(); |
38 | /// |
39 | /// # #[cfg (feature = "derive" )] |
40 | /// x.serialize(&mut buffer).unwrap(); |
41 | /// # #[cfg (feature = "derive" )] |
42 | /// assert_eq!(buffer.len(), single_serialized_buffer_len * 2); |
43 | /// |
44 | /// # #[cfg (feature = "derive" )] |
45 | /// let mut buffer: Vec<u8> = vec![0; 1024 + single_serialized_buffer_len]; |
46 | /// # #[cfg (feature = "derive" )] |
47 | /// let mut buffer_slice_enough_for_the_data = &mut buffer[1024..1024 + single_serialized_buffer_len]; |
48 | /// # #[cfg (feature = "derive" )] |
49 | /// x.serialize(&mut buffer_slice_enough_for_the_data).unwrap(); |
50 | /// ``` |
51 | pub trait BorshSerialize { |
52 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()>; |
53 | |
54 | #[inline ] |
55 | #[doc (hidden)] |
56 | fn u8_slice(slice: &[Self]) -> Option<&[u8]> |
57 | where |
58 | Self: Sized, |
59 | { |
60 | let _ = slice; |
61 | None |
62 | } |
63 | } |
64 | |
65 | impl BorshSerialize for u8 { |
66 | #[inline ] |
67 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
68 | writer.write_all(buf:core::slice::from_ref(self)) |
69 | } |
70 | |
71 | #[inline ] |
72 | fn u8_slice(slice: &[Self]) -> Option<&[u8]> { |
73 | Some(slice) |
74 | } |
75 | } |
76 | |
77 | macro_rules! impl_for_integer { |
78 | ($type: ident) => { |
79 | impl BorshSerialize for $type { |
80 | #[inline] |
81 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
82 | let bytes = self.to_le_bytes(); |
83 | writer.write_all(&bytes) |
84 | } |
85 | } |
86 | }; |
87 | } |
88 | |
89 | impl_for_integer!(i8); |
90 | impl_for_integer!(i16); |
91 | impl_for_integer!(i32); |
92 | impl_for_integer!(i64); |
93 | impl_for_integer!(i128); |
94 | impl_for_integer!(u16); |
95 | impl_for_integer!(u32); |
96 | impl_for_integer!(u64); |
97 | impl_for_integer!(u128); |
98 | |
99 | macro_rules! impl_for_nonzero_integer { |
100 | ($type: ty) => { |
101 | impl BorshSerialize for $type { |
102 | #[inline] |
103 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
104 | BorshSerialize::serialize(&self.get(), writer) |
105 | } |
106 | } |
107 | }; |
108 | } |
109 | |
110 | impl_for_nonzero_integer!(core::num::NonZeroI8); |
111 | impl_for_nonzero_integer!(core::num::NonZeroI16); |
112 | impl_for_nonzero_integer!(core::num::NonZeroI32); |
113 | impl_for_nonzero_integer!(core::num::NonZeroI64); |
114 | impl_for_nonzero_integer!(core::num::NonZeroI128); |
115 | impl_for_nonzero_integer!(core::num::NonZeroU8); |
116 | impl_for_nonzero_integer!(core::num::NonZeroU16); |
117 | impl_for_nonzero_integer!(core::num::NonZeroU32); |
118 | impl_for_nonzero_integer!(core::num::NonZeroU64); |
119 | impl_for_nonzero_integer!(core::num::NonZeroU128); |
120 | impl_for_nonzero_integer!(core::num::NonZeroUsize); |
121 | |
122 | impl BorshSerialize for isize { |
123 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
124 | BorshSerialize::serialize(&(*self as i64), writer) |
125 | } |
126 | } |
127 | |
128 | impl BorshSerialize for usize { |
129 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
130 | BorshSerialize::serialize(&(*self as u64), writer) |
131 | } |
132 | } |
133 | |
134 | // Note NaNs have a portability issue. Specifically, signalling NaNs on MIPS are quiet NaNs on x86, |
135 | // and vice-versa. We disallow NaNs to avoid this issue. |
136 | macro_rules! impl_for_float { |
137 | ($type: ident) => { |
138 | impl BorshSerialize for $type { |
139 | #[inline] |
140 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
141 | if self.is_nan() { |
142 | return Err(Error::new(ErrorKind::InvalidData, FLOAT_NAN_ERR)); |
143 | } |
144 | writer.write_all(&self.to_bits().to_le_bytes()) |
145 | } |
146 | } |
147 | }; |
148 | } |
149 | |
150 | impl_for_float!(f32); |
151 | impl_for_float!(f64); |
152 | |
153 | impl BorshSerialize for bool { |
154 | #[inline ] |
155 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
156 | (u8::from(*self)).serialize(writer) |
157 | } |
158 | } |
159 | |
160 | impl<T> BorshSerialize for Option<T> |
161 | where |
162 | T: BorshSerialize, |
163 | { |
164 | #[inline ] |
165 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
166 | match self { |
167 | None => 0u8.serialize(writer), |
168 | Some(value: &T) => { |
169 | 1u8.serialize(writer)?; |
170 | value.serialize(writer) |
171 | } |
172 | } |
173 | } |
174 | } |
175 | |
176 | impl<T, E> BorshSerialize for core::result::Result<T, E> |
177 | where |
178 | T: BorshSerialize, |
179 | E: BorshSerialize, |
180 | { |
181 | #[inline ] |
182 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
183 | match self { |
184 | Err(e: &E) => { |
185 | 0u8.serialize(writer)?; |
186 | e.serialize(writer) |
187 | } |
188 | Ok(v: &T) => { |
189 | 1u8.serialize(writer)?; |
190 | v.serialize(writer) |
191 | } |
192 | } |
193 | } |
194 | } |
195 | |
196 | impl BorshSerialize for str { |
197 | #[inline ] |
198 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
199 | self.as_bytes().serialize(writer) |
200 | } |
201 | } |
202 | |
203 | impl BorshSerialize for String { |
204 | #[inline ] |
205 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
206 | self.as_bytes().serialize(writer) |
207 | } |
208 | } |
209 | |
210 | /// Module is available if borsh is built with `features = ["ascii"]`. |
211 | #[cfg (feature = "ascii" )] |
212 | pub mod ascii { |
213 | //! |
214 | //! Module defines [BorshSerialize] implementation for |
215 | //! some types from [ascii](::ascii) crate. |
216 | use super::BorshSerialize; |
217 | use crate::io::{Result, Write}; |
218 | |
219 | impl BorshSerialize for ascii::AsciiChar { |
220 | #[inline ] |
221 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
222 | self.as_byte().serialize(writer) |
223 | } |
224 | } |
225 | |
226 | impl BorshSerialize for ascii::AsciiStr { |
227 | #[inline ] |
228 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
229 | self.as_bytes().serialize(writer) |
230 | } |
231 | } |
232 | |
233 | impl BorshSerialize for ascii::AsciiString { |
234 | #[inline ] |
235 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
236 | self.as_bytes().serialize(writer) |
237 | } |
238 | } |
239 | } |
240 | |
241 | /// Helper method that is used to serialize a slice of data (without the length marker). |
242 | #[inline ] |
243 | fn serialize_slice<T: BorshSerialize, W: Write>(data: &[T], writer: &mut W) -> Result<()> { |
244 | if let Some(u8_slice: &[u8]) = T::u8_slice(data) { |
245 | writer.write_all(buf:u8_slice)?; |
246 | } else { |
247 | for item: &T in data { |
248 | item.serialize(writer)?; |
249 | } |
250 | } |
251 | Ok(()) |
252 | } |
253 | |
254 | impl<T> BorshSerialize for [T] |
255 | where |
256 | T: BorshSerialize, |
257 | { |
258 | #[inline ] |
259 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
260 | writer.write_all( |
261 | &(u32::try_from(self.len()).map_err(|_| ErrorKind::InvalidData)?).to_le_bytes(), |
262 | )?; |
263 | serialize_slice(self, writer) |
264 | } |
265 | } |
266 | |
267 | impl<T: BorshSerialize + ?Sized> BorshSerialize for &T { |
268 | #[inline ] |
269 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
270 | (*self).serialize(writer) |
271 | } |
272 | } |
273 | |
274 | impl<T> BorshSerialize for Cow<'_, T> |
275 | where |
276 | T: BorshSerialize + ToOwned + ?Sized, |
277 | { |
278 | #[inline ] |
279 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
280 | self.as_ref().serialize(writer) |
281 | } |
282 | } |
283 | |
284 | impl<T> BorshSerialize for Vec<T> |
285 | where |
286 | T: BorshSerialize, |
287 | { |
288 | #[inline ] |
289 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
290 | check_zst::<T>()?; |
291 | |
292 | self.as_slice().serialize(writer) |
293 | } |
294 | } |
295 | |
296 | #[cfg (feature = "bytes" )] |
297 | impl BorshSerialize for bytes::Bytes { |
298 | #[inline ] |
299 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
300 | self.as_ref().serialize(writer) |
301 | } |
302 | } |
303 | |
304 | #[cfg (feature = "bytes" )] |
305 | impl BorshSerialize for bytes::BytesMut { |
306 | #[inline ] |
307 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
308 | self.as_ref().serialize(writer) |
309 | } |
310 | } |
311 | |
312 | #[cfg (feature = "bson" )] |
313 | impl BorshSerialize for bson::oid::ObjectId { |
314 | #[inline ] |
315 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
316 | self.bytes().serialize(writer) |
317 | } |
318 | } |
319 | |
320 | #[cfg (feature = "indexmap" )] |
321 | // Taken from https://github.com/indexmap-rs/indexmap/blob/dd06e5773e4f91748396c67d00c83637f5c0dd49/src/borsh.rs#L74C1-L86C2 |
322 | // license: MIT OR Apache-2.0 |
323 | impl<T, S> BorshSerialize for indexmap::IndexSet<T, S> |
324 | where |
325 | T: BorshSerialize, |
326 | { |
327 | #[inline ] |
328 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
329 | check_zst::<T>()?; |
330 | |
331 | let iterator = self.iter(); |
332 | |
333 | u32::try_from(iterator.len()) |
334 | .map_err(|_| ErrorKind::InvalidData)? |
335 | .serialize(writer)?; |
336 | |
337 | for item in iterator { |
338 | item.serialize(writer)?; |
339 | } |
340 | |
341 | Ok(()) |
342 | } |
343 | } |
344 | |
345 | #[cfg (feature = "indexmap" )] |
346 | // Taken from https://github.com/indexmap-rs/indexmap/blob/dd06e5773e4f91748396c67d00c83637f5c0dd49/src/borsh.rs#L15 |
347 | // license: MIT OR Apache-2.0 |
348 | impl<K, V, S> BorshSerialize for indexmap::IndexMap<K, V, S> |
349 | where |
350 | K: BorshSerialize, |
351 | V: BorshSerialize, |
352 | { |
353 | #[inline ] |
354 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
355 | check_zst::<K>()?; |
356 | |
357 | let iterator = self.iter(); |
358 | |
359 | u32::try_from(iterator.len()) |
360 | .map_err(|_| ErrorKind::InvalidData)? |
361 | .serialize(writer)?; |
362 | |
363 | for (key, value) in iterator { |
364 | key.serialize(writer)?; |
365 | value.serialize(writer)?; |
366 | } |
367 | |
368 | Ok(()) |
369 | } |
370 | } |
371 | |
372 | impl<T> BorshSerialize for VecDeque<T> |
373 | where |
374 | T: BorshSerialize, |
375 | { |
376 | #[inline ] |
377 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
378 | check_zst::<T>()?; |
379 | |
380 | writer.write_all( |
381 | &(u32::try_from(self.len()).map_err(|_| ErrorKind::InvalidData)?).to_le_bytes(), |
382 | )?; |
383 | let slices: (&[T], &[T]) = self.as_slices(); |
384 | serialize_slice(data:slices.0, writer)?; |
385 | serialize_slice(data:slices.1, writer) |
386 | } |
387 | } |
388 | |
389 | impl<T> BorshSerialize for LinkedList<T> |
390 | where |
391 | T: BorshSerialize, |
392 | { |
393 | #[inline ] |
394 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
395 | check_zst::<T>()?; |
396 | |
397 | writer.write_all( |
398 | &(u32::try_from(self.len()).map_err(|_| ErrorKind::InvalidData)?).to_le_bytes(), |
399 | )?; |
400 | for item: &T in self { |
401 | item.serialize(writer)?; |
402 | } |
403 | Ok(()) |
404 | } |
405 | } |
406 | |
407 | /// Module is available if borsh is built with `features = ["std"]` or `features = ["hashbrown"]`. |
408 | /// |
409 | /// Module defines [BorshSerialize] implementation for |
410 | /// [HashMap](std::collections::HashMap)/[HashSet](std::collections::HashSet). |
411 | #[cfg (hash_collections)] |
412 | pub mod hashes { |
413 | use crate::__private::maybestd::vec::Vec; |
414 | use crate::error::check_zst; |
415 | use crate::{ |
416 | BorshSerialize, |
417 | __private::maybestd::collections::{HashMap, HashSet}, |
418 | }; |
419 | use core::convert::TryFrom; |
420 | use core::hash::BuildHasher; |
421 | |
422 | use crate::io::{ErrorKind, Result, Write}; |
423 | |
424 | impl<K, V, H> BorshSerialize for HashMap<K, V, H> |
425 | where |
426 | K: BorshSerialize + Ord, |
427 | V: BorshSerialize, |
428 | H: BuildHasher, |
429 | { |
430 | #[inline ] |
431 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
432 | check_zst::<K>()?; |
433 | |
434 | let mut vec = self.iter().collect::<Vec<_>>(); |
435 | vec.sort_by(|(a, _), (b, _)| a.cmp(b)); |
436 | u32::try_from(vec.len()) |
437 | .map_err(|_| ErrorKind::InvalidData)? |
438 | .serialize(writer)?; |
439 | for kv in vec { |
440 | kv.serialize(writer)?; |
441 | } |
442 | Ok(()) |
443 | } |
444 | } |
445 | |
446 | impl<T, H> BorshSerialize for HashSet<T, H> |
447 | where |
448 | T: BorshSerialize + Ord, |
449 | H: BuildHasher, |
450 | { |
451 | #[inline ] |
452 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
453 | check_zst::<T>()?; |
454 | |
455 | let mut vec = self.iter().collect::<Vec<_>>(); |
456 | vec.sort(); |
457 | u32::try_from(vec.len()) |
458 | .map_err(|_| ErrorKind::InvalidData)? |
459 | .serialize(writer)?; |
460 | for item in vec { |
461 | item.serialize(writer)?; |
462 | } |
463 | Ok(()) |
464 | } |
465 | } |
466 | } |
467 | |
468 | impl<K, V> BorshSerialize for BTreeMap<K, V> |
469 | where |
470 | K: BorshSerialize, |
471 | V: BorshSerialize, |
472 | { |
473 | #[inline ] |
474 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
475 | check_zst::<K>()?; |
476 | // NOTE: BTreeMap iterates over the entries that are sorted by key, so the serialization |
477 | // result will be consistent without a need to sort the entries as we do for HashMap |
478 | // serialization. |
479 | u32u32::try_from(self.len()) |
480 | .map_err(|_| ErrorKind::InvalidData)? |
481 | .serialize(writer)?; |
482 | for (key: &K, value: &V) in self { |
483 | key.serialize(writer)?; |
484 | value.serialize(writer)?; |
485 | } |
486 | Ok(()) |
487 | } |
488 | } |
489 | |
490 | impl<T> BorshSerialize for BTreeSet<T> |
491 | where |
492 | T: BorshSerialize, |
493 | { |
494 | #[inline ] |
495 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
496 | check_zst::<T>()?; |
497 | // NOTE: BTreeSet iterates over the items that are sorted, so the serialization result will |
498 | // be consistent without a need to sort the entries as we do for HashSet serialization. |
499 | u32u32::try_from(self.len()) |
500 | .map_err(|_| ErrorKind::InvalidData)? |
501 | .serialize(writer)?; |
502 | for item: &T in self { |
503 | item.serialize(writer)?; |
504 | } |
505 | Ok(()) |
506 | } |
507 | } |
508 | |
509 | #[cfg (feature = "std" )] |
510 | impl BorshSerialize for std::net::SocketAddr { |
511 | #[inline ] |
512 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
513 | match *self { |
514 | std::net::SocketAddr::V4(ref addr: &SocketAddrV4) => { |
515 | 0u8.serialize(writer)?; |
516 | addr.serialize(writer) |
517 | } |
518 | std::net::SocketAddr::V6(ref addr: &SocketAddrV6) => { |
519 | 1u8.serialize(writer)?; |
520 | addr.serialize(writer) |
521 | } |
522 | } |
523 | } |
524 | } |
525 | |
526 | #[cfg (feature = "std" )] |
527 | impl BorshSerialize for std::net::SocketAddrV4 { |
528 | #[inline ] |
529 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
530 | self.ip().serialize(writer)?; |
531 | self.port().serialize(writer) |
532 | } |
533 | } |
534 | |
535 | #[cfg (feature = "std" )] |
536 | impl BorshSerialize for std::net::SocketAddrV6 { |
537 | #[inline ] |
538 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
539 | self.ip().serialize(writer)?; |
540 | self.port().serialize(writer) |
541 | } |
542 | } |
543 | |
544 | #[cfg (feature = "std" )] |
545 | impl BorshSerialize for std::net::Ipv4Addr { |
546 | #[inline ] |
547 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
548 | writer.write_all(&self.octets()) |
549 | } |
550 | } |
551 | |
552 | #[cfg (feature = "std" )] |
553 | impl BorshSerialize for std::net::Ipv6Addr { |
554 | #[inline ] |
555 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
556 | writer.write_all(&self.octets()) |
557 | } |
558 | } |
559 | |
560 | #[cfg (feature = "std" )] |
561 | impl BorshSerialize for std::net::IpAddr { |
562 | #[inline ] |
563 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
564 | match self { |
565 | std::net::IpAddr::V4(ipv4: &Ipv4Addr) => { |
566 | writer.write_all(&0u8.to_le_bytes())?; |
567 | ipv4.serialize(writer) |
568 | } |
569 | std::net::IpAddr::V6(ipv6: &Ipv6Addr) => { |
570 | writer.write_all(&1u8.to_le_bytes())?; |
571 | ipv6.serialize(writer) |
572 | } |
573 | } |
574 | } |
575 | } |
576 | impl<T: BorshSerialize + ?Sized> BorshSerialize for Box<T> { |
577 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
578 | self.as_ref().serialize(writer) |
579 | } |
580 | } |
581 | |
582 | impl<T, const N: usize> BorshSerialize for [T; N] |
583 | where |
584 | T: BorshSerialize, |
585 | { |
586 | #[inline ] |
587 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
588 | if N == 0 { |
589 | return Ok(()); |
590 | } else if let Some(u8_slice: &[u8]) = T::u8_slice(self) { |
591 | writer.write_all(buf:u8_slice)?; |
592 | } else { |
593 | for el: &T in self.iter() { |
594 | el.serialize(writer)?; |
595 | } |
596 | } |
597 | Ok(()) |
598 | } |
599 | } |
600 | |
601 | macro_rules! impl_tuple { |
602 | (@unit $name:ty) => { |
603 | impl BorshSerialize for $name { |
604 | #[inline] |
605 | fn serialize<W: Write>(&self, _writer: &mut W) -> Result<()> { |
606 | Ok(()) |
607 | } |
608 | } |
609 | }; |
610 | |
611 | ($($idx:tt $name:ident)+) => { |
612 | impl<$($name),+> BorshSerialize for ($($name,)+) |
613 | where $($name: BorshSerialize,)+ |
614 | { |
615 | #[inline] |
616 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
617 | $(self.$idx.serialize(writer)?;)+ |
618 | Ok(()) |
619 | } |
620 | } |
621 | }; |
622 | } |
623 | |
624 | impl_tuple!(@unit ()); |
625 | impl_tuple!(@unit core::ops::RangeFull); |
626 | |
627 | impl_tuple!(0 T0); |
628 | impl_tuple!(0 T0 1 T1); |
629 | impl_tuple!(0 T0 1 T1 2 T2); |
630 | impl_tuple!(0 T0 1 T1 2 T2 3 T3); |
631 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4); |
632 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5); |
633 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6); |
634 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7); |
635 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8); |
636 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9); |
637 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10); |
638 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11); |
639 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12); |
640 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13); |
641 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14); |
642 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15); |
643 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16); |
644 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17); |
645 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18); |
646 | impl_tuple!(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15 16 T16 17 T17 18 T18 19 T19); |
647 | |
648 | macro_rules! impl_range { |
649 | ($type:ident, $this:ident, $($field:expr),*) => { |
650 | impl<T: BorshSerialize> BorshSerialize for core::ops::$type<T> { |
651 | #[inline] |
652 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
653 | let $this = self; |
654 | $( $field.serialize(writer)?; )* |
655 | Ok(()) |
656 | } |
657 | } |
658 | }; |
659 | } |
660 | |
661 | impl_range!(Range, this, &this.start, &this.end); |
662 | impl_range!(RangeInclusive, this, this.start(), this.end()); |
663 | impl_range!(RangeFrom, this, &this.start); |
664 | impl_range!(RangeTo, this, &this.end); |
665 | impl_range!(RangeToInclusive, this, &this.end); |
666 | |
667 | /// Module is available if borsh is built with `features = ["rc"]`. |
668 | #[cfg (feature = "rc" )] |
669 | pub mod rc { |
670 | //! |
671 | //! Module defines [BorshSerialize] implementation for |
672 | //! [alloc::rc::Rc](std::rc::Rc) and [alloc::sync::Arc](std::sync::Arc). |
673 | use crate::__private::maybestd::{rc::Rc, sync::Arc}; |
674 | use crate::io::{Result, Write}; |
675 | use crate::BorshSerialize; |
676 | |
677 | /// This impl requires the [`"rc"`] Cargo feature of borsh. |
678 | /// |
679 | /// Serializing a data structure containing `Rc` will serialize a copy of |
680 | /// the contents of the `Rc` each time the `Rc` is referenced within the |
681 | /// data structure. Serialization will not attempt to deduplicate these |
682 | /// repeated data. |
683 | impl<T: BorshSerialize + ?Sized> BorshSerialize for Rc<T> { |
684 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
685 | (**self).serialize(writer) |
686 | } |
687 | } |
688 | |
689 | /// This impl requires the [`"rc"`] Cargo feature of borsh. |
690 | /// |
691 | /// Serializing a data structure containing `Arc` will serialize a copy of |
692 | /// the contents of the `Arc` each time the `Arc` is referenced within the |
693 | /// data structure. Serialization will not attempt to deduplicate these |
694 | /// repeated data. |
695 | impl<T: BorshSerialize + ?Sized> BorshSerialize for Arc<T> { |
696 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
697 | (**self).serialize(writer) |
698 | } |
699 | } |
700 | } |
701 | |
702 | impl<T: ?Sized> BorshSerialize for PhantomData<T> { |
703 | fn serialize<W: Write>(&self, _: &mut W) -> Result<()> { |
704 | Ok(()) |
705 | } |
706 | } |
707 | |
708 | impl<T> BorshSerialize for core::cell::Cell<T> |
709 | where |
710 | T: BorshSerialize + Copy, |
711 | { |
712 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
713 | <T as BorshSerialize>::serialize(&self.get(), writer) |
714 | } |
715 | } |
716 | |
717 | impl<T> BorshSerialize for core::cell::RefCell<T> |
718 | where |
719 | T: BorshSerialize + Sized, |
720 | { |
721 | fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> { |
722 | match self.try_borrow() { |
723 | Ok(ref value: &Ref<'_, T>) => value.serialize(writer), |
724 | Err(_) => Err(Error::new(kind:ErrorKind::Other, error:"already mutably borrowed" )), |
725 | } |
726 | } |
727 | } |
728 | |