1use core::marker::PhantomData;
2use core::mem::MaybeUninit;
3use core::{
4 convert::{TryFrom, TryInto},
5 mem::size_of,
6};
7
8#[cfg(feature = "bytes")]
9use bytes::{BufMut, BytesMut};
10
11use crate::__private::maybestd::{
12 borrow::{Borrow, Cow, ToOwned},
13 boxed::Box,
14 collections::{BTreeMap, BTreeSet, LinkedList, VecDeque},
15 format,
16 string::{String, ToString},
17 vec,
18 vec::Vec,
19};
20use crate::io::{Error, ErrorKind, Read, Result};
21
22use crate::error::check_zst;
23
24mod hint;
25
26const ERROR_NOT_ALL_BYTES_READ: &str = "Not all bytes read";
27const ERROR_UNEXPECTED_LENGTH_OF_INPUT: &str = "Unexpected length of input";
28const ERROR_OVERFLOW_ON_MACHINE_WITH_32_BIT_ISIZE: &str = "Overflow on machine with 32 bit isize";
29const ERROR_OVERFLOW_ON_MACHINE_WITH_32_BIT_USIZE: &str = "Overflow on machine with 32 bit usize";
30const ERROR_INVALID_ZERO_VALUE: &str = "Expected a non-zero value";
31
32#[cfg(feature = "de_strict_order")]
33const ERROR_WRONG_ORDER_OF_KEYS: &str = "keys were not serialized in ascending order";
34
35pub trait BorshDeserialize: Sized {
37 fn deserialize(buf: &mut &[u8]) -> Result<Self> {
40 Self::deserialize_reader(&mut *buf)
41 }
42
43 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self>;
44
45 fn try_from_slice(v: &[u8]) -> Result<Self> {
47 let mut v_mut = v;
48 let result = Self::deserialize(&mut v_mut)?;
49 if !v_mut.is_empty() {
50 return Err(Error::new(ErrorKind::InvalidData, ERROR_NOT_ALL_BYTES_READ));
51 }
52 Ok(result)
53 }
54
55 fn try_from_reader<R: Read>(reader: &mut R) -> Result<Self> {
56 let result = Self::deserialize_reader(reader)?;
57 let mut buf = [0u8; 1];
58 match reader.read_exact(&mut buf) {
59 Err(f) if f.kind() == ErrorKind::UnexpectedEof => Ok(result),
60 _ => Err(Error::new(ErrorKind::InvalidData, ERROR_NOT_ALL_BYTES_READ)),
61 }
62 }
63
64 #[inline]
65 #[doc(hidden)]
66 fn vec_from_reader<R: Read>(len: u32, reader: &mut R) -> Result<Option<Vec<Self>>> {
67 let _ = len;
68 let _ = reader;
69 Ok(None)
70 }
71
72 #[inline]
73 #[doc(hidden)]
74 fn array_from_reader<R: Read, const N: usize>(reader: &mut R) -> Result<Option<[Self; N]>> {
75 let _ = reader;
76 Ok(None)
77 }
78}
79
80pub trait EnumExt: BorshDeserialize {
82 fn deserialize_variant<R: Read>(reader: &mut R, tag: u8) -> Result<Self>;
136}
137
138fn unexpected_eof_to_unexpected_length_of_input(e: Error) -> Error {
139 if e.kind() == ErrorKind::UnexpectedEof {
140 Error::new(ErrorKind::InvalidData, ERROR_UNEXPECTED_LENGTH_OF_INPUT)
141 } else {
142 e
143 }
144}
145
146impl BorshDeserialize for u8 {
147 #[inline]
148 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
149 let mut buf = [0u8; 1];
150 reader
151 .read_exact(&mut buf)
152 .map_err(unexpected_eof_to_unexpected_length_of_input)?;
153 Ok(buf[0])
154 }
155
156 #[inline]
157 #[doc(hidden)]
158 fn vec_from_reader<R: Read>(len: u32, reader: &mut R) -> Result<Option<Vec<Self>>> {
159 let len: usize = len.try_into().map_err(|_| ErrorKind::InvalidData)?;
160 let mut vec = vec![0u8; len.min(1024 * 1024)];
165 let mut pos = 0;
166 while pos < len {
167 if pos == vec.len() {
168 vec.resize(vec.len().saturating_mul(2).min(len), 0)
169 }
170 match reader.read(&mut vec.as_mut_slice()[pos..])? {
172 0 => {
173 return Err(Error::new(
174 ErrorKind::InvalidData,
175 ERROR_UNEXPECTED_LENGTH_OF_INPUT,
176 ))
177 }
178 read => {
179 pos += read;
180 }
181 }
182 }
183 Ok(Some(vec))
184 }
185
186 #[inline]
187 #[doc(hidden)]
188 fn array_from_reader<R: Read, const N: usize>(reader: &mut R) -> Result<Option<[Self; N]>> {
189 let mut arr = [0u8; N];
190 reader
191 .read_exact(&mut arr)
192 .map_err(unexpected_eof_to_unexpected_length_of_input)?;
193 Ok(Some(arr))
194 }
195}
196
197macro_rules! impl_for_integer {
198 ($type: ident) => {
199 impl BorshDeserialize for $type {
200 #[inline]
201 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
202 let mut buf = [0u8; size_of::<$type>()];
203 reader
204 .read_exact(&mut buf)
205 .map_err(unexpected_eof_to_unexpected_length_of_input)?;
206 let res = $type::from_le_bytes(buf.try_into().unwrap());
207 Ok(res)
208 }
209 }
210 };
211}
212
213impl_for_integer!(i8);
214impl_for_integer!(i16);
215impl_for_integer!(i32);
216impl_for_integer!(i64);
217impl_for_integer!(i128);
218impl_for_integer!(u16);
219impl_for_integer!(u32);
220impl_for_integer!(u64);
221impl_for_integer!(u128);
222
223macro_rules! impl_for_nonzero_integer {
224 ($type: ty) => {
225 impl BorshDeserialize for $type {
226 #[inline]
227 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
228 <$type>::new(BorshDeserialize::deserialize_reader(reader)?)
229 .ok_or_else(|| Error::new(ErrorKind::InvalidData, ERROR_INVALID_ZERO_VALUE))
230 }
231 }
232 };
233}
234
235impl_for_nonzero_integer!(core::num::NonZeroI8);
236impl_for_nonzero_integer!(core::num::NonZeroI16);
237impl_for_nonzero_integer!(core::num::NonZeroI32);
238impl_for_nonzero_integer!(core::num::NonZeroI64);
239impl_for_nonzero_integer!(core::num::NonZeroI128);
240impl_for_nonzero_integer!(core::num::NonZeroU8);
241impl_for_nonzero_integer!(core::num::NonZeroU16);
242impl_for_nonzero_integer!(core::num::NonZeroU32);
243impl_for_nonzero_integer!(core::num::NonZeroU64);
244impl_for_nonzero_integer!(core::num::NonZeroU128);
245impl_for_nonzero_integer!(core::num::NonZeroUsize);
246
247impl BorshDeserialize for isize {
248 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
249 let i: i64 = BorshDeserialize::deserialize_reader(reader)?;
250 let i = isize::try_from(i).map_err(|_| {
251 Error::new(
252 ErrorKind::InvalidData,
253 ERROR_OVERFLOW_ON_MACHINE_WITH_32_BIT_ISIZE,
254 )
255 })?;
256 Ok(i)
257 }
258}
259
260impl BorshDeserialize for usize {
261 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
262 let u: u64 = BorshDeserialize::deserialize_reader(reader)?;
263 let u = usize::try_from(u).map_err(|_| {
264 Error::new(
265 ErrorKind::InvalidData,
266 ERROR_OVERFLOW_ON_MACHINE_WITH_32_BIT_USIZE,
267 )
268 })?;
269 Ok(u)
270 }
271}
272
273macro_rules! impl_for_float {
276 ($type: ident, $int_type: ident) => {
277 impl BorshDeserialize for $type {
278 #[inline]
279 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
280 let mut buf = [0u8; size_of::<$type>()];
281 reader
282 .read_exact(&mut buf)
283 .map_err(unexpected_eof_to_unexpected_length_of_input)?;
284 let res = $type::from_bits($int_type::from_le_bytes(buf.try_into().unwrap()));
285 if res.is_nan() {
286 return Err(Error::new(
287 ErrorKind::InvalidData,
288 "For portability reasons we do not allow to deserialize NaNs.",
289 ));
290 }
291 Ok(res)
292 }
293 }
294 };
295}
296
297impl_for_float!(f32, u32);
298impl_for_float!(f64, u64);
299
300impl BorshDeserialize for bool {
301 #[inline]
302 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
303 let b: u8 = BorshDeserialize::deserialize_reader(reader)?;
304 if b == 0 {
305 Ok(false)
306 } else if b == 1 {
307 Ok(true)
308 } else {
309 let msg = format!("Invalid bool representation: {}", b);
310
311 Err(Error::new(ErrorKind::InvalidData, msg))
312 }
313 }
314}
315
316impl<T> BorshDeserialize for Option<T>
317where
318 T: BorshDeserialize,
319{
320 #[inline]
321 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
322 let flag: u8 = BorshDeserialize::deserialize_reader(reader)?;
323 if flag == 0 {
324 Ok(None)
325 } else if flag == 1 {
326 Ok(Some(T::deserialize_reader(reader)?))
327 } else {
328 let msg = format!(
329 "Invalid Option representation: {}. The first byte must be 0 or 1",
330 flag
331 );
332
333 Err(Error::new(ErrorKind::InvalidData, msg))
334 }
335 }
336}
337
338impl<T, E> BorshDeserialize for core::result::Result<T, E>
339where
340 T: BorshDeserialize,
341 E: BorshDeserialize,
342{
343 #[inline]
344 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
345 let flag: u8 = BorshDeserialize::deserialize_reader(reader)?;
346 if flag == 0 {
347 Ok(Err(E::deserialize_reader(reader)?))
348 } else if flag == 1 {
349 Ok(Ok(T::deserialize_reader(reader)?))
350 } else {
351 let msg = format!(
352 "Invalid Result representation: {}. The first byte must be 0 or 1",
353 flag
354 );
355
356 Err(Error::new(ErrorKind::InvalidData, msg))
357 }
358 }
359}
360
361impl BorshDeserialize for String {
362 #[inline]
363 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
364 String::from_utf8(Vec::<u8>::deserialize_reader(reader)?).map_err(|err| {
365 let msg = err.to_string();
366 Error::new(ErrorKind::InvalidData, msg)
367 })
368 }
369}
370
371#[cfg(feature = "ascii")]
373pub mod ascii {
374 use crate::BorshDeserialize;
378 use crate::__private::maybestd::{string::ToString, vec::Vec};
379 use crate::io::{Error, ErrorKind, Read, Result};
380
381 impl BorshDeserialize for ascii::AsciiString {
382 #[inline]
383 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
384 let bytes = Vec::<u8>::deserialize_reader(reader)?;
385 ascii::AsciiString::from_ascii(bytes)
386 .map_err(|err| Error::new(ErrorKind::InvalidData, err.to_string()))
387 }
388 }
389
390 impl BorshDeserialize for ascii::AsciiChar {
391 #[inline]
392 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
393 let byte = u8::deserialize_reader(reader)?;
394 ascii::AsciiChar::from_ascii(byte)
395 .map_err(|err| Error::new(ErrorKind::InvalidData, err.to_string()))
396 }
397 }
398}
399
400impl<T> BorshDeserialize for Vec<T>
401where
402 T: BorshDeserialize,
403{
404 #[inline]
405 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
406 check_zst::<T>()?;
407
408 let len = u32::deserialize_reader(reader)?;
409 if len == 0 {
410 Ok(Vec::new())
411 } else if let Some(vec_bytes) = T::vec_from_reader(len, reader)? {
412 Ok(vec_bytes)
413 } else {
414 let mut result = Vec::with_capacity(hint::cautious::<T>(len));
416 for _ in 0..len {
417 result.push(T::deserialize_reader(reader)?);
418 }
419 Ok(result)
420 }
421 }
422}
423
424#[cfg(feature = "bytes")]
425impl BorshDeserialize for bytes::Bytes {
426 #[inline]
427 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
428 let vec = <Vec<u8>>::deserialize_reader(reader)?;
429 Ok(vec.into())
430 }
431}
432
433#[cfg(feature = "bytes")]
434impl BorshDeserialize for bytes::BytesMut {
435 #[inline]
436 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
437 let len = u32::deserialize_reader(reader)?;
438 let mut out = BytesMut::with_capacity(hint::cautious::<u8>(len));
439 for _ in 0..len {
440 out.put_u8(u8::deserialize_reader(reader)?);
441 }
442 Ok(out)
443 }
444}
445
446#[cfg(feature = "bson")]
447impl BorshDeserialize for bson::oid::ObjectId {
448 #[inline]
449 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
450 let mut buf = [0u8; 12];
451 reader.read_exact(&mut buf)?;
452 Ok(bson::oid::ObjectId::from_bytes(buf))
453 }
454}
455
456impl<T> BorshDeserialize for Cow<'_, T>
457where
458 T: ToOwned + ?Sized,
459 T::Owned: BorshDeserialize,
460{
461 #[inline]
462 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
463 Ok(Cow::Owned(BorshDeserialize::deserialize_reader(reader)?))
464 }
465}
466
467impl<T> BorshDeserialize for VecDeque<T>
468where
469 T: BorshDeserialize,
470{
471 #[inline]
472 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
473 let vec = <Vec<T>>::deserialize_reader(reader)?;
474 Ok(vec.into())
475 }
476}
477
478impl<T> BorshDeserialize for LinkedList<T>
479where
480 T: BorshDeserialize,
481{
482 #[inline]
483 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
484 let vec = <Vec<T>>::deserialize_reader(reader)?;
485 Ok(vec.into_iter().collect::<LinkedList<T>>())
486 }
487}
488
489#[cfg(hash_collections)]
491pub mod hashes {
492 use core::hash::{BuildHasher, Hash};
496
497 use crate::BorshDeserialize;
498 use crate::__private::maybestd::collections::{HashMap, HashSet};
499 use crate::__private::maybestd::vec::Vec;
500 use crate::io::{Read, Result};
501
502 #[cfg(feature = "de_strict_order")]
503 const ERROR_WRONG_ORDER_OF_KEYS: &str = "keys were not serialized in ascending order";
504 use crate::error::check_zst;
505 #[cfg(feature = "de_strict_order")]
506 use crate::io::{Error, ErrorKind};
507
508 impl<T, H> BorshDeserialize for HashSet<T, H>
509 where
510 T: BorshDeserialize + Eq + Hash + Ord,
511 H: BuildHasher + Default,
512 {
513 #[inline]
514 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
515 let vec = <Vec<T>>::deserialize_reader(reader)?;
520
521 #[cfg(feature = "de_strict_order")]
522 for pair in vec.windows(2) {
525 let [a, b] = pair else {
526 unreachable!("`windows` always return a slice of length 2 or nothing");
527 };
528 let cmp_result = a.cmp(b).is_lt();
529 if !cmp_result {
530 return Err(Error::new(
531 ErrorKind::InvalidData,
532 ERROR_WRONG_ORDER_OF_KEYS,
533 ));
534 }
535 }
536
537 Ok(vec.into_iter().collect::<HashSet<T, H>>())
538 }
539 }
540
541 impl<K, V, H> BorshDeserialize for HashMap<K, V, H>
542 where
543 K: BorshDeserialize + Eq + Hash + Ord,
544 V: BorshDeserialize,
545 H: BuildHasher + Default,
546 {
547 #[inline]
548 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
549 check_zst::<K>()?;
550 let vec = <Vec<(K, V)>>::deserialize_reader(reader)?;
555
556 #[cfg(feature = "de_strict_order")]
557 for pair in vec.windows(2) {
560 let [(a_k, _a_v), (b_k, _b_v)] = pair else {
561 unreachable!("`windows` always return a slice of length 2 or nothing");
562 };
563 let cmp_result = a_k.cmp(b_k).is_lt();
564 if !cmp_result {
565 return Err(Error::new(
566 ErrorKind::InvalidData,
567 ERROR_WRONG_ORDER_OF_KEYS,
568 ));
569 }
570 }
571
572 Ok(vec.into_iter().collect::<HashMap<K, V, H>>())
573 }
574 }
575}
576
577impl<T> BorshDeserialize for BTreeSet<T>
578where
579 T: BorshDeserialize + Ord,
580{
581 #[inline]
582 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
583 let vec = <Vec<T>>::deserialize_reader(reader)?;
588
589 #[cfg(feature = "de_strict_order")]
590 for pair in vec.windows(2) {
593 let [a, b] = pair else {
594 unreachable!("`windows` always return a slice of length 2 or nothing");
595 };
596 let cmp_result = a.cmp(b).is_lt();
597 if !cmp_result {
598 return Err(Error::new(
599 ErrorKind::InvalidData,
600 ERROR_WRONG_ORDER_OF_KEYS,
601 ));
602 }
603 }
604 Ok(vec.into_iter().collect::<BTreeSet<T>>())
607 }
608}
609
610impl<K, V> BorshDeserialize for BTreeMap<K, V>
611where
612 K: BorshDeserialize + Ord,
613 V: BorshDeserialize,
614{
615 #[inline]
616 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
617 check_zst::<K>()?;
618 let vec = <Vec<(K, V)>>::deserialize_reader(reader)?;
623
624 #[cfg(feature = "de_strict_order")]
625 for pair in vec.windows(2) {
628 let [(a_k, _a_v), (b_k, _b_v)] = pair else {
629 unreachable!("`windows` always return a slice of length 2 or nothing");
630 };
631 let cmp_result = a_k.cmp(b_k).is_lt();
632 if !cmp_result {
633 return Err(Error::new(
634 ErrorKind::InvalidData,
635 ERROR_WRONG_ORDER_OF_KEYS,
636 ));
637 }
638 }
639
640 Ok(vec.into_iter().collect::<BTreeMap<K, V>>())
643 }
644}
645
646#[cfg(feature = "std")]
647impl BorshDeserialize for std::net::SocketAddr {
648 #[inline]
649 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
650 let kind = u8::deserialize_reader(reader)?;
651 match kind {
652 0 => std::net::SocketAddrV4::deserialize_reader(reader).map(std::net::SocketAddr::V4),
653 1 => std::net::SocketAddrV6::deserialize_reader(reader).map(std::net::SocketAddr::V6),
654 value => Err(Error::new(
655 ErrorKind::InvalidData,
656 format!("Invalid SocketAddr variant: {}", value),
657 )),
658 }
659 }
660}
661
662#[cfg(feature = "std")]
663impl BorshDeserialize for std::net::SocketAddrV4 {
664 #[inline]
665 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
666 let ip = std::net::Ipv4Addr::deserialize_reader(reader)?;
667 let port = u16::deserialize_reader(reader)?;
668 Ok(std::net::SocketAddrV4::new(ip, port))
669 }
670}
671
672#[cfg(feature = "std")]
673impl BorshDeserialize for std::net::SocketAddrV6 {
674 #[inline]
675 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
676 let ip = std::net::Ipv6Addr::deserialize_reader(reader)?;
677 let port = u16::deserialize_reader(reader)?;
678 Ok(std::net::SocketAddrV6::new(ip, port, 0, 0))
679 }
680}
681
682#[cfg(feature = "std")]
683impl BorshDeserialize for std::net::Ipv4Addr {
684 #[inline]
685 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
686 let mut buf = [0u8; 4];
687 reader
688 .read_exact(&mut buf)
689 .map_err(unexpected_eof_to_unexpected_length_of_input)?;
690 Ok(std::net::Ipv4Addr::from(buf))
691 }
692}
693
694#[cfg(feature = "std")]
695impl BorshDeserialize for std::net::Ipv6Addr {
696 #[inline]
697 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
698 let mut buf = [0u8; 16];
699 reader
700 .read_exact(&mut buf)
701 .map_err(unexpected_eof_to_unexpected_length_of_input)?;
702 Ok(std::net::Ipv6Addr::from(buf))
703 }
704}
705
706impl<T, U> BorshDeserialize for Box<T>
707where
708 U: Into<Box<T>> + Borrow<T>,
709 T: ToOwned<Owned = U> + ?Sized,
710 T::Owned: BorshDeserialize,
711{
712 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
713 Ok(T::Owned::deserialize_reader(reader)?.into())
714 }
715}
716
717impl<T, const N: usize> BorshDeserialize for [T; N]
718where
719 T: BorshDeserialize,
720{
721 #[inline]
722 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
723 struct ArrayDropGuard<T, const N: usize> {
724 buffer: [MaybeUninit<T>; N],
725 init_count: usize,
726 }
727 impl<T, const N: usize> Drop for ArrayDropGuard<T, N> {
728 fn drop(&mut self) {
729 let init_range = &mut self.buffer[..self.init_count];
730 unsafe {
734 core::ptr::drop_in_place(init_range as *mut _ as *mut [T]);
735 };
736 }
737 }
738 impl<T, const N: usize> ArrayDropGuard<T, N> {
739 unsafe fn transmute_to_array(mut self) -> [T; N] {
740 debug_assert_eq!(self.init_count, N);
741 self.init_count = 0;
743 core::ptr::read(&self.buffer as *const _ as *const [T; N])
747 }
748 fn fill_buffer(&mut self, mut f: impl FnMut() -> Result<T>) -> Result<()> {
749 for elem in self.buffer.iter_mut() {
752 elem.write(f()?);
753 self.init_count += 1;
754 }
755 Ok(())
756 }
757 }
758
759 if let Some(arr) = T::array_from_reader(reader)? {
760 Ok(arr)
761 } else {
762 let mut result = ArrayDropGuard {
763 buffer: unsafe { MaybeUninit::uninit().assume_init() },
764 init_count: 0,
765 };
766
767 result.fill_buffer(|| T::deserialize_reader(reader))?;
768
769 Ok(unsafe { result.transmute_to_array() })
771 }
772 }
773}
774
775#[test]
776fn array_deserialization_doesnt_leak() {
777 use core::sync::atomic::{AtomicUsize, Ordering};
778
779 static DESERIALIZE_COUNT: AtomicUsize = AtomicUsize::new(0);
780 static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
781
782 #[allow(unused)]
783 struct MyType(u8);
784 impl BorshDeserialize for MyType {
785 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
786 let val = u8::deserialize_reader(reader)?;
787 let v = DESERIALIZE_COUNT.fetch_add(1, Ordering::SeqCst);
788 if v >= 7 {
789 panic!("panic in deserialize");
790 }
791 Ok(MyType(val))
792 }
793 }
794 impl Drop for MyType {
795 fn drop(&mut self) {
796 DROP_COUNT.fetch_add(1, Ordering::SeqCst);
797 }
798 }
799
800 assert!(<[MyType; 5] as BorshDeserialize>::deserialize(&mut &[0u8; 3][..]).is_err());
801 assert_eq!(DESERIALIZE_COUNT.load(Ordering::SeqCst), 3);
802 assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3);
803
804 assert!(<[MyType; 2] as BorshDeserialize>::deserialize(&mut &[0u8; 2][..]).is_ok());
805 assert_eq!(DESERIALIZE_COUNT.load(Ordering::SeqCst), 5);
806 assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 5);
807
808 #[cfg(feature = "std")]
809 {
810 let result = std::panic::catch_unwind(|| {
812 <[MyType; 3] as BorshDeserialize>::deserialize(&mut &[0u8; 3][..]).unwrap();
813 });
814 assert!(result.is_err());
815 assert_eq!(DESERIALIZE_COUNT.load(Ordering::SeqCst), 8);
816 assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 7); }
818}
819
820macro_rules! impl_tuple {
821 (@unit $name:ty) => {
822 impl BorshDeserialize for $name {
823 #[inline]
824 fn deserialize_reader<R: Read>(_reader: &mut R) -> Result<Self> {
825 Ok(<$name>::default())
826 }
827 }
828 };
829
830 ($($name:ident)+) => {
831 impl<$($name),+> BorshDeserialize for ($($name,)+)
832 where $($name: BorshDeserialize,)+
833 {
834 #[inline]
835 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
836
837 Ok(($($name::deserialize_reader(reader)?,)+))
838 }
839 }
840 };
841}
842
843impl_tuple!(@unit ());
844impl_tuple!(@unit core::ops::RangeFull);
845
846impl_tuple!(T0);
847impl_tuple!(T0 T1);
848impl_tuple!(T0 T1 T2);
849impl_tuple!(T0 T1 T2 T3);
850impl_tuple!(T0 T1 T2 T3 T4);
851impl_tuple!(T0 T1 T2 T3 T4 T5);
852impl_tuple!(T0 T1 T2 T3 T4 T5 T6);
853impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7);
854impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8);
855impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9);
856impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
857impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
858impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);
859impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13);
860impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14);
861impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
862impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16);
863impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17);
864impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18);
865impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19);
866
867macro_rules! impl_range {
868 ($type:ident, $make:expr, $($side:ident),*) => {
869 impl<T: BorshDeserialize> BorshDeserialize for core::ops::$type<T> {
870 #[inline]
871 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
872 let ($($side,)*) = <_>::deserialize_reader(reader)?;
873 Ok($make)
874 }
875 }
876 };
877}
878
879impl_range!(Range, start..end, start, end);
880impl_range!(RangeInclusive, start..=end, start, end);
881impl_range!(RangeFrom, start.., start);
882impl_range!(RangeTo, ..end, end);
883impl_range!(RangeToInclusive, ..=end, end);
884
885#[cfg(feature = "rc")]
887pub mod rc {
888 use crate::__private::maybestd::{boxed::Box, rc::Rc, sync::Arc};
892 use crate::io::{Read, Result};
893 use crate::BorshDeserialize;
894
895 impl<T: ?Sized> BorshDeserialize for Rc<T>
901 where
902 Box<T>: BorshDeserialize,
903 {
904 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
905 Ok(Box::<T>::deserialize_reader(reader)?.into())
906 }
907 }
908
909 impl<T: ?Sized> BorshDeserialize for Arc<T>
915 where
916 Box<T>: BorshDeserialize,
917 {
918 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
919 Ok(Box::<T>::deserialize_reader(reader)?.into())
920 }
921 }
922}
923
924impl<T: ?Sized> BorshDeserialize for PhantomData<T> {
925 fn deserialize_reader<R: Read>(_: &mut R) -> Result<Self> {
926 Ok(PhantomData)
927 }
928}
929
930impl<T> BorshDeserialize for core::cell::Cell<T>
931where
932 T: BorshDeserialize + Copy,
933{
934 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
935 <T as BorshDeserialize>::deserialize_reader(reader).map(core::cell::Cell::new)
936 }
937}
938
939impl<T> BorshDeserialize for core::cell::RefCell<T>
940where
941 T: BorshDeserialize,
942{
943 fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
944 <T as BorshDeserialize>::deserialize_reader(reader).map(core::cell::RefCell::new)
945 }
946}
947
948pub fn from_slice<T: BorshDeserialize>(v: &[u8]) -> Result<T> {
977 let mut v_mut = v;
978 let object = T::deserialize(&mut v_mut)?;
979 if !v_mut.is_empty() {
980 return Err(Error::new(
981 ErrorKind::InvalidData,
982 crate::de::ERROR_NOT_ALL_BYTES_READ,
983 ));
984 }
985 Ok(object)
986}
987
988pub fn from_reader<R: Read, T: BorshDeserialize>(reader: &mut R) -> Result<T> {
1011 let result = T::deserialize_reader(reader)?;
1012 let mut buf = [0u8; 1];
1013 match reader.read_exact(&mut buf) {
1014 Err(f) if f.kind() == ErrorKind::UnexpectedEof => Ok(result),
1015 _ => Err(Error::new(ErrorKind::InvalidData, ERROR_NOT_ALL_BYTES_READ)),
1016 }
1017}