musli_core/de/
sequence_decoder.rs

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::Context;

use super::{Decode, Decoder, SizeHint};

/// Trait governing how to decode a sequence.
pub trait SequenceDecoder<'de> {
    /// Context associated with the decoder.
    type Cx: ?Sized + Context;
    /// The decoder for individual items.
    type DecodeNext<'this>: Decoder<
        'de,
        Cx = Self::Cx,
        Error = <Self::Cx as Context>::Error,
        Mode = <Self::Cx as Context>::Mode,
    >
    where
        Self: 'this;

    /// Get a size hint of known remaining elements.
    #[inline]
    fn size_hint(&self) -> SizeHint {
        SizeHint::any()
    }

    /// Return decoder to decode the next element.
    ///
    /// This will error or provide garbled data in case the next element is not
    /// available.
    #[must_use = "Decoders must be consumed"]
    fn decode_next(&mut self) -> Result<Self::DecodeNext<'_>, <Self::Cx as Context>::Error>;

    /// Try to decode the next element.
    #[must_use = "Decoders must be consumed"]
    fn try_decode_next(
        &mut self,
    ) -> Result<Option<Self::DecodeNext<'_>>, <Self::Cx as Context>::Error>;

    /// Decode the next element of the given type, erroring in case it's absent.
    #[inline]
    fn next<T>(&mut self) -> Result<T, <Self::Cx as Context>::Error>
    where
        T: Decode<'de, <Self::Cx as Context>::Mode>,
    {
        self.decode_next()?.decode()
    }

    /// Decode the next element of the given type.
    #[inline]
    fn try_next<T>(&mut self) -> Result<Option<T>, <Self::Cx as Context>::Error>
    where
        T: Decode<'de, <Self::Cx as Context>::Mode>,
    {
        let Some(decoder) = self.try_decode_next()? else {
            return Ok(None);
        };

        Ok(Some(decoder.decode()?))
    }
}