rmp_serde/
decode.rs

1//! Generic MessagePack deserialization.
2
3use crate::config::sealed::SerializerConfig as _;
4use std::convert::TryInto;
5use std::error;
6use std::fmt::{self, Display, Formatter};
7use std::io::{self, Cursor, ErrorKind, Read};
8use std::marker::PhantomData;
9use std::num::TryFromIntError;
10use std::str::{self, Utf8Error};
11
12use byteorder::{self, ReadBytesExt};
13
14use serde;
15use serde::de::value::SeqDeserializer;
16use serde::de::{self, Deserialize, DeserializeOwned, DeserializeSeed, Unexpected, Visitor};
17use serde::forward_to_deserialize_any;
18
19use rmp;
20use rmp::decode::{self, DecodeStringError, MarkerReadError, NumValueReadError, RmpRead, ValueReadError};
21use rmp::Marker;
22
23use crate::config::{BinaryConfig, DefaultConfig, HumanReadableConfig, SerializerConfig};
24use crate::MSGPACK_EXT_STRUCT_NAME;
25
26/// Enum representing errors that can occur while decoding MessagePack data.
27#[derive(Debug)]
28pub enum Error {
29    /// The enclosed I/O error occurred while trying to read a MessagePack
30    /// marker.
31    InvalidMarkerRead(io::Error),
32    /// The enclosed I/O error occurred while trying to read the encoded
33    /// MessagePack data.
34    InvalidDataRead(io::Error),
35    /// A mismatch occurred between the decoded and expected value types.
36    TypeMismatch(Marker),
37    /// A numeric cast failed due to an out-of-range error.
38    OutOfRange,
39    /// A decoded array did not have the enclosed expected length.
40    LengthMismatch(u32),
41    /// An otherwise uncategorized error occurred. See the enclosed `String` for
42    /// details.
43    Uncategorized(String),
44    /// A general error occurred while deserializing the expected type. See the
45    /// enclosed `String` for details.
46    Syntax(String),
47    /// An encoded string could not be parsed as UTF-8.
48    Utf8Error(Utf8Error),
49    /// The depth limit was exceeded.
50    DepthLimitExceeded,
51}
52
53macro_rules! depth_count(
54    ( $counter:expr, $expr:expr ) => {
55        {
56            $counter -= 1;
57            if $counter == 0 {
58                return Err(Error::DepthLimitExceeded)
59            }
60            let res = $expr;
61            $counter += 1;
62            res
63        }
64    }
65);
66
67impl error::Error for Error {
68    #[cold]
69    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
70        match *self {
71            Error::TypeMismatch(..) => None,
72            Error::InvalidMarkerRead(ref err) => Some(err),
73            Error::InvalidDataRead(ref err) => Some(err),
74            Error::LengthMismatch(..) => None,
75            Error::OutOfRange => None,
76            Error::Uncategorized(..) => None,
77            Error::Syntax(..) => None,
78            Error::Utf8Error(ref err) => Some(err),
79            Error::DepthLimitExceeded => None,
80        }
81    }
82}
83
84impl de::Error for Error {
85    #[cold]
86    fn custom<T: Display>(msg: T) -> Self {
87        Error::Syntax(msg.to_string())
88    }
89}
90
91impl Display for Error {
92    #[cold]
93    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> {
94        match *self {
95            Error::InvalidMarkerRead(ref err) => write!(fmt, "IO error while reading marker: {err}"),
96            Error::InvalidDataRead(ref err) => write!(fmt, "IO error while reading data: {err}"),
97            Error::TypeMismatch(ref actual_marker) => {
98                write!(fmt, "wrong msgpack marker {actual_marker:?}")
99            }
100            Error::OutOfRange => fmt.write_str("numeric cast found out of range"),
101            Error::LengthMismatch(expected_length) => write!(
102                fmt,
103                "array had incorrect length, expected {expected_length}"
104            ),
105            Error::Uncategorized(ref msg) => write!(fmt, "uncategorized error: {msg}"),
106            Error::Syntax(ref msg) => fmt.write_str(msg),
107            Error::Utf8Error(ref err) => write!(fmt, "string found to be invalid utf8: {err}"),
108            Error::DepthLimitExceeded => fmt.write_str("depth limit exceeded"),
109        }
110    }
111}
112
113impl From<MarkerReadError> for Error {
114    #[cold]
115    fn from(err: MarkerReadError) -> Error {
116        match err {
117            MarkerReadError(err) => Error::InvalidMarkerRead(err),
118        }
119    }
120}
121
122impl From<Utf8Error> for Error {
123    #[cold]
124    fn from(err: Utf8Error) -> Error {
125        Error::Utf8Error(err)
126    }
127}
128
129impl From<ValueReadError> for Error {
130    #[cold]
131    fn from(err: ValueReadError) -> Error {
132        match err {
133            ValueReadError::TypeMismatch(marker) => Error::TypeMismatch(marker),
134            ValueReadError::InvalidMarkerRead(err) => Error::InvalidMarkerRead(err),
135            ValueReadError::InvalidDataRead(err) => Error::InvalidDataRead(err),
136        }
137    }
138}
139
140impl From<NumValueReadError> for Error {
141    #[cold]
142    fn from(err: NumValueReadError) -> Error {
143        match err {
144            NumValueReadError::TypeMismatch(marker) => Error::TypeMismatch(marker),
145            NumValueReadError::InvalidMarkerRead(err) => Error::InvalidMarkerRead(err),
146            NumValueReadError::InvalidDataRead(err) => Error::InvalidDataRead(err),
147            NumValueReadError::OutOfRange => Error::OutOfRange,
148        }
149    }
150}
151
152impl<'a> From<DecodeStringError<'a>> for Error {
153    #[cold]
154    fn from(err: DecodeStringError<'_>) -> Error {
155        match err {
156            DecodeStringError::InvalidMarkerRead(err) => Error::InvalidMarkerRead(err),
157            DecodeStringError::InvalidDataRead(err) => Error::InvalidDataRead(err),
158            DecodeStringError::TypeMismatch(marker) => Error::TypeMismatch(marker),
159            DecodeStringError::BufferSizeTooSmall(..) => Error::Uncategorized("BufferSizeTooSmall".to_string()),
160            DecodeStringError::InvalidUtf8(..) => Error::Uncategorized("InvalidUtf8".to_string()),
161        }
162    }
163}
164
165impl From<TryFromIntError> for Error {
166    #[cold]
167    fn from(_: TryFromIntError) -> Self {
168        Error::OutOfRange
169    }
170}
171
172/// A Deserializer that reads bytes from a buffer.
173///
174/// # Note
175///
176/// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying
177/// operation is retried.
178#[derive(Debug)]
179pub struct Deserializer<R, C = DefaultConfig> {
180    rd: R,
181    _config: PhantomData<C>,
182    is_human_readable: bool,
183    marker: Option<Marker>,
184    depth: u16,
185}
186
187impl<R: Read, C> Deserializer<R, C> {
188    #[inline]
189    fn take_or_read_marker(&mut self) -> Result<Marker, MarkerReadError> {
190        self.marker
191            .take()
192            .map_or_else(|| rmp::decode::read_marker(&mut self.rd), Ok)
193    }
194
195    #[inline]
196    fn peek_or_read_marker(&mut self) -> Result<Marker, MarkerReadError> {
197        if let Some(m) = self.marker {
198            Ok(m)
199        } else {
200            let m = rmp::decode::read_marker(&mut self.rd)?;
201            Ok(self.marker.insert(m).to_owned())
202        }
203    }
204}
205
206impl<R: Read> Deserializer<ReadReader<R>, DefaultConfig> {
207    /// Constructs a new `Deserializer` by consuming the given reader.
208    #[inline]
209    pub fn new(rd: R) -> Self {
210        Self {
211            rd: ReadReader::new(rd),
212            _config: PhantomData,
213            is_human_readable: DefaultConfig.is_human_readable(),
214            // Cached marker in case of deserializing optional values.
215            marker: None,
216            depth: 1024,
217        }
218    }
219}
220
221impl<R: Read, C> Deserializer<ReadReader<R>, C> {
222    /// Gets a reference to the underlying reader in this decoder.
223    #[inline(always)]
224    pub fn get_ref(&self) -> &R {
225        &self.rd.rd
226    }
227
228    /// Gets a mutable reference to the underlying reader in this decoder.
229    #[inline(always)]
230    pub fn get_mut(&mut self) -> &mut R {
231        &mut self.rd.rd
232    }
233
234    /// Consumes this deserializer returning the underlying reader.
235    #[inline]
236    pub fn into_inner(self) -> R {
237        self.rd.rd
238    }
239}
240
241impl<R: Read, C: SerializerConfig> Deserializer<R, C> {
242    /// Consumes this deserializer and returns a new one, which will deserialize types with
243    /// human-readable representations (`Deserializer::is_human_readable` will return `true`).
244    ///
245    /// This is primarily useful if you need to interoperate with serializations produced by older
246    /// versions of `rmp-serde`.
247    #[inline]
248    pub fn with_human_readable(self) -> Deserializer<R, HumanReadableConfig<C>> {
249        let Deserializer { rd, _config: _, is_human_readable: _, marker, depth } = self;
250        Deserializer {
251            rd,
252            is_human_readable: true,
253            _config: PhantomData,
254            marker,
255            depth,
256        }
257    }
258
259    /// Consumes this deserializer and returns a new one, which will deserialize types with
260    /// binary representations (`Deserializer::is_human_readable` will return `false`).
261    ///
262    /// This is the default MessagePack deserialization mechanism, consuming the most compact
263    /// representation.
264    #[inline]
265    pub fn with_binary(self) -> Deserializer<R, BinaryConfig<C>> {
266        let Deserializer { rd, _config: _, is_human_readable: _, marker, depth } = self;
267        Deserializer {
268            rd,
269            is_human_readable: false,
270            _config: PhantomData,
271            marker,
272            depth,
273        }
274    }
275}
276
277impl<R: AsRef<[u8]>> Deserializer<ReadReader<Cursor<R>>> {
278    /// Returns the current position of this deserializer, i.e. how many bytes were read.
279    #[inline(always)]
280    pub fn position(&self) -> u64 {
281        self.rd.rd.position()
282    }
283}
284
285impl<'de, R> Deserializer<ReadRefReader<'de, R>>
286where
287    R: AsRef<[u8]> + ?Sized,
288{
289    /// Constructs a new `Deserializer` from the given byte slice.
290    #[inline(always)]
291    pub fn from_read_ref(rd: &'de R) -> Self {
292        Deserializer {
293            rd: ReadRefReader::new(rd),
294            is_human_readable: DefaultConfig.is_human_readable(),
295            _config: PhantomData,
296            marker: None,
297            depth: 1024,
298        }
299    }
300
301    /// Gets a reference to the underlying reader in this decoder.
302    #[inline(always)]
303    #[must_use]
304    pub fn get_ref(&self) -> &R {
305        self.rd.whole_slice
306    }
307}
308
309impl<'de, R: ReadSlice<'de>, C: SerializerConfig> Deserializer<R, C> {
310    /// Changes the maximum nesting depth that is allowed
311    #[inline(always)]
312    pub fn set_max_depth(&mut self, depth: usize) {
313        self.depth = depth.min(u16::MAX as _) as u16;
314    }
315}
316
317#[inline(never)]
318fn read_i128_marker<'de, R: ReadSlice<'de>>(marker: Marker, rd: &mut R) -> Result<i128, Error> {
319    Ok(match marker {
320        Marker::FixPos(val) => val.into(),
321        Marker::FixNeg(val) => val.into(),
322        Marker::U8 => rd.read_data_u8()?.into(),
323        Marker::U16 => rd.read_data_u16()?.into(),
324        Marker::U32 => rd.read_data_u32()?.into(),
325        Marker::U64 => rd.read_data_u64()?.into(),
326        Marker::I8 => rd.read_data_i8()?.into(),
327        Marker::I16 => rd.read_data_i16()?.into(),
328        Marker::I32 => rd.read_data_i32()?.into(),
329        Marker::I64 => rd.read_data_i64()?.into(),
330        Marker::Bin8 => {
331            let len = read_u8(&mut *rd)?;
332            read_128_buf(rd, len)?
333        },
334        Marker::FixArray(len) => {
335            read_128_buf(rd, len)?
336        },
337        marker => return Err(Error::TypeMismatch(marker)),
338    })
339}
340
341fn read_128_buf<'de, R: ReadSlice<'de>>(rd: &mut R, len: u8) -> Result<i128, Error> {
342    if len != 16 {
343        return Err(Error::LengthMismatch(16));
344    }
345    let buf = match read_bin_data(rd, 16)? {
346        Reference::Borrowed(buf) => buf,
347        Reference::Copied(buf) => buf,
348    };
349    Ok(i128::from_be_bytes(buf.try_into().map_err(|_| Error::LengthMismatch(16))?))
350}
351
352fn read_str_data<'de, V, R>(rd: &mut R, len: u32, visitor: V) -> Result<V::Value, Error>
353    where V: Visitor<'de>, R: ReadSlice<'de>
354{
355    match read_bin_data(rd, len)? {
356        Reference::Borrowed(buf) => {
357            match str::from_utf8(buf) {
358                Ok(s) => visitor.visit_borrowed_str(s),
359                Err(err) => {
360                    // Allow to unpack invalid UTF-8 bytes into a byte array.
361                    match visitor.visit_borrowed_bytes::<Error>(buf) {
362                        Ok(buf) => Ok(buf),
363                        Err(..) => Err(Error::Utf8Error(err)),
364                    }
365                }
366            }
367        }
368        Reference::Copied(buf) => {
369            match str::from_utf8(buf) {
370                Ok(s) => visitor.visit_str(s),
371                Err(err) => {
372                    // Allow to unpack invalid UTF-8 bytes into a byte array.
373                    match visitor.visit_bytes::<Error>(buf) {
374                        Ok(buf) => Ok(buf),
375                        Err(..) => Err(Error::Utf8Error(err)),
376                    }
377                }
378            }
379        }
380    }
381}
382
383fn read_bin_data<'a, 'de, R: ReadSlice<'de>>(rd: &'a mut R, len: u32) -> Result<Reference<'de, 'a, [u8]>, Error> {
384    rd.read_slice(len as usize).map_err(Error::InvalidDataRead)
385}
386
387fn read_u8<R: Read>(rd: &mut R) -> Result<u8, Error> {
388    byteorder::ReadBytesExt::read_u8(rd).map_err(Error::InvalidDataRead)
389}
390
391fn read_u16<R: Read>(rd: &mut R) -> Result<u16, Error> {
392    rd.read_u16::<byteorder::BigEndian>()
393        .map_err(Error::InvalidDataRead)
394}
395
396fn read_u32<R: Read>(rd: &mut R) -> Result<u32, Error> {
397    rd.read_u32::<byteorder::BigEndian>()
398        .map_err(Error::InvalidDataRead)
399}
400
401fn ext_len<R: Read>(rd: &mut R, marker: Marker) -> Result<u32, Error> {
402    Ok(match marker {
403        Marker::FixExt1 => 1,
404        Marker::FixExt2 => 2,
405        Marker::FixExt4 => 4,
406        Marker::FixExt8 => 8,
407        Marker::FixExt16 => 16,
408        Marker::Ext8 => u32::from(read_u8(rd)?),
409        Marker::Ext16 => u32::from(read_u16(rd)?),
410        Marker::Ext32 => read_u32(rd)?,
411        _ => return Err(Error::TypeMismatch(marker)),
412    })
413}
414
415#[derive(Debug)]
416enum ExtDeserializerState {
417    New,
418    ReadTag,
419    ReadBinary,
420}
421
422#[derive(Debug)]
423struct ExtDeserializer<'a, R, C> {
424    rd: &'a mut R,
425    _config: PhantomData<C>,
426    len: u32,
427    state: ExtDeserializerState,
428}
429
430impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> ExtDeserializer<'a, R, C> {
431    fn new(d: &'a mut Deserializer<R, C>, len: u32) -> Self {
432        ExtDeserializer {
433            rd: &mut d.rd,
434            _config: d._config,
435            len,
436            state: ExtDeserializerState::New,
437        }
438    }
439}
440
441impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::Deserializer<'de> for ExtDeserializer<'a, R, C> {
442    type Error = Error;
443
444    #[inline(always)]
445    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
446        where V: Visitor<'de>
447    {
448        visitor.visit_seq(self)
449    }
450
451    forward_to_deserialize_any! {
452        bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
453        seq bytes byte_buf map unit_struct newtype_struct
454        struct identifier tuple enum ignored_any tuple_struct
455    }
456}
457
458impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::SeqAccess<'de> for ExtDeserializer<'a, R, C> {
459    type Error = Error;
460
461    #[inline]
462    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
463    where
464        T: DeserializeSeed<'de>,
465    {
466        match self.state {
467            ExtDeserializerState::New | ExtDeserializerState::ReadTag => Ok(Some(seed.deserialize(self)?)),
468            ExtDeserializerState::ReadBinary => Ok(None),
469        }
470    }
471}
472
473/// Deserializer for Ext `SeqAccess`
474impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::Deserializer<'de> for &mut ExtDeserializer<'a, R, C> {
475    type Error = Error;
476
477    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
478        where V: Visitor<'de>
479    {
480        match self.state {
481            ExtDeserializerState::New => {
482                let tag = self.rd.read_data_i8()?;
483                self.state = ExtDeserializerState::ReadTag;
484                visitor.visit_i8(tag)
485            }
486            ExtDeserializerState::ReadTag => {
487                let data = self.rd.read_slice(self.len as usize).map_err(Error::InvalidDataRead)?;
488                self.state = ExtDeserializerState::ReadBinary;
489                match data {
490                    Reference::Borrowed(bytes) => visitor.visit_borrowed_bytes(bytes),
491                    Reference::Copied(bytes) => visitor.visit_bytes(bytes),
492                }
493            }
494            ExtDeserializerState::ReadBinary => {
495                debug_assert!(false);
496                Err(Error::TypeMismatch(Marker::Reserved))
497            },
498        }
499    }
500
501    forward_to_deserialize_any! {
502        bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
503        seq bytes byte_buf map unit_struct newtype_struct
504        tuple_struct struct identifier tuple enum ignored_any
505    }
506}
507
508#[inline(never)]
509fn any_num<'de, R: ReadSlice<'de>, V: Visitor<'de>>(rd: &mut R, visitor: V, marker: Marker) -> Result<V::Value, Error> {
510    match marker {
511        Marker::Null => visitor.visit_unit(),
512        Marker::True |
513        Marker::False => visitor.visit_bool(marker == Marker::True),
514        Marker::FixPos(val) => visitor.visit_u8(val),
515        Marker::FixNeg(val) => visitor.visit_i8(val),
516        Marker::U8 => visitor.visit_u8(rd.read_data_u8()?),
517        Marker::U16 => visitor.visit_u16(rd.read_data_u16()?),
518        Marker::U32 => visitor.visit_u32(rd.read_data_u32()?),
519        Marker::U64 => visitor.visit_u64(rd.read_data_u64()?),
520        Marker::I8 => visitor.visit_i8(rd.read_data_i8()?),
521        Marker::I16 => visitor.visit_i16(rd.read_data_i16()?),
522        Marker::I32 => visitor.visit_i32(rd.read_data_i32()?),
523        Marker::I64 => visitor.visit_i64(rd.read_data_i64()?),
524        Marker::F32 => visitor.visit_f32(rd.read_data_f32()?),
525        Marker::F64 => visitor.visit_f64(rd.read_data_f64()?),
526        other_marker => Err(Error::TypeMismatch(other_marker)),
527    }
528}
529
530impl<'de, R: ReadSlice<'de>, C: SerializerConfig> Deserializer<R, C> {
531    fn any_inner<V: Visitor<'de>>(&mut self, visitor: V, allow_bytes: bool) -> Result<V::Value, Error> {
532        let marker = self.take_or_read_marker()?;
533        match marker {
534            Marker::Null |
535            Marker::True |
536            Marker::False |
537            Marker::FixPos(_) |
538            Marker::FixNeg(_) |
539            Marker::U8 |
540            Marker::U16 |
541            Marker::U32 |
542            Marker::U64 |
543            Marker::I8 |
544            Marker::I16 |
545            Marker::I32 |
546            Marker::I64 |
547            Marker::F32 |
548            Marker::F64 => any_num(&mut self.rd, visitor, marker),
549            Marker::FixStr(_) | Marker::Str8 | Marker::Str16 | Marker::Str32 => {
550                let len = match marker {
551                    Marker::FixStr(len) => Ok(len.into()),
552                    Marker::Str8 => read_u8(&mut self.rd).map(u32::from),
553                    Marker::Str16 => read_u16(&mut self.rd).map(u32::from),
554                    Marker::Str32 => read_u32(&mut self.rd).map(u32::from),
555                    _ => return Err(Error::TypeMismatch(Marker::Reserved)),
556                }?;
557                read_str_data(&mut self.rd, len, visitor)
558            }
559            Marker::FixArray(_) |
560            Marker::Array16 |
561            Marker::Array32 => {
562                let len = match marker {
563                    Marker::FixArray(len) => len.into(),
564                    Marker::Array16 => read_u16(&mut self.rd)?.into(),
565                    Marker::Array32 => read_u32(&mut self.rd)?,
566                    _ => return Err(Error::TypeMismatch(Marker::Reserved)),
567                };
568
569                depth_count!(self.depth, {
570                    let mut seq = SeqAccess::new(self, len);
571                    let res = visitor.visit_seq(&mut seq)?;
572                    match seq.left {
573                        0 => Ok(res),
574                        excess => Err(Error::LengthMismatch(len - excess)),
575                    }
576                })
577            }
578            Marker::FixMap(_) |
579            Marker::Map16 |
580            Marker::Map32 => {
581                let len = match marker {
582                    Marker::FixMap(len) => len.into(),
583                    Marker::Map16 => read_u16(&mut self.rd)?.into(),
584                    Marker::Map32 => read_u32(&mut self.rd)?,
585                    _ => return Err(Error::TypeMismatch(Marker::Reserved)),
586                };
587
588                depth_count!(self.depth, {
589                    let mut seq = MapAccess::new(self, len);
590                    let res = visitor.visit_map(&mut seq)?;
591                    match seq.left {
592                        0 => Ok(res),
593                        excess => Err(Error::LengthMismatch(len - excess)),
594                    }
595                })
596            }
597            Marker::Bin8 | Marker::Bin16 | Marker::Bin32 => {
598                let len = match marker {
599                    Marker::Bin8 => read_u8(&mut self.rd).map(u32::from),
600                    Marker::Bin16 => read_u16(&mut self.rd).map(u32::from),
601                    Marker::Bin32 => read_u32(&mut self.rd).map(u32::from),
602                    _ => return Err(Error::TypeMismatch(Marker::Reserved)),
603                }?;
604                match read_bin_data(&mut self.rd, len)? {
605                    Reference::Borrowed(buf) if allow_bytes => visitor.visit_borrowed_bytes(buf),
606                    Reference::Copied(buf) if allow_bytes => visitor.visit_bytes(buf),
607                    Reference::Borrowed(buf) | Reference::Copied(buf) => {
608                        visitor.visit_seq(SeqDeserializer::new(buf.iter().copied()))
609                    },
610                }
611            }
612            Marker::FixExt1 |
613            Marker::FixExt2 |
614            Marker::FixExt4 |
615            Marker::FixExt8 |
616            Marker::FixExt16 |
617            Marker::Ext8 |
618            Marker::Ext16 |
619            Marker::Ext32 => {
620                let len = ext_len(&mut self.rd, marker)?;
621                depth_count!(self.depth, visitor.visit_newtype_struct(ExtDeserializer::new(self, len)))
622            }
623            Marker::Reserved => Err(Error::TypeMismatch(Marker::Reserved)),
624        }
625    }
626}
627
628impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> serde::Deserializer<'de> for &'a mut Deserializer<R, C> {
629    type Error = Error;
630
631    #[inline(always)]
632    fn is_human_readable(&self) -> bool {
633        self.is_human_readable
634    }
635
636    #[inline(always)]
637    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
638        where V: Visitor<'de>
639    {
640        self.any_inner(visitor, true)
641    }
642
643    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
644        where V: Visitor<'de>
645    {
646        // # Important
647        //
648        // If a nested Option `o ∈ { Option<Opion<t>>, Option<Option<Option<t>>>, ..., Option<Option<...Option<t>...> }`
649        // is visited for the first time, the marker (read from the underlying Reader) will determine
650        // `o`'s innermost type `t`.
651        // For subsequent visits of `o` the marker will not be re-read again but kept until type `t`
652        // is visited.
653        //
654        // # Note
655        //
656        // Round trips of Options where `Option<t> = None` such as `Some(None)` will fail because
657        // they are just seriialized as `nil`. The serialization format has probably to be changed
658        // to solve this. But as serde_json behaves the same, I think it's not worth doing this.
659        let marker = self.take_or_read_marker()?;
660
661        if marker == Marker::Null {
662            visitor.visit_none()
663        } else {
664            // Keep the marker until `o`'s innermost type `t` is visited.
665            self.marker = Some(marker);
666            visitor.visit_some(self)
667        }
668    }
669
670    fn deserialize_enum<V>(self, _name: &str, _variants: &[&str], visitor: V) -> Result<V::Value, Error>
671        where V: Visitor<'de>
672    {
673        let marker = self.peek_or_read_marker()?;
674        match rmp::decode::marker_to_len(&mut self.rd, marker) {
675            Ok(len) => match len {
676                // Enums are either encoded as maps with a single K/V pair
677                // where the K = the variant & V = associated data
678                // or as just the variant
679                1 => {
680                    self.marker = None;
681                    visitor.visit_enum(VariantAccess::new(self))
682                }
683                n => Err(Error::LengthMismatch(n)),
684            },
685            // TODO: Check this is a string
686            Err(_) => visitor.visit_enum(UnitVariantAccess::new(self)),
687        }
688    }
689
690    fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Error>
691        where V: Visitor<'de>
692    {
693        if name == MSGPACK_EXT_STRUCT_NAME {
694            let marker = self.take_or_read_marker()?;
695
696            let len = ext_len(&mut self.rd, marker)?;
697            let ext_de = ExtDeserializer::new(self, len);
698            return visitor.visit_newtype_struct(ext_de);
699        }
700
701        visitor.visit_newtype_struct(self)
702    }
703
704    fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
705        where V: Visitor<'de>
706    {
707        // We need to special case this so that [] is treated as a unit struct when asked for,
708        // but as a sequence otherwise. This is because we serialize unit structs as [] rather
709        // than as 'nil'.
710        match self.take_or_read_marker()? {
711            Marker::Null | Marker::FixArray(0) => visitor.visit_unit(),
712            marker => {
713                self.marker = Some(marker);
714                self.deserialize_any(visitor)
715            }
716        }
717    }
718
719    #[inline]
720    fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
721    where
722        V: Visitor<'de>,
723    {
724        visitor.visit_i128(read_i128_marker(self.take_or_read_marker()?, &mut self.rd)?)
725    }
726
727    #[inline]
728    fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
729    where
730        V: Visitor<'de>,
731    {
732        visitor.visit_u128(read_i128_marker(self.take_or_read_marker()?, &mut self.rd)? as u128)
733    }
734
735    #[inline]
736    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
737        self.any_inner(visitor, false)
738    }
739
740    #[inline]
741    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
742        self.any_inner(visitor, false)
743    }
744
745    #[inline]
746    fn deserialize_struct<V>(self, _: &'static str, _: &'static [&'static str], visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
747        self.any_inner(visitor, false)
748    }
749
750    #[inline]
751    fn deserialize_tuple_struct<V>(self, _: &'static str, _: usize, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
752        self.any_inner(visitor, false)
753    }
754
755    forward_to_deserialize_any! {
756        bytes byte_buf unit
757        map identifier str string char
758        ignored_any
759    }
760
761    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
762        let marker = self.take_or_read_marker()?;
763        any_num(&mut self.rd, visitor, marker)
764    }
765
766    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
767        let marker = self.take_or_read_marker()?;
768        any_num(&mut self.rd, visitor, marker)
769    }
770
771    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
772        let marker = self.take_or_read_marker()?;
773        any_num(&mut self.rd, visitor, marker)
774    }
775
776    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
777        let marker = self.take_or_read_marker()?;
778        any_num(&mut self.rd, visitor, marker)
779    }
780
781    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
782        let marker = self.take_or_read_marker()?;
783        any_num(&mut self.rd, visitor, marker)
784    }
785
786    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
787        let marker = self.take_or_read_marker()?;
788        any_num(&mut self.rd, visitor, marker)
789    }
790
791    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
792        let marker = self.take_or_read_marker()?;
793        any_num(&mut self.rd, visitor, marker)
794    }
795
796    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
797        let marker = self.take_or_read_marker()?;
798        any_num(&mut self.rd, visitor, marker)
799    }
800
801    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
802        let marker = self.take_or_read_marker()?;
803        any_num(&mut self.rd, visitor, marker)
804    }
805
806    fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
807        let marker = self.take_or_read_marker()?;
808        any_num(&mut self.rd, visitor, marker)
809    }
810
811    fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de> {
812        let marker = self.take_or_read_marker()?;
813        any_num(&mut self.rd, visitor, marker)
814    }
815}
816
817struct SeqAccess<'a, R, C> {
818    de: &'a mut Deserializer<R, C>,
819    left: u32,
820}
821
822impl<'a, R: 'a, C> SeqAccess<'a, R, C> {
823    #[inline]
824    fn new(de: &'a mut Deserializer<R, C>, len: u32) -> Self {
825        SeqAccess { de, left: len }
826    }
827}
828
829impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::SeqAccess<'de> for SeqAccess<'a, R, C> {
830    type Error = Error;
831
832    #[inline]
833    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
834        where T: DeserializeSeed<'de>
835    {
836        if self.left > 0 {
837            self.left -= 1;
838            Ok(Some(seed.deserialize(&mut *self.de)?))
839        } else {
840            Ok(None)
841        }
842    }
843
844    #[inline(always)]
845    fn size_hint(&self) -> Option<usize> {
846        self.left.try_into().ok()
847    }
848}
849
850struct MapAccess<'a, R, C> {
851    de: &'a mut Deserializer<R, C>,
852    left: u32,
853}
854
855impl<'a, R: 'a, C> MapAccess<'a, R, C> {
856    #[inline]
857    fn new(de: &'a mut Deserializer<R, C>, len: u32) -> Self {
858        MapAccess { de, left: len }
859    }
860}
861
862impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::MapAccess<'de> for MapAccess<'a, R, C> {
863    type Error = Error;
864
865    #[inline]
866    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
867        where K: DeserializeSeed<'de>
868    {
869        if self.left > 0 {
870            self.left -= 1;
871            seed.deserialize(&mut *self.de).map(Some)
872        } else {
873            Ok(None)
874        }
875    }
876
877    #[inline]
878    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
879        where V: DeserializeSeed<'de>
880    {
881        seed.deserialize(&mut *self.de)
882    }
883
884    #[inline(always)]
885    fn size_hint(&self) -> Option<usize> {
886        self.left.try_into().ok()
887    }
888}
889
890struct UnitVariantAccess<'a, R: 'a, C> {
891    de: &'a mut Deserializer<R, C>,
892}
893
894impl<'a, R: 'a, C> UnitVariantAccess<'a, R, C> {
895    pub fn new(de: &'a mut Deserializer<R, C>) -> Self {
896        UnitVariantAccess { de }
897    }
898}
899
900impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> de::EnumAccess<'de>
901    for UnitVariantAccess<'a, R, C>
902{
903    type Error = Error;
904    type Variant = Self;
905
906    #[inline]
907    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
908    where
909        V: de::DeserializeSeed<'de>,
910    {
911        let variant = seed.deserialize(&mut *self.de)?;
912        Ok((variant, self))
913    }
914}
915
916impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> de::VariantAccess<'de>
917    for UnitVariantAccess<'a, R, C>
918{
919    type Error = Error;
920
921    fn unit_variant(self) -> Result<(), Error> {
922        Ok(())
923    }
924
925    fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Error>
926    where
927        T: de::DeserializeSeed<'de>,
928    {
929        Err(de::Error::invalid_type(
930            Unexpected::UnitVariant,
931            &"newtype variant",
932        ))
933    }
934
935    fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Error>
936    where
937        V: de::Visitor<'de>,
938    {
939        Err(de::Error::invalid_type(
940            Unexpected::UnitVariant,
941            &"tuple variant",
942        ))
943    }
944
945    fn struct_variant<V>(
946        self,
947        _fields: &'static [&'static str],
948        _visitor: V,
949    ) -> Result<V::Value, Error>
950    where
951        V: de::Visitor<'de>,
952    {
953        Err(de::Error::invalid_type(
954            Unexpected::UnitVariant,
955            &"struct variant",
956        ))
957    }
958}
959
960struct VariantAccess<'a, R, C> {
961    de: &'a mut Deserializer<R, C>,
962}
963
964impl<'a, R: 'a, C> VariantAccess<'a, R, C> {
965    pub fn new(de: &'a mut Deserializer<R, C>) -> Self {
966        VariantAccess { de }
967    }
968}
969
970impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> de::EnumAccess<'de> for VariantAccess<'a, R, C> {
971    type Error = Error;
972    type Variant = Self;
973
974    #[inline]
975    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
976        where V: de::DeserializeSeed<'de>,
977    {
978        Ok((seed.deserialize(&mut *self.de)?, self))
979    }
980}
981
982impl<'de, 'a, R: ReadSlice<'de>, C: SerializerConfig> de::VariantAccess<'de> for VariantAccess<'a, R, C> {
983    type Error = Error;
984
985    #[inline]
986    fn unit_variant(self) -> Result<(), Error> {
987        decode::read_nil(&mut self.de.rd)?;
988        Ok(())
989    }
990
991    #[inline]
992    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
993        where T: DeserializeSeed<'de>
994    {
995        seed.deserialize(self.de)
996    }
997
998    #[inline]
999    fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
1000        where V: Visitor<'de>
1001    {
1002        de::Deserializer::deserialize_tuple(self.de, len, visitor)
1003    }
1004
1005    #[inline]
1006    fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value, Error>
1007        where V: Visitor<'de>
1008    {
1009        de::Deserializer::deserialize_tuple(self.de, fields.len(), visitor)
1010    }
1011}
1012
1013/// Unification of both borrowed and non-borrowed reference types.
1014#[derive(Clone, Copy, Debug, PartialEq)]
1015pub enum Reference<'b, 'c, T: ?Sized + 'static> {
1016    /// The reference is pointed at data that was borrowed.
1017    Borrowed(&'b T),
1018    /// The reference is pointed at data that was copied.
1019    Copied(&'c T),
1020}
1021
1022/// Extends the `Read` trait by allowing to read slices directly by borrowing bytes.
1023///
1024/// Used to allow zero-copy reading.
1025pub trait ReadSlice<'de>: Read {
1026    /// Reads the exact number of bytes from the underlying byte-array.
1027    fn read_slice<'a>(&'a mut self, len: usize) -> Result<Reference<'de, 'a, [u8]>, io::Error>;
1028}
1029
1030/// Owned reader wrapper.
1031#[derive(Debug)]
1032pub struct ReadReader<R: Read> {
1033    rd: R,
1034    buf: Vec<u8>,
1035}
1036
1037impl<R: Read> ReadReader<R> {
1038    #[inline]
1039    fn new(rd: R) -> Self {
1040        ReadReader {
1041            rd,
1042            buf: Vec::with_capacity(128),
1043        }
1044    }
1045}
1046
1047impl<'de, R: Read> ReadSlice<'de> for ReadReader<R> {
1048    #[inline]
1049    fn read_slice<'a>(&'a mut self, len: usize) -> Result<Reference<'de, 'a, [u8]>, io::Error> {
1050        self.buf.clear();
1051        let read = self.rd.by_ref().take(len as u64).read_to_end(&mut self.buf)?;
1052        if read != len {
1053            return Err(io::ErrorKind::UnexpectedEof.into());
1054        }
1055
1056        Ok(Reference::Copied(&self.buf[..]))
1057    }
1058}
1059
1060impl<R: Read> Read for ReadReader<R> {
1061    #[inline]
1062    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
1063        self.rd.read(buf)
1064    }
1065
1066    #[inline]
1067    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
1068        self.rd.read_exact(buf)
1069    }
1070}
1071
1072/// Borrowed reader wrapper.
1073#[derive(Debug)]
1074pub struct ReadRefReader<'a, R: ?Sized> {
1075    whole_slice: &'a R,
1076    buf: &'a [u8],
1077}
1078
1079impl<'a, T> ReadRefReader<'a, T> {
1080    /// Returns the part that hasn't been consumed yet
1081    #[must_use]
1082    pub fn remaining_slice(&self) -> &'a [u8] {
1083        self.buf
1084    }
1085}
1086
1087impl<'a, T: AsRef<[u8]> + ?Sized> ReadRefReader<'a, T> {
1088    #[inline]
1089    fn new(rd: &'a T) -> Self {
1090        Self {
1091            whole_slice: rd,
1092            buf: rd.as_ref(),
1093        }
1094    }
1095}
1096
1097impl<'a, T: AsRef<[u8]> + ?Sized> Read for ReadRefReader<'a, T> {
1098    #[inline]
1099    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
1100        self.buf.read(buf)
1101    }
1102
1103    #[inline]
1104    fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), io::Error> {
1105        self.buf.read_exact(buf)
1106    }
1107}
1108
1109impl<'de, T: AsRef<[u8]> + ?Sized> ReadSlice<'de> for ReadRefReader<'de, T> {
1110    #[inline]
1111    fn read_slice<'a>(&'a mut self, len: usize) -> Result<Reference<'de, 'a, [u8]>, io::Error> {
1112        if len > self.buf.len() {
1113            return Err(ErrorKind::UnexpectedEof.into());
1114        }
1115        let (a, b) = self.buf.split_at(len);
1116        self.buf = b;
1117        Ok(Reference::Borrowed(a))
1118    }
1119}
1120
1121#[test]
1122fn test_as_ref_reader() {
1123    let buf = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
1124    let mut rd = ReadRefReader::new(&buf);
1125
1126    assert_eq!(rd.read_slice(1).unwrap(), Reference::Borrowed(&[0][..]));
1127    assert_eq!(rd.read_slice(6).unwrap(), Reference::Borrowed(&[1, 2, 3, 4, 5, 6][..]));
1128    assert!(rd.read_slice(5).is_err());
1129    assert_eq!(rd.read_slice(4).unwrap(), Reference::Borrowed(&[7, 8, 9, 10][..]));
1130}
1131
1132/// Deserialize an instance of type `T` from an I/O stream of MessagePack.
1133///
1134/// # Errors
1135///
1136/// This conversion can fail if the structure of the Value does not match the structure expected
1137/// by `T`. It can also fail if the structure is correct but `T`'s implementation of `Deserialize`
1138/// decides that something is wrong with the data, for example required struct fields are missing.
1139#[inline]
1140pub fn from_read<R, T>(rd: R) -> Result<T, Error>
1141where R: Read,
1142      T: DeserializeOwned
1143{
1144    Deserialize::deserialize(&mut Deserializer::new(rd))
1145}
1146
1147/// Deserialize a temporary scope-bound instance of type `T` from a slice, with zero-copy if possible.
1148///
1149/// Deserialization will be performed in zero-copy manner whenever it is possible, borrowing the
1150/// data from the slice itself. For example, strings and byte-arrays won't copied.
1151///
1152/// # Errors
1153///
1154/// This conversion can fail if the structure of the Value does not match the structure expected
1155/// by `T`. It can also fail if the structure is correct but `T`'s implementation of `Deserialize`
1156/// decides that something is wrong with the data, for example required struct fields are missing.
1157///
1158/// # Examples
1159///
1160/// ```
1161/// use serde::Deserialize;
1162///
1163/// // Encoded `["Bobby", 8]`.
1164/// let buf = [0x92, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0x8];
1165///
1166/// #[derive(Debug, Deserialize, PartialEq)]
1167/// struct Dog<'a> {
1168///    name: &'a str,
1169///    age: u8,
1170/// }
1171///
1172/// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_slice(&buf).unwrap());
1173/// ```
1174#[inline(always)]
1175#[allow(deprecated)]
1176pub fn from_slice<'a, T>(input: &'a [u8]) -> Result<T, Error>
1177where
1178    T: Deserialize<'a>,
1179{
1180    from_read_ref(input)
1181}
1182
1183#[inline]
1184#[doc(hidden)]
1185#[deprecated(note = "use from_slice")]
1186pub fn from_read_ref<'a, R, T>(rd: &'a R) -> Result<T, Error>
1187where
1188    R: AsRef<[u8]> + ?Sized,
1189    T: Deserialize<'a>,
1190{
1191    let mut de = Deserializer::from_read_ref(rd);
1192    Deserialize::deserialize(&mut de)
1193}