Skip to main content

ByteSource

Trait ByteSource 

Source
pub trait ByteSource: 'static {
    // Required methods
    fn poll_next(
        &mut self,
        context: &mut Context<'_>,
    ) -> Poll<Option<Result<Vec<u8>>>>;
    fn close(&mut self);
    fn poll_closed(&mut self, context: &mut Context<'_>) -> Poll<Result<()>>;
}
Expand description

The producing side of one byte stream, supplied by its issuer.

A writer learns that the consumer stopped only at its next stream operation; the source cannot cancel an idle producer, which ends at its call’s own timeout.

§Issuer obligations

  • Termination. Whenever the issuer’s physical work ends, for any reason (return, error, panic, or the work being dropped), the source must resolve both sides: a waiting or later poll_next yields Err unless the producer had already ended cleanly, and poll_closed becomes Ready. The framework cannot do this itself: it has no link from the physical work to the source. The issuer needs a drop guard on its physical side for this.
  • Failure is never a clean end. A failed, trapped or timed-out producer yields Err, never None. The Err item is terminal; reads after it yield None.
  • A clean end is only the body’s end. It does not mean the producer’s call succeeded; a later failure reaches Host::errors and the result of ByteStream::close. A close the consumer asked for should settle as Ok unless the producer itself failed, since every recorded failure also fails Host::close.
  • No re-entry. Every method runs while the stream end is borrowed. A source must not call back into the host synchronously (for example through actions that end or cancel a root); it may only store wakers and buffer state, and wake them.

Required Methods§

Source

fn poll_next( &mut self, context: &mut Context<'_>, ) -> Poll<Option<Result<Vec<u8>>>>

The next chunk, None once the producer ended cleanly, or the producer’s failure. Returns Pending with the waker stored when no chunk is buffered.

Source

fn close(&mut self)

The consumer will read no more. Called at most once.

Source

fn poll_closed(&mut self, context: &mut Context<'_>) -> Poll<Result<()>>

Ready once the producer has physically stopped after ByteSource::close or its own end, with its final outcome (an error or a timeout of the producer’s call, for example).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§