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
60
61
62
63
64
65
66
67
use crate::{Allocator, Context};
use super::{Decode, Decoder, SizeHint};
/// Trait governing how to decode a sequence.
pub trait SequenceDecoder<'de> {
/// Context associated with the decoder.
type Cx: Context<Error = Self::Error, Allocator = Self::Allocator>;
/// Error associated with decoding.
type Error;
/// The allocator associated with the decoder.
type Allocator: Allocator;
/// The mode of the decoder.
type Mode: 'static;
/// The decoder for individual items.
type DecodeNext<'this>: Decoder<
'de,
Cx = Self::Cx,
Error = Self::Error,
Allocator = Self::Allocator,
Mode = Self::Mode,
>
where
Self: 'this;
/// Access the context associated with the decoder.
fn cx(&self) -> Self::Cx;
/// 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::Error>;
/// Try to decode the next element.
#[must_use = "Decoders must be consumed"]
fn try_decode_next(&mut self) -> Result<Option<Self::DecodeNext<'_>>, Self::Error>;
/// Decode the next element of the given type, erroring in case it's absent.
#[inline]
fn next<T>(&mut self) -> Result<T, Self::Error>
where
T: Decode<'de, Self::Mode, Self::Allocator>,
{
self.decode_next()?.decode()
}
/// Decode the next element of the given type.
#[inline]
fn try_next<T>(&mut self) -> Result<Option<T>, Self::Error>
where
T: Decode<'de, Self::Mode, Self::Allocator>,
{
let Some(decoder) = self.try_decode_next()? else {
return Ok(None);
};
Ok(Some(decoder.decode()?))
}
}