Skip to main content

Consumable

Trait Consumable 

Source
pub trait Consumable: Subscribable {
    type Error: Error + Send + Sync + 'static + From<Self::SubscribeError>;

    // Provided methods
    fn consume<V>(&self, visitor: V) -> Result<Consumer<V::Output>, Self::Error>
       where V: StreamVisitor,
             Self: OutputStream { ... }
    fn consume_async<V>(
        &self,
        visitor: V,
    ) -> Result<Consumer<V::Output>, Self::Error>
       where V: AsyncStreamVisitor,
             Self: OutputStream { ... }
}
Expand description

Enables a stream to be consumed by StreamVisitors and AsyncStreamVisitors.

Construct a bundled visitor under visitors (or your own StreamVisitor / AsyncStreamVisitor implementation) and pass it to consume or consume_async. The returned Consumer owns the spawned tokio task that drives the visitor over this stream.

Implementors only need to specify Error. The consume and consume_async methods have default impls that subscribe via Subscribable::try_subscribe and spawn the consumer task; those defaults additionally require Self: OutputStream because they label the consumer task with OutputStream::name.

Required Associated Types§

Source

type Error: Error + Send + Sync + 'static + From<Self::SubscribeError>

Error returned when consumer creation fails. Must be constructible from the underlying Subscribable::SubscribeError so the default consume / consume_async impls can propagate subscription failures.

Provided Methods§

Source

fn consume<V>(&self, visitor: V) -> Result<Consumer<V::Output>, Self::Error>
where V: StreamVisitor, Self: OutputStream,

Tries to drive the provided synchronous StreamVisitor over this stream and returns a Consumer that owns the spawned task.

The returned Consumer’s wait yields whatever the visitor produces through StreamVisitor::into_output, allowing visitor implementors to give and get back ownership of some value.

§Errors

Returns Self::Error if the backend rejects the consumer creation (for example, because a single-subscriber backend already has an active consumer).

Source

fn consume_async<V>( &self, visitor: V, ) -> Result<Consumer<V::Output>, Self::Error>

Tries to drive the provided asynchronous AsyncStreamVisitor over this stream and returns a Consumer that owns the spawned task.

Use this when observing a chunk requires .await (for example, forwarding chunks to an async writer or channel). See consume for the synchronous variant.

§Errors

Returns Self::Error if the backend rejects the consumer creation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§