borsh/de/
mod.rs

1use core::{
2    convert::TryInto,
3    hash::Hash,
4    mem::{forget, size_of},
5};
6
7use crate::maybestd::{
8    borrow::{Borrow, Cow, ToOwned},
9    boxed::Box,
10    collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque},
11    format,
12    io::{Error, ErrorKind, Result},
13    string::{String, ToString},
14    vec::Vec,
15};
16
17mod hint;
18
19const ERROR_NOT_ALL_BYTES_READ: &str = "Not all bytes read";
20const ERROR_UNEXPECTED_LENGTH_OF_INPUT: &str = "Unexpected length of input";
21
22/// A data-structure that can be de-serialized from binary format by NBOR.
23pub trait BorshDeserialize: Sized {
24    /// Deserializes this instance from a given slice of bytes.
25    /// Updates the buffer to point at the remaining bytes.
26    fn deserialize(buf: &mut &[u8]) -> Result<Self>;
27
28    /// Deserialize this instance from a slice of bytes.
29    fn try_from_slice(v: &[u8]) -> Result<Self> {
30        let mut v_mut = v;
31        let result = Self::deserialize(&mut v_mut)?;
32        if !v_mut.is_empty() {
33            return Err(Error::new(ErrorKind::InvalidData, ERROR_NOT_ALL_BYTES_READ));
34        }
35        Ok(result)
36    }
37
38    /// Whether Self is u8.
39    /// NOTE: `Vec<u8>` is the most common use-case for serialization and deserialization, it's
40    /// worth handling it as a special case to improve performance.
41    /// It's a workaround for specific `Vec<u8>` implementation versus generic `Vec<T>`
42    /// implementation. See https://github.com/rust-lang/rfcs/pull/1210 for details.
43    #[inline]
44    fn is_u8() -> bool {
45        false
46    }
47}
48
49impl BorshDeserialize for u8 {
50    #[inline]
51    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
52        if buf.is_empty() {
53            return Err(Error::new(
54                ErrorKind::InvalidInput,
55                ERROR_UNEXPECTED_LENGTH_OF_INPUT,
56            ));
57        }
58        let res = buf[0];
59        *buf = &buf[1..];
60        Ok(res)
61    }
62
63    #[inline]
64    fn is_u8() -> bool {
65        true
66    }
67}
68
69macro_rules! impl_for_integer {
70    ($type: ident) => {
71        impl BorshDeserialize for $type {
72            #[inline]
73            fn deserialize(buf: &mut &[u8]) -> Result<Self> {
74                if buf.len() < size_of::<$type>() {
75                    return Err(Error::new(
76                        ErrorKind::InvalidInput,
77                        ERROR_UNEXPECTED_LENGTH_OF_INPUT,
78                    ));
79                }
80                let res = $type::from_le_bytes(buf[..size_of::<$type>()].try_into().unwrap());
81                *buf = &buf[size_of::<$type>()..];
82                Ok(res)
83            }
84        }
85    };
86}
87
88impl_for_integer!(i8);
89impl_for_integer!(i16);
90impl_for_integer!(i32);
91impl_for_integer!(i64);
92impl_for_integer!(i128);
93impl_for_integer!(u16);
94impl_for_integer!(u32);
95impl_for_integer!(u64);
96impl_for_integer!(u128);
97
98// Note NaNs have a portability issue. Specifically, signalling NaNs on MIPS are quiet NaNs on x86,
99// and vice-versa. We disallow NaNs to avoid this issue.
100macro_rules! impl_for_float {
101    ($type: ident, $int_type: ident) => {
102        impl BorshDeserialize for $type {
103            #[inline]
104            fn deserialize(buf: &mut &[u8]) -> Result<Self> {
105                if buf.len() < size_of::<$type>() {
106                    return Err(Error::new(
107                        ErrorKind::InvalidInput,
108                        ERROR_UNEXPECTED_LENGTH_OF_INPUT,
109                    ));
110                }
111                let res = $type::from_bits($int_type::from_le_bytes(
112                    buf[..size_of::<$int_type>()].try_into().unwrap(),
113                ));
114                *buf = &buf[size_of::<$int_type>()..];
115                if res.is_nan() {
116                    return Err(Error::new(
117                        ErrorKind::InvalidInput,
118                        "For portability reasons we do not allow to deserialize NaNs.",
119                    ));
120                }
121                Ok(res)
122            }
123        }
124    };
125}
126
127impl_for_float!(f32, u32);
128impl_for_float!(f64, u64);
129
130impl BorshDeserialize for bool {
131    #[inline]
132    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
133        if buf.is_empty() {
134            return Err(Error::new(
135                ErrorKind::InvalidInput,
136                ERROR_UNEXPECTED_LENGTH_OF_INPUT,
137            ));
138        }
139        let b = buf[0];
140        *buf = &buf[1..];
141        if b == 0 {
142            Ok(false)
143        } else if b == 1 {
144            Ok(true)
145        } else {
146            let msg = format!("Invalid bool representation: {}", b);
147
148            Err(Error::new(ErrorKind::InvalidInput, msg))
149        }
150    }
151}
152
153impl<T> BorshDeserialize for Option<T>
154where
155    T: BorshDeserialize,
156{
157    #[inline]
158    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
159        if buf.is_empty() {
160            return Err(Error::new(
161                ErrorKind::InvalidInput,
162                ERROR_UNEXPECTED_LENGTH_OF_INPUT,
163            ));
164        }
165        let flag = buf[0];
166        *buf = &buf[1..];
167        if flag == 0 {
168            Ok(None)
169        } else if flag == 1 {
170            Ok(Some(T::deserialize(buf)?))
171        } else {
172            let msg = format!(
173                "Invalid Option representation: {}. The first byte must be 0 or 1",
174                flag
175            );
176
177            Err(Error::new(ErrorKind::InvalidInput, msg))
178        }
179    }
180}
181
182impl<T, E> BorshDeserialize for core::result::Result<T, E>
183where
184    T: BorshDeserialize,
185    E: BorshDeserialize,
186{
187    #[inline]
188    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
189        if buf.is_empty() {
190            return Err(Error::new(
191                ErrorKind::InvalidInput,
192                ERROR_UNEXPECTED_LENGTH_OF_INPUT,
193            ));
194        }
195        let flag = buf[0];
196        *buf = &buf[1..];
197        if flag == 0 {
198            Ok(Err(E::deserialize(buf)?))
199        } else if flag == 1 {
200            Ok(Ok(T::deserialize(buf)?))
201        } else {
202            let msg = format!(
203                "Invalid Result representation: {}. The first byte must be 0 or 1",
204                flag
205            );
206
207            Err(Error::new(ErrorKind::InvalidInput, msg))
208        }
209    }
210}
211
212impl BorshDeserialize for String {
213    #[inline]
214    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
215        String::from_utf8(Vec::<u8>::deserialize(buf)?).map_err(|err| {
216            let msg = err.to_string();
217            Error::new(ErrorKind::InvalidData, msg)
218        })
219    }
220}
221
222impl<T> BorshDeserialize for Vec<T>
223where
224    T: BorshDeserialize,
225{
226    #[inline]
227    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
228        let len = u32::deserialize(buf)?;
229        if len == 0 {
230            Ok(Vec::new())
231        } else if T::is_u8() && size_of::<T>() == size_of::<u8>() {
232            let len = len.try_into().map_err(|_| ErrorKind::InvalidInput)?;
233            if buf.len() < len {
234                return Err(Error::new(
235                    ErrorKind::InvalidInput,
236                    ERROR_UNEXPECTED_LENGTH_OF_INPUT,
237                ));
238            }
239            let result = buf[..len].to_vec();
240            *buf = &buf[len..];
241            // See comment from https://doc.rust-lang.org/std/mem/fn.transmute.html
242            // The no-copy, unsafe way, still using transmute, but not UB.
243            // This is equivalent to the original, but safer, and reuses the
244            // same `Vec` internals. Therefore, the new inner type must have the
245            // exact same size, and the same alignment, as the old type.
246            //
247            // The size of the memory should match because `size_of::<T>() == size_of::<u8>()`.
248            //
249            // `T::is_u8()` is a workaround for not being able to implement `Vec<u8>` separately.
250            let result = unsafe {
251                // Ensure the original vector is not dropped.
252                let mut v_clone = core::mem::ManuallyDrop::new(result);
253                Vec::from_raw_parts(
254                    v_clone.as_mut_ptr() as *mut T,
255                    v_clone.len(),
256                    v_clone.capacity(),
257                )
258            };
259            Ok(result)
260        } else if size_of::<T>() == 0 {
261            let mut result = Vec::new();
262            result.push(T::deserialize(buf)?);
263
264            let p = result.as_mut_ptr();
265            unsafe {
266                forget(result);
267                let len = len.try_into().map_err(|_| ErrorKind::InvalidInput)?;
268                let result = Vec::from_raw_parts(p, len, len);
269                Ok(result)
270            }
271        } else {
272            // TODO(16): return capacity allocation when we can safely do that.
273            let mut result = Vec::with_capacity(hint::cautious::<T>(len));
274            for _ in 0..len {
275                result.push(T::deserialize(buf)?);
276            }
277            Ok(result)
278        }
279    }
280}
281
282impl<T> BorshDeserialize for Cow<'_, T>
283where
284    T: ToOwned + ?Sized,
285    T::Owned: BorshDeserialize,
286{
287    #[inline]
288    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
289        Ok(Cow::Owned(BorshDeserialize::deserialize(buf)?))
290    }
291}
292
293impl<T> BorshDeserialize for VecDeque<T>
294where
295    T: BorshDeserialize,
296{
297    #[inline]
298    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
299        let vec = <Vec<T>>::deserialize(buf)?;
300        Ok(vec.into())
301    }
302}
303
304impl<T> BorshDeserialize for LinkedList<T>
305where
306    T: BorshDeserialize,
307{
308    #[inline]
309    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
310        let vec = <Vec<T>>::deserialize(buf)?;
311        Ok(vec.into_iter().collect::<LinkedList<T>>())
312    }
313}
314
315impl<T> BorshDeserialize for BinaryHeap<T>
316where
317    T: BorshDeserialize + Ord,
318{
319    #[inline]
320    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
321        let vec = <Vec<T>>::deserialize(buf)?;
322        Ok(vec.into_iter().collect::<BinaryHeap<T>>())
323    }
324}
325
326impl<T> BorshDeserialize for HashSet<T>
327where
328    T: BorshDeserialize + Eq + Hash,
329{
330    #[inline]
331    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
332        let vec = <Vec<T>>::deserialize(buf)?;
333        Ok(vec.into_iter().collect::<HashSet<T>>())
334    }
335}
336
337impl<K, V> BorshDeserialize for HashMap<K, V>
338where
339    K: BorshDeserialize + Eq + Hash,
340    V: BorshDeserialize,
341{
342    #[inline]
343    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
344        let len = u32::deserialize(buf)?;
345        // TODO(16): return capacity allocation when we can safely do that.
346        let mut result = HashMap::new();
347        for _ in 0..len {
348            let key = K::deserialize(buf)?;
349            let value = V::deserialize(buf)?;
350            result.insert(key, value);
351        }
352        Ok(result)
353    }
354}
355
356impl<T> BorshDeserialize for BTreeSet<T>
357where
358    T: BorshDeserialize + Ord,
359{
360    #[inline]
361    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
362        let vec = <Vec<T>>::deserialize(buf)?;
363        Ok(vec.into_iter().collect::<BTreeSet<T>>())
364    }
365}
366
367impl<K, V> BorshDeserialize for BTreeMap<K, V>
368where
369    K: BorshDeserialize + Ord + core::hash::Hash,
370    V: BorshDeserialize,
371{
372    #[inline]
373    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
374        let len = u32::deserialize(buf)?;
375        let mut result = BTreeMap::new();
376        for _ in 0..len {
377            let key = K::deserialize(buf)?;
378            let value = V::deserialize(buf)?;
379            result.insert(key, value);
380        }
381        Ok(result)
382    }
383}
384
385#[cfg(feature = "std")]
386impl BorshDeserialize for std::net::SocketAddr {
387    #[inline]
388    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
389        let kind = u8::deserialize(buf)?;
390        match kind {
391            0 => std::net::SocketAddrV4::deserialize(buf).map(std::net::SocketAddr::V4),
392            1 => std::net::SocketAddrV6::deserialize(buf).map(std::net::SocketAddr::V6),
393            value => Err(Error::new(
394                ErrorKind::InvalidInput,
395                format!("Invalid SocketAddr variant: {}", value),
396            )),
397        }
398    }
399}
400
401#[cfg(feature = "std")]
402impl BorshDeserialize for std::net::SocketAddrV4 {
403    #[inline]
404    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
405        let ip = std::net::Ipv4Addr::deserialize(buf)?;
406        let port = u16::deserialize(buf)?;
407        Ok(std::net::SocketAddrV4::new(ip, port))
408    }
409}
410
411#[cfg(feature = "std")]
412impl BorshDeserialize for std::net::SocketAddrV6 {
413    #[inline]
414    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
415        let ip = std::net::Ipv6Addr::deserialize(buf)?;
416        let port = u16::deserialize(buf)?;
417        Ok(std::net::SocketAddrV6::new(ip, port, 0, 0))
418    }
419}
420
421#[cfg(feature = "std")]
422impl BorshDeserialize for std::net::Ipv4Addr {
423    #[inline]
424    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
425        if buf.len() < 4 {
426            return Err(Error::new(
427                ErrorKind::InvalidInput,
428                ERROR_UNEXPECTED_LENGTH_OF_INPUT,
429            ));
430        }
431        let bytes: [u8; 4] = buf[..4].try_into().unwrap();
432        let res = std::net::Ipv4Addr::from(bytes);
433        *buf = &buf[4..];
434        Ok(res)
435    }
436}
437
438#[cfg(feature = "std")]
439impl BorshDeserialize for std::net::Ipv6Addr {
440    #[inline]
441    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
442        if buf.len() < 16 {
443            return Err(Error::new(
444                ErrorKind::InvalidInput,
445                ERROR_UNEXPECTED_LENGTH_OF_INPUT,
446            ));
447        }
448        let bytes: [u8; 16] = buf[..16].try_into().unwrap();
449        let res = std::net::Ipv6Addr::from(bytes);
450        *buf = &buf[16..];
451        Ok(res)
452    }
453}
454
455impl<T, U> BorshDeserialize for Box<T>
456where
457    U: Into<Box<T>> + Borrow<T>,
458    T: ToOwned<Owned = U> + ?Sized,
459    T::Owned: BorshDeserialize,
460{
461    fn deserialize(buf: &mut &[u8]) -> Result<Self> {
462        Ok(T::Owned::deserialize(buf)?.into())
463    }
464}
465
466macro_rules! impl_arrays {
467    ($($len:expr)+) => {
468    $(
469        impl<T> BorshDeserialize for [T; $len]
470        where
471            T: BorshDeserialize + Default + Copy
472        {
473            #[inline]
474            fn deserialize(buf: &mut &[u8]) -> Result<Self> {
475                let mut result = [T::default(); $len];
476                if T::is_u8() && size_of::<T>() == size_of::<u8>() {
477                    if buf.len() < $len {
478                        return Err(Error::new(
479                            ErrorKind::InvalidInput,
480                            ERROR_UNEXPECTED_LENGTH_OF_INPUT,
481                        ));
482                    }
483                    // The size of the memory should match because `size_of::<T>() == size_of::<u8>()`.
484                    // `T::is_u8()` is a workaround for not being able to implement `[u8; *]` separately.
485                    result.copy_from_slice(unsafe { core::slice::from_raw_parts(buf.as_ptr() as *const T, $len) });
486                    *buf = &buf[$len..];
487                } else {
488                    for i in 0..$len {
489                        result[i] = T::deserialize(buf)?;
490                    }
491                }
492                Ok(result)
493            }
494        }
495    )+
496    };
497}
498
499impl_arrays!(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 64 65 128 256 512 1024 2048);
500
501impl<T> BorshDeserialize for [T; 0]
502where
503    T: BorshDeserialize + Default + Copy,
504{
505    #[inline]
506    fn deserialize(_buf: &mut &[u8]) -> Result<Self> {
507        Ok([T::default(); 0])
508    }
509}
510
511impl BorshDeserialize for () {
512    fn deserialize(_buf: &mut &[u8]) -> Result<Self> {
513        Ok(())
514    }
515}
516
517macro_rules! impl_tuple {
518    ($($name:ident)+) => {
519      impl<$($name),+> BorshDeserialize for ($($name),+)
520      where $($name: BorshDeserialize,)+
521      {
522        #[inline]
523        fn deserialize(buf: &mut &[u8]) -> Result<Self> {
524
525            Ok(($($name::deserialize(buf)?,)+))
526        }
527      }
528    };
529}
530
531impl_tuple!(T0 T1);
532impl_tuple!(T0 T1 T2);
533impl_tuple!(T0 T1 T2 T3);
534impl_tuple!(T0 T1 T2 T3 T4);
535impl_tuple!(T0 T1 T2 T3 T4 T5);
536impl_tuple!(T0 T1 T2 T3 T4 T5 T6);
537impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7);
538impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8);
539impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9);
540impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
541impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
542impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);
543impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13);
544impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14);
545impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
546impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16);
547impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17);
548impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18);
549impl_tuple!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19);