borsh/de/
mod.rs

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
35/// A data-structure that can be de-serialized from binary format by NBOR.
36pub trait BorshDeserialize: Sized {
37    /// Deserializes this instance from a given slice of bytes.
38    /// Updates the buffer to point at the remaining bytes.
39    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    /// Deserialize this instance from a slice of bytes.
46    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
80/// Additional methods offered on enums which is used by `[derive(BorshDeserialize)]`.
81pub trait EnumExt: BorshDeserialize {
82    /// Deserialises given variant of an enum from the reader.
83    ///
84    /// This may be used to perform validation or filtering based on what
85    /// variant is being deserialised.
86    ///
87    /// ```
88    /// use borsh::BorshDeserialize;
89    /// use borsh::de::EnumExt as _;
90    ///
91    /// /// derive is only available if borsh is built with `features = ["derive"]`
92    /// # #[cfg(feature = "derive")]
93    /// #[derive(Debug, PartialEq, Eq, BorshDeserialize)]
94    /// enum MyEnum {
95    ///     Zero,
96    ///     One(u8),
97    ///     Many(Vec<u8>)
98    /// }
99    ///
100    /// # #[cfg(feature = "derive")]
101    /// #[derive(Debug, PartialEq, Eq)]
102    /// struct OneOrZero(MyEnum);
103    ///
104    /// # #[cfg(feature = "derive")]
105    /// impl borsh::de::BorshDeserialize for OneOrZero {
106    ///     fn deserialize_reader<R: borsh::io::Read>(
107    ///         reader: &mut R,
108    ///     ) -> borsh::io::Result<Self> {
109    ///         use borsh::de::EnumExt;
110    ///         let tag = u8::deserialize_reader(reader)?;
111    ///         if tag == 2 {
112    ///             Err(borsh::io::Error::new(
113    ///                 borsh::io::ErrorKind::InvalidData,
114    ///                 "MyEnum::Many not allowed here",
115    ///             ))
116    ///         } else {
117    ///             MyEnum::deserialize_variant(reader, tag).map(Self)
118    ///         }
119    ///     }
120    /// }
121    ///
122    /// use borsh::from_slice;
123    /// let data = b"\0";
124    /// # #[cfg(feature = "derive")]
125    /// assert_eq!(MyEnum::Zero, from_slice::<MyEnum>(&data[..]).unwrap());
126    /// # #[cfg(feature = "derive")]
127    /// assert_eq!(MyEnum::Zero, from_slice::<OneOrZero>(&data[..]).unwrap().0);
128    ///
129    /// let data = b"\x02\0\0\0\0";
130    /// # #[cfg(feature = "derive")]
131    /// assert_eq!(MyEnum::Many(Vec::new()), from_slice::<MyEnum>(&data[..]).unwrap());
132    /// # #[cfg(feature = "derive")]
133    /// assert!(from_slice::<OneOrZero>(&data[..]).is_err());
134    /// ```
135    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        // Avoid OOM by limiting the size of allocation.  This makes the read
161        // less efficient (since we need to loop and reallocate) but it protects
162        // us from someone sending us [0xff, 0xff, 0xff, 0xff] and forcing us to
163        // allocate 4GiB of memory.
164        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            // TODO(mina86): Convert this to read_buf once that stabilises.
171            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
273// Note NaNs have a portability issue. Specifically, signalling NaNs on MIPS are quiet NaNs on x86,
274// and vice-versa. We disallow NaNs to avoid this issue.
275macro_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/// Module is available if borsh is built with `features = ["ascii"]`.
372#[cfg(feature = "ascii")]
373pub mod ascii {
374    //!
375    //! Module defines [BorshDeserialize] implementation for
376    //! some types from [ascii](::ascii) crate.
377    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            // TODO(16): return capacity allocation when we can safely do that.
415            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
456#[cfg(feature = "indexmap")]
457// Taken from https://github.com/indexmap-rs/indexmap/blob/dd06e5773e4f91748396c67d00c83637f5c0dd49/src/borsh.rs#L39
458// license: MIT OR Apache-2.0
459impl<K, V, S> BorshDeserialize for indexmap::IndexMap<K, V, S>
460where
461    K: BorshDeserialize + Eq + core::hash::Hash,
462    V: BorshDeserialize,
463    S: core::hash::BuildHasher + Default,
464{
465    #[inline]
466    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
467        check_zst::<K>()?;
468        let vec = <Vec<(K, V)>>::deserialize_reader(reader)?;
469        Ok(vec.into_iter().collect::<indexmap::IndexMap<K, V, S>>())
470    }
471}
472
473#[cfg(feature = "indexmap")]
474// Taken from https://github.com/indexmap-rs/indexmap/blob/dd06e5773e4f91748396c67d00c83637f5c0dd49/src/borsh.rs#L75
475// license: MIT OR Apache-2.0
476impl<T, S> BorshDeserialize for indexmap::IndexSet<T, S>
477where
478    T: BorshDeserialize + Eq + core::hash::Hash,
479    S: core::hash::BuildHasher + Default,
480{
481    #[inline]
482    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
483        check_zst::<T>()?;
484        let vec = <Vec<T>>::deserialize_reader(reader)?;
485        Ok(vec.into_iter().collect::<indexmap::IndexSet<T, S>>())
486    }
487}
488
489impl<T> BorshDeserialize for Cow<'_, T>
490where
491    T: ToOwned + ?Sized,
492    T::Owned: BorshDeserialize,
493{
494    #[inline]
495    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
496        Ok(Cow::Owned(BorshDeserialize::deserialize_reader(reader)?))
497    }
498}
499
500impl<T> BorshDeserialize for VecDeque<T>
501where
502    T: BorshDeserialize,
503{
504    #[inline]
505    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
506        let vec = <Vec<T>>::deserialize_reader(reader)?;
507        Ok(vec.into())
508    }
509}
510
511impl<T> BorshDeserialize for LinkedList<T>
512where
513    T: BorshDeserialize,
514{
515    #[inline]
516    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
517        let vec = <Vec<T>>::deserialize_reader(reader)?;
518        Ok(vec.into_iter().collect::<LinkedList<T>>())
519    }
520}
521
522/// Module is available if borsh is built with `features = ["std"]` or `features = ["hashbrown"]`.
523///
524/// Module defines [BorshDeserialize] implementation for
525/// [HashMap](std::collections::HashMap)/[HashSet](std::collections::HashSet).
526#[cfg(hash_collections)]
527pub mod hashes {
528    use core::hash::{BuildHasher, Hash};
529
530    use crate::BorshDeserialize;
531    use crate::__private::maybestd::collections::{HashMap, HashSet};
532    use crate::__private::maybestd::vec::Vec;
533    use crate::io::{Read, Result};
534
535    #[cfg(feature = "de_strict_order")]
536    const ERROR_WRONG_ORDER_OF_KEYS: &str = "keys were not serialized in ascending order";
537    use crate::error::check_zst;
538    #[cfg(feature = "de_strict_order")]
539    use crate::io::{Error, ErrorKind};
540
541    impl<T, H> BorshDeserialize for HashSet<T, H>
542    where
543        T: BorshDeserialize + Eq + Hash + Ord,
544        H: BuildHasher + Default,
545    {
546        #[inline]
547        fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
548            // NOTE: deserialize-as-you-go approach as once was in HashSet is better in the sense
549            // that it allows to fail early, and not allocate memory for all the elements
550            // which may fail `cmp()` checks
551            // NOTE: deserialize first to `Vec<T>` is faster
552            let vec = <Vec<T>>::deserialize_reader(reader)?;
553
554            #[cfg(feature = "de_strict_order")]
555            // TODO: replace with `is_sorted` api when stabilizes https://github.com/rust-lang/rust/issues/53485
556            // TODO: first replace with `array_windows` api when stabilizes https://github.com/rust-lang/rust/issues/75027
557            for pair in vec.windows(2) {
558                let [a, b] = pair else {
559                    unreachable!("`windows` always return a slice of length 2 or nothing");
560                };
561                let cmp_result = a.cmp(b).is_lt();
562                if !cmp_result {
563                    return Err(Error::new(
564                        ErrorKind::InvalidData,
565                        ERROR_WRONG_ORDER_OF_KEYS,
566                    ));
567                }
568            }
569
570            Ok(vec.into_iter().collect::<HashSet<T, H>>())
571        }
572    }
573
574    impl<K, V, H> BorshDeserialize for HashMap<K, V, H>
575    where
576        K: BorshDeserialize + Eq + Hash + Ord,
577        V: BorshDeserialize,
578        H: BuildHasher + Default,
579    {
580        #[inline]
581        fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
582            check_zst::<K>()?;
583            // NOTE: deserialize-as-you-go approach as once was in HashSet is better in the sense
584            // that it allows to fail early, and not allocate memory for all the entries
585            // which may fail `cmp()` checks
586            // NOTE: deserialize first to `Vec<(K, V)>` is faster
587            let vec = <Vec<(K, V)>>::deserialize_reader(reader)?;
588
589            #[cfg(feature = "de_strict_order")]
590            // TODO: replace with `is_sorted` api when stabilizes https://github.com/rust-lang/rust/issues/53485
591            // TODO: first replace with `array_windows` api when stabilizes https://github.com/rust-lang/rust/issues/75027
592            for pair in vec.windows(2) {
593                let [(a_k, _a_v), (b_k, _b_v)] = pair else {
594                    unreachable!("`windows` always return a slice of length 2 or nothing");
595                };
596                let cmp_result = a_k.cmp(b_k).is_lt();
597                if !cmp_result {
598                    return Err(Error::new(
599                        ErrorKind::InvalidData,
600                        ERROR_WRONG_ORDER_OF_KEYS,
601                    ));
602                }
603            }
604
605            Ok(vec.into_iter().collect::<HashMap<K, V, H>>())
606        }
607    }
608}
609
610impl<T> BorshDeserialize for BTreeSet<T>
611where
612    T: BorshDeserialize + Ord,
613{
614    #[inline]
615    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
616        // NOTE: deserialize-as-you-go approach as once was in HashSet is better in the sense
617        // that it allows to fail early, and not allocate memory for all the elements
618        // which may fail `cmp()` checks
619        // NOTE: deserialize first to `Vec<T>` is faster
620        let vec = <Vec<T>>::deserialize_reader(reader)?;
621
622        #[cfg(feature = "de_strict_order")]
623        // TODO: replace with `is_sorted` api when stabilizes https://github.com/rust-lang/rust/issues/53485
624        // TODO: first replace with `array_windows` api when stabilizes https://github.com/rust-lang/rust/issues/75027
625        for pair in vec.windows(2) {
626            let [a, b] = pair else {
627                unreachable!("`windows` always return a slice of length 2 or nothing");
628            };
629            let cmp_result = a.cmp(b).is_lt();
630            if !cmp_result {
631                return Err(Error::new(
632                    ErrorKind::InvalidData,
633                    ERROR_WRONG_ORDER_OF_KEYS,
634                ));
635            }
636        }
637        // NOTE: BTreeSet has an optimization inside of impl <T> FromIterator<T> for BTreeSet<T, Global>,
638        // based on BTreeMap::bulk_build_from_sorted_iter
639        Ok(vec.into_iter().collect::<BTreeSet<T>>())
640    }
641}
642
643impl<K, V> BorshDeserialize for BTreeMap<K, V>
644where
645    K: BorshDeserialize + Ord,
646    V: BorshDeserialize,
647{
648    #[inline]
649    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
650        check_zst::<K>()?;
651        // NOTE: deserialize-as-you-go approach as once was in HashSet is better in the sense
652        // that it allows to fail early, and not allocate memory for all the entries
653        // which may fail `cmp()` checks
654        // NOTE: deserialize first to `Vec<(K, V)>` is faster
655        let vec = <Vec<(K, V)>>::deserialize_reader(reader)?;
656
657        #[cfg(feature = "de_strict_order")]
658        // TODO: replace with `is_sorted` api when stabilizes https://github.com/rust-lang/rust/issues/53485
659        // TODO: first replace with `array_windows` api when stabilizes https://github.com/rust-lang/rust/issues/75027
660        for pair in vec.windows(2) {
661            let [(a_k, _a_v), (b_k, _b_v)] = pair else {
662                unreachable!("`windows` always return a slice of length 2 or nothing");
663            };
664            let cmp_result = a_k.cmp(b_k).is_lt();
665            if !cmp_result {
666                return Err(Error::new(
667                    ErrorKind::InvalidData,
668                    ERROR_WRONG_ORDER_OF_KEYS,
669                ));
670            }
671        }
672
673        // NOTE: BTreeMap has an optimization inside of impl<K, V> FromIterator<(K, V)> for BTreeMap<K, V, Global>,
674        // based on BTreeMap::bulk_build_from_sorted_iter
675        Ok(vec.into_iter().collect::<BTreeMap<K, V>>())
676    }
677}
678
679impl BorshDeserialize for core::net::SocketAddr {
680    #[inline]
681    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
682        let kind = u8::deserialize_reader(reader)?;
683        match kind {
684            0 => core::net::SocketAddrV4::deserialize_reader(reader).map(core::net::SocketAddr::V4),
685            1 => core::net::SocketAddrV6::deserialize_reader(reader).map(core::net::SocketAddr::V6),
686            value => Err(Error::new(
687                ErrorKind::InvalidData,
688                format!("Invalid SocketAddr variant: {}", value),
689            )),
690        }
691    }
692}
693
694impl BorshDeserialize for core::net::IpAddr {
695    #[inline]
696    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
697        let kind = u8::deserialize_reader(reader)?;
698        match kind {
699            0u8 => {
700                // Deserialize an Ipv4Addr and convert it to IpAddr::V4
701                let ipv4_addr = core::net::Ipv4Addr::deserialize_reader(reader)?;
702                Ok(core::net::IpAddr::V4(ipv4_addr))
703            }
704            1u8 => {
705                // Deserialize an Ipv6Addr and convert it to IpAddr::V6
706                let ipv6_addr = core::net::Ipv6Addr::deserialize_reader(reader)?;
707                Ok(core::net::IpAddr::V6(ipv6_addr))
708            }
709            value => Err(Error::new(
710                ErrorKind::InvalidData,
711                format!("Invalid IpAddr variant: {}", value),
712            )),
713        }
714    }
715}
716
717impl BorshDeserialize for core::net::SocketAddrV4 {
718    #[inline]
719    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
720        let ip = core::net::Ipv4Addr::deserialize_reader(reader)?;
721        let port = u16::deserialize_reader(reader)?;
722        Ok(core::net::SocketAddrV4::new(ip, port))
723    }
724}
725
726impl BorshDeserialize for core::net::SocketAddrV6 {
727    #[inline]
728    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
729        let ip = core::net::Ipv6Addr::deserialize_reader(reader)?;
730        let port = u16::deserialize_reader(reader)?;
731        Ok(core::net::SocketAddrV6::new(ip, port, 0, 0))
732    }
733}
734
735impl BorshDeserialize for core::net::Ipv4Addr {
736    #[inline]
737    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
738        let mut buf = [0u8; 4];
739        reader
740            .read_exact(&mut buf)
741            .map_err(unexpected_eof_to_unexpected_length_of_input)?;
742        Ok(core::net::Ipv4Addr::from(buf))
743    }
744}
745
746impl BorshDeserialize for core::net::Ipv6Addr {
747    #[inline]
748    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
749        let mut buf = [0u8; 16];
750        reader
751            .read_exact(&mut buf)
752            .map_err(unexpected_eof_to_unexpected_length_of_input)?;
753        Ok(core::net::Ipv6Addr::from(buf))
754    }
755}
756
757impl<T, U> BorshDeserialize for Box<T>
758where
759    U: Into<Box<T>> + Borrow<T>,
760    T: ToOwned<Owned = U> + ?Sized,
761    T::Owned: BorshDeserialize,
762{
763    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
764        Ok(T::Owned::deserialize_reader(reader)?.into())
765    }
766}
767
768impl<T, const N: usize> BorshDeserialize for [T; N]
769where
770    T: BorshDeserialize,
771{
772    #[inline]
773    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
774        struct ArrayDropGuard<T, const N: usize> {
775            buffer: [MaybeUninit<T>; N],
776            init_count: usize,
777        }
778        impl<T, const N: usize> Drop for ArrayDropGuard<T, N> {
779            fn drop(&mut self) {
780                let init_range = &mut self.buffer[..self.init_count];
781                // SAFETY: Elements up to self.init_count have been initialized. Assumes this value
782                //         is only incremented in `fill_buffer`, which writes the element before
783                //         increasing the init_count.
784                unsafe {
785                    core::ptr::drop_in_place(init_range as *mut _ as *mut [T]);
786                };
787            }
788        }
789        impl<T, const N: usize> ArrayDropGuard<T, N> {
790            unsafe fn transmute_to_array(mut self) -> [T; N] {
791                debug_assert_eq!(self.init_count, N);
792                // Set init_count to 0 so that the values do not get dropped twice.
793                self.init_count = 0;
794                // SAFETY: This cast is required because `mem::transmute` does not work with
795                //         const generics https://github.com/rust-lang/rust/issues/61956. This
796                //         array is guaranteed to be initialized by this point.
797                core::ptr::read(&self.buffer as *const _ as *const [T; N])
798            }
799            fn fill_buffer(&mut self, mut f: impl FnMut() -> Result<T>) -> Result<()> {
800                // TODO: replace with `core::array::try_from_fn` when stabilized to avoid manually
801                // dropping uninitialized values through the guard drop.
802                for elem in self.buffer.iter_mut() {
803                    elem.write(f()?);
804                    self.init_count += 1;
805                }
806                Ok(())
807            }
808        }
809
810        if let Some(arr) = T::array_from_reader(reader)? {
811            Ok(arr)
812        } else {
813            let mut result = ArrayDropGuard {
814                buffer: unsafe { MaybeUninit::uninit().assume_init() },
815                init_count: 0,
816            };
817
818            result.fill_buffer(|| T::deserialize_reader(reader))?;
819
820            // SAFETY: The elements up to `i` have been initialized in `fill_buffer`.
821            Ok(unsafe { result.transmute_to_array() })
822        }
823    }
824}
825
826#[test]
827fn array_deserialization_doesnt_leak() {
828    use core::sync::atomic::{AtomicUsize, Ordering};
829
830    static DESERIALIZE_COUNT: AtomicUsize = AtomicUsize::new(0);
831    static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
832
833    #[allow(unused)]
834    struct MyType(u8);
835    impl BorshDeserialize for MyType {
836        fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
837            let val = u8::deserialize_reader(reader)?;
838            let v = DESERIALIZE_COUNT.fetch_add(1, Ordering::SeqCst);
839            if v >= 7 {
840                panic!("panic in deserialize");
841            }
842            Ok(MyType(val))
843        }
844    }
845    impl Drop for MyType {
846        fn drop(&mut self) {
847            DROP_COUNT.fetch_add(1, Ordering::SeqCst);
848        }
849    }
850
851    assert!(<[MyType; 5] as BorshDeserialize>::deserialize(&mut &[0u8; 3][..]).is_err());
852    assert_eq!(DESERIALIZE_COUNT.load(Ordering::SeqCst), 3);
853    assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3);
854
855    assert!(<[MyType; 2] as BorshDeserialize>::deserialize(&mut &[0u8; 2][..]).is_ok());
856    assert_eq!(DESERIALIZE_COUNT.load(Ordering::SeqCst), 5);
857    assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 5);
858
859    #[cfg(feature = "std")]
860    {
861        // Test that during a panic in deserialize, the values are still dropped.
862        let result = std::panic::catch_unwind(|| {
863            <[MyType; 3] as BorshDeserialize>::deserialize(&mut &[0u8; 3][..]).unwrap();
864        });
865        assert!(result.is_err());
866        assert_eq!(DESERIALIZE_COUNT.load(Ordering::SeqCst), 8);
867        assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 7); // 5 because 6 panicked and was not init
868    }
869}
870
871macro_rules! impl_tuple {
872    (@unit $name:ty) => {
873        impl BorshDeserialize for $name {
874            #[inline]
875            fn deserialize_reader<R: Read>(_reader: &mut R) -> Result<Self> {
876                Ok(<$name>::default())
877            }
878        }
879    };
880
881    ($($name:ident)+) => {
882      impl<$($name),+> BorshDeserialize for ($($name,)+)
883      where $($name: BorshDeserialize,)+
884      {
885        #[inline]
886        fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
887
888            Ok(($($name::deserialize_reader(reader)?,)+))
889        }
890      }
891    };
892}
893
894impl_tuple!(@unit ());
895impl_tuple!(@unit core::ops::RangeFull);
896
897impl_tuple!(T0);
898impl_tuple!(T0 T1);
899impl_tuple!(T0 T1 T2);
900impl_tuple!(T0 T1 T2 T3);
901impl_tuple!(T0 T1 T2 T3 T4);
902impl_tuple!(T0 T1 T2 T3 T4 T5);
903impl_tuple!(T0 T1 T2 T3 T4 T5 T6);
904impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7);
905impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8);
906impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9);
907impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
908impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
909impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);
910impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13);
911impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14);
912impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
913impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16);
914impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17);
915impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18);
916impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19);
917
918macro_rules! impl_range {
919    ($type:ident, $make:expr, $($side:ident),*) => {
920        impl<T: BorshDeserialize> BorshDeserialize for core::ops::$type<T> {
921            #[inline]
922            fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
923                let ($($side,)*) = <_>::deserialize_reader(reader)?;
924                Ok($make)
925            }
926        }
927    };
928}
929
930impl_range!(Range, start..end, start, end);
931impl_range!(RangeInclusive, start..=end, start, end);
932impl_range!(RangeFrom, start.., start);
933impl_range!(RangeTo, ..end, end);
934impl_range!(RangeToInclusive, ..=end, end);
935
936/// Module is available if borsh is built with `features = ["rc"]`.
937#[cfg(feature = "rc")]
938pub mod rc {
939    //!
940    //! Module defines [BorshDeserialize] implementation for
941    //! [alloc::rc::Rc](std::rc::Rc) and [alloc::sync::Arc](std::sync::Arc).
942    use crate::__private::maybestd::{boxed::Box, rc::Rc, sync::Arc};
943    use crate::io::{Read, Result};
944    use crate::BorshDeserialize;
945
946    /// This impl requires the [`"rc"`] Cargo feature of borsh.
947    ///
948    /// Deserializing a data structure containing `Rc` will not attempt to
949    /// deduplicate `Rc` references to the same data. Every deserialized `Rc`
950    /// will end up with a strong count of 1.
951    impl<T: ?Sized> BorshDeserialize for Rc<T>
952    where
953        Box<T>: BorshDeserialize,
954    {
955        fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
956            Ok(Box::<T>::deserialize_reader(reader)?.into())
957        }
958    }
959
960    /// This impl requires the [`"rc"`] Cargo feature of borsh.
961    ///
962    /// Deserializing a data structure containing `Arc` will not attempt to
963    /// deduplicate `Arc` references to the same data. Every deserialized `Arc`
964    /// will end up with a strong count of 1.
965    impl<T: ?Sized> BorshDeserialize for Arc<T>
966    where
967        Box<T>: BorshDeserialize,
968    {
969        fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
970            Ok(Box::<T>::deserialize_reader(reader)?.into())
971        }
972    }
973}
974
975impl<T: ?Sized> BorshDeserialize for PhantomData<T> {
976    fn deserialize_reader<R: Read>(_: &mut R) -> Result<Self> {
977        Ok(PhantomData)
978    }
979}
980
981impl<T> BorshDeserialize for core::cell::Cell<T>
982where
983    T: BorshDeserialize + Copy,
984{
985    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
986        <T as BorshDeserialize>::deserialize_reader(reader).map(core::cell::Cell::new)
987    }
988}
989
990impl<T> BorshDeserialize for core::cell::RefCell<T>
991where
992    T: BorshDeserialize,
993{
994    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
995        <T as BorshDeserialize>::deserialize_reader(reader).map(core::cell::RefCell::new)
996    }
997}
998
999/// Deserializes an object from a slice of bytes.
1000/// # Example
1001/// ```
1002/// use borsh::{BorshDeserialize, BorshSerialize, from_slice, to_vec};
1003///
1004/// /// derive is only available if borsh is built with `features = ["derive"]`
1005/// # #[cfg(feature = "derive")]
1006/// #[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
1007/// struct MyStruct {
1008///    a: u64,
1009///    b: Vec<u8>,
1010/// }
1011///
1012/// # #[cfg(feature = "derive")]
1013/// let original = MyStruct { a: 10, b: vec![1, 2, 3] };
1014/// # #[cfg(feature = "derive")]
1015/// let encoded = to_vec(&original).unwrap();
1016/// # #[cfg(feature = "derive")]
1017/// let decoded = from_slice::<MyStruct>(&encoded).unwrap();
1018/// # #[cfg(feature = "derive")]
1019/// assert_eq!(original, decoded);
1020/// ```
1021/// # Panics
1022/// If the data is invalid, this function will panic.
1023/// # Errors
1024/// If the data is invalid, this function will return an error.
1025/// # Note
1026/// This function will return an error if the data is not fully read.
1027pub fn from_slice<T: BorshDeserialize>(v: &[u8]) -> Result<T> {
1028    let mut v_mut = v;
1029    let object = T::deserialize(&mut v_mut)?;
1030    if !v_mut.is_empty() {
1031        return Err(Error::new(
1032            ErrorKind::InvalidData,
1033            crate::de::ERROR_NOT_ALL_BYTES_READ,
1034        ));
1035    }
1036    Ok(object)
1037}
1038
1039/// Deserializes an object from a reader.
1040/// # Example
1041/// ```
1042/// use borsh::{BorshDeserialize, BorshSerialize, from_reader, to_vec};
1043///
1044/// /// derive is only available if borsh is built with `features = ["derive"]`
1045/// # #[cfg(feature = "derive")]
1046/// #[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
1047/// struct MyStruct {
1048///     a: u64,
1049///     b: Vec<u8>,
1050/// }
1051///
1052/// # #[cfg(feature = "derive")]
1053/// let original = MyStruct { a: 10, b: vec![1, 2, 3] };
1054/// # #[cfg(feature = "derive")]
1055/// let encoded = to_vec(&original).unwrap();
1056/// # #[cfg(feature = "derive")]
1057/// let decoded = from_reader::<_, MyStruct>(&mut encoded.as_slice()).unwrap();
1058/// # #[cfg(feature = "derive")]
1059/// assert_eq!(original, decoded);
1060/// ```
1061pub fn from_reader<R: Read, T: BorshDeserialize>(reader: &mut R) -> Result<T> {
1062    let result = T::deserialize_reader(reader)?;
1063    let mut buf = [0u8; 1];
1064    match reader.read_exact(&mut buf) {
1065        Err(f) if f.kind() == ErrorKind::UnexpectedEof => Ok(result),
1066        _ => Err(Error::new(ErrorKind::InvalidData, ERROR_NOT_ALL_BYTES_READ)),
1067    }
1068}