1use crate::Context;
23use super::{Decode, Decoder, SizeHint};
45/// Trait governing how to decode a sequence.
6pub trait SequenceDecoder<'de> {
7/// Context associated with the decoder.
8type Cx: ?Sized + Context;
9/// The decoder for individual items.
10type DecodeNext<'this>: Decoder<
11'de,
12 Cx = Self::Cx,
13 Error = <Self::Cx as Context>::Error,
14 Mode = <Self::Cx as Context>::Mode,
15 >
16where
17Self: 'this;
1819/// Get a size hint of known remaining elements.
20#[inline]
21fn size_hint(&self) -> SizeHint {
22 SizeHint::any()
23 }
2425/// Return decoder to decode the next element.
26 ///
27 /// This will error or provide garbled data in case the next element is not
28 /// available.
29#[must_use = "Decoders must be consumed"]
30fn decode_next(&mut self) -> Result<Self::DecodeNext<'_>, <Self::Cx as Context>::Error>;
3132/// Try to decode the next element.
33#[must_use = "Decoders must be consumed"]
34fn try_decode_next(
35&mut self,
36 ) -> Result<Option<Self::DecodeNext<'_>>, <Self::Cx as Context>::Error>;
3738/// Decode the next element of the given type, erroring in case it's absent.
39#[inline]
40fn next<T>(&mut self) -> Result<T, <Self::Cx as Context>::Error>
41where
42T: Decode<'de, <Self::Cx as Context>::Mode>,
43 {
44self.decode_next()?.decode()
45 }
4647/// Decode the next element of the given type.
48#[inline]
49fn try_next<T>(&mut self) -> Result<Option<T>, <Self::Cx as Context>::Error>
50where
51T: Decode<'de, <Self::Cx as Context>::Mode>,
52 {
53let Some(decoder) = self.try_decode_next()? else {
54return Ok(None);
55 };
5657Ok(Some(decoder.decode()?))
58 }
59}