pub struct Decoder<P, S> { /* private fields */ }Expand description
Owned-buffer streaming decoder, parameterised on the Partial type.
Accepts bytes via feed and yields decoded values via
next. A None from next means “need more bytes,
call feed again” - malformed packets are silently skipped (the
framed bytes are drained to guarantee forward progress).
§Example
// fresh mode (no in-flight partial): seek sentinel + decode framed body
let mut dec = Decoder::<<MyPacket as DecodePartial<&[u8]>>::Partial>::new();
let mut scratch = [0u8; 2048];
loop {
let n = socket.recv(&mut scratch)?;
dec.feed(&scratch[..n]);
while let Some(pkt) = dec.next() {
handle(pkt);
}
}
// resume mode: handed back from `Packet::NeedMore`
match MyPacket::decode_partial(&mut input) {
Ok(Packet::Ready(t)) => use_packet(t),
Ok(Packet::NeedMore(partial)) => {
let mut dec = Decoder::new();
dec.feed(more_bytes);
while let Some(pkt) = dec.next() {
use_packet(pkt);
break;
}
}
Err(label) => report(label),
}Implementations§
Source§impl<P, S> Decoder<P, S>
Decoder implementation
impl<P, S> Decoder<P, S>
Decoder implementation
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Construct an empty decoder with a pre-allocated buffer capacity, in fresh mode.
Sourcepub fn buffered(&self) -> &[u8] ⓘ
pub fn buffered(&self) -> &[u8] ⓘ
The bytes currently buffered but not yet decoded. Useful for observability and diagnostics.
Sourcepub fn partial(&self) -> Option<&P>
pub fn partial(&self) -> Option<&P>
Borrow the in-flight partial, if any. Some only after the decoder
was constructed from a Packet::NeedMore return and before the
next Self::next completes the packet.
Sourcepub fn into_partial(self) -> Option<P>
pub fn into_partial(self) -> Option<P>
Consume the decoder and return its in-flight partial, if any.
Used by the derive-generated decode_partial wrapper to lift
“input fully consumed, NeedMore returned” into a finalisation
at the fresh-mode boundary.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Drop all buffered bytes and any in-flight partial. Call after a
Malformed error if you want to resync cleanly rather than
byte-walk through corrupt input.
Sourcepub fn feed(&mut self, bytes: &[u8])
pub fn feed(&mut self, bytes: &[u8])
Append bytes to the internal buffer. Does not trigger decoding on its
own - call next to try to consume complete values.
Sourcepub fn iter(&mut self) -> DecoderIter<'_, P, S> ⓘ
pub fn iter(&mut self) -> DecoderIter<'_, P, S> ⓘ
Returns an iterator over the buffered values, consuming them as they are decoded
Source§impl<P, T> Decoder<P, &[u8]>where
P: Partial<Final = T> + Default,
for<'a> T: DecodePartial<&'a [u8], Partial = P> + ResumePartial<&'a [u8]> + SeekSentinel<&'a [u8]>,
Decoder decoding implementation - resume mode
impl<P, T> Decoder<P, &[u8]>where
P: Partial<Final = T> + Default,
for<'a> T: DecodePartial<&'a [u8], Partial = P> + ResumePartial<&'a [u8]> + SeekSentinel<&'a [u8]>,
Decoder decoding implementation - resume mode
This is just the &u8 specialization of Decoder
When the decoder carries an in-flight partial, next
resumes DecodePartial::decode_partial without re-seeking the
sentinel: the partial state already lives past the framing.
Sourcepub fn finish(self) -> Result<T, DecodeIterError>
pub fn finish(self) -> Result<T, DecodeIterError>
Force-finalise the current partial. Use when the upstream signals “no more bytes coming” and you want the accumulated partial surfaced (or its required-field failure reported). The decoder is consumed; the buffered bytes are discarded.
Trait Implementations§
Source§impl<'d, P, T, S> IntoIterator for &'d mut Decoder<P, S>where
Decoder<P, S>: PartialIterator<P>,
S: Stream,
P: Partial<Final = T> + Default,
for<'b> T: DecodePartial<S, Partial = P> + ResumePartial<S> + SeekSentinel<S>,
Decoder implementation of IntoIterator
impl<'d, P, T, S> IntoIterator for &'d mut Decoder<P, S>where
Decoder<P, S>: PartialIterator<P>,
S: Stream,
P: Partial<Final = T> + Default,
for<'b> T: DecodePartial<S, Partial = P> + ResumePartial<S> + SeekSentinel<S>,
Decoder implementation of IntoIterator
Source§impl<'a, P, T> PartialIterator<P> for &mut Decoder<P, &'a [u8]>
&mut Decoder implementation of PartialIterator for [&[u8]]
impl<'a, P, T> PartialIterator<P> for &mut Decoder<P, &'a [u8]>
&mut Decoder implementation of PartialIterator for [&[u8]]
Source§fn next_resume(&mut self) -> Result<<P as Partial>::Final, DecodeIterError>
fn next_resume(&mut self) -> Result<<P as Partial>::Final, DecodeIterError>
Source§fn next_fresh(&mut self) -> Result<<P as Partial>::Final, DecodeIterError>
fn next_fresh(&mut self) -> Result<<P as Partial>::Final, DecodeIterError>
Source§impl<P, T> PartialIterator<P> for Decoder<P, &[u8]>where
P: Partial<Final = T> + Default,
for<'a> T: DecodePartial<&'a [u8], Partial = P> + ResumePartial<&'a [u8]> + SeekSentinel<&'a [u8]>,
Decoder implementation of PartialIterator for [&[u8]]
impl<P, T> PartialIterator<P> for Decoder<P, &[u8]>where
P: Partial<Final = T> + Default,
for<'a> T: DecodePartial<&'a [u8], Partial = P> + ResumePartial<&'a [u8]> + SeekSentinel<&'a [u8]>,
Decoder implementation of PartialIterator for [&[u8]]
Source§fn next_resume(&mut self) -> Result<T, DecodeIterError>
fn next_resume(&mut self) -> Result<T, DecodeIterError>
Resume-mode entry
Source§fn next_fresh(&mut self) -> Result<T, DecodeIterError>
fn next_fresh(&mut self) -> Result<T, DecodeIterError>
Fresh-mode entry