Skip to main content

Step

Struct Step 

Source
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:

consumedeventmeaning
0Nonestop. More input is needed - or the message is over and the decoder is done; the step does not say which.
> 0Nonekeep going. A chunk-size line, a chunk CRLF or an empty trailer block: progress with nothing for the host.
> 0Somean event.
0Somean 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>>

Trait Implementations§

Source§

impl<'a> Debug for Step<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Step<'a>

§

impl<'a> Freeze for Step<'a>

§

impl<'a> RefUnwindSafe for Step<'a>

§

impl<'a> Send for Step<'a>

§

impl<'a> Sync for Step<'a>

§

impl<'a> Unpin for Step<'a>

§

impl<'a> UnsafeUnpin for Step<'a>

§

impl<'a> UnwindSafe for Step<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.