pub struct Step<'a> {
pub consumed: usize,
pub event: Option<Event<'a>>,
}Expand description
One decode step: how much of input was consumed, and the event it
produced, if any. See the step contract.
consumed and event are independent. All four shapes occur:
consumed | event | meaning |
|---|---|---|
0 | None | stop. More input is needed - or the message is over and the decoder is done; the step does not say which. |
> 0 | None | keep going. A chunk-size line, a chunk CRLF or an empty trailer block: progress with nothing for the host. |
> 0 | Some | an event. |
0 | Some | an event that reads no input: Event::End and Event::Upgrade both arrive this way. |
So the loop condition is consumed > 0 || event.is_some(), and a host that
stops on either zero alone is wrong in one of the two directions:
let mut ended = false;
loop {
let step = decoder.receive(&input)?;
let consumed = step.consumed;
let progressed = consumed > 0 || step.event.is_some();
// Body borrows `input`, so handle the event before draining.
ended |= matches!(step.event, Some(Event::End));
input.drain(..consumed);
if !progressed {
break; // read more bytes from the transport, then continue
}
}
// `End` came from a step after the last byte was consumed.
assert!(ended && input.is_empty() && decoder.reusable());The zero-consumption event is the trap. The step that consumes the last
byte of a message does not carry End; the next call does, and it reads
nothing. A host that loops while it has input - or stops when a step
consumed nothing - never makes that call, and the finished message sits in
the decoder until the peer’s idle timeout closes the connection.
Decoder::wants_step names the pending call for a host that keys its
loop on input rather than on steps.
Fields§
§consumed: usize§event: Option<Event<'a>>