Skip to main content

CollectChunksAsync

Struct CollectChunksAsync 

Source
pub struct CollectChunksAsync<T, F>
where T: Sink, F: for<'a> FnMut(Chunk, &'a mut T) -> Pin<Box<dyn Future<Output = Next> + Send + 'a>> + Send + 'static,
{ /* private fields */ }
Expand description

Asynchronous counterpart to CollectChunks. The per-chunk hook is a closure that returns a boxed future, allowing the future to borrow chunk and sink across .await while remaining Send for tokio::spawn. Construct via fold and pass to Consumable::consume_async.

The boxed future incurs one allocation per observed chunk; for hot paths that cannot afford that, implement AsyncStreamVisitor directly on a struct that owns the sink.

§Example

let visitor = CollectChunksAsync::fold(Vec::<u8>::new(), |chunk, sink| {
    Box::pin(async move {
        sink.extend_from_slice(chunk.as_ref());
        Next::Continue
    })
});

Implementations§

Source§

impl<T, F> CollectChunksAsync<T, F>
where T: Sink, F: for<'a> FnMut(Chunk, &'a mut T) -> Pin<Box<dyn Future<Output = Next> + Send + 'a>> + Send + 'static,

Source

pub fn fold(sink: T, f: F) -> Self

Fold-style constructor: sink is the initial accumulator and f folds each observed chunk into it asynchronously. The closure must wrap its async body in Box::pin(async move { ... }) so the returned future can borrow chunk and sink while remaining Send.

Trait Implementations§

Source§

impl<T, F> AsyncStreamVisitor for CollectChunksAsync<T, F>
where T: Sink, F: for<'a> FnMut(Chunk, &'a mut T) -> Pin<Box<dyn Future<Output = Next> + Send + 'a>> + Send + 'static,

Source§

type Output = T

The value produced by into_output after the visitor has finished observing the stream. Returned via Consumer::wait and Consumer::cancel.
Source§

fn on_chunk(&mut self, chunk: Chunk) -> impl Future<Output = Next> + Send + '_

Asynchronously observes a single chunk. Read more
Source§

fn into_output(self) -> Self::Output

Consumes the visitor and returns its final output. Read more
Source§

fn on_gap(&mut self)

Invoked when the stream backend reports that one or more chunks were dropped between the last delivered chunk and the next one. Read more
Source§

fn on_eof(&mut self) -> impl Future<Output = ()> + Send + '_

Asynchronously observes end-of-stream. Read more

Auto Trait Implementations§

§

impl<T, F> Freeze for CollectChunksAsync<T, F>
where T: Freeze, F: Freeze,

§

impl<T, F> RefUnwindSafe for CollectChunksAsync<T, F>

§

impl<T, F> Send for CollectChunksAsync<T, F>

§

impl<T, F> Sync for CollectChunksAsync<T, F>
where T: Sync, F: Sync,

§

impl<T, F> Unpin for CollectChunksAsync<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnsafeUnpin for CollectChunksAsync<T, F>
where T: UnsafeUnpin, F: UnsafeUnpin,

§

impl<T, F> UnwindSafe for CollectChunksAsync<T, F>
where T: UnwindSafe, F: UnwindSafe,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Sink for T
where T: Send + 'static,