pub trait AsyncStreamVisitor: Send + 'static {
type Output: Send + 'static;
// Required methods
fn on_chunk(
&mut self,
chunk: Chunk,
) -> impl Future<Output = Next> + Send + '_;
fn into_output(self) -> Self::Output;
// Provided methods
fn on_gap(&mut self) { ... }
fn on_eof(&mut self) -> impl Future<Output = ()> + Send + '_ { ... }
}Expand description
An asynchronous visitor that observes stream events and produces a final value.
AsyncStreamVisitor is the asynchronous counterpart to StreamVisitor. Use it when
observing a chunk needs to .await (network I/O, async writers, channel sends).
The trait uses return-position impl Future rather than async fn to keep the Send bound
on the returned future expressible on stable Rust; this is the same shape used by
AsyncChunkCollector and
AsyncLineCollector. See StreamVisitor for the lifecycle
description; the only difference is that on_chunk and on_eof are async.
§Example
/// Forwards every chunk to an mpsc channel.
struct ForwardChunks { tx: tokio::sync::mpsc::Sender<Vec<u8>> }
impl AsyncStreamVisitor for ForwardChunks {
type Output = ();
async fn on_chunk(&mut self, chunk: Chunk) -> Next {
match self.tx.send(chunk.as_ref().to_vec()).await {
Ok(()) => Next::Continue,
Err(_) => Next::Break,
}
}
fn into_output(self) {}
}Required Associated Types§
Sourcetype Output: Send + 'static
type Output: Send + 'static
The value produced by into_output after the visitor
has finished observing the stream. Returned via Consumer::wait
and Consumer::cancel.
Required Methods§
Sourcefn on_chunk(&mut self, chunk: Chunk) -> impl Future<Output = Next> + Send + '_
fn on_chunk(&mut self, chunk: Chunk) -> impl Future<Output = Next> + Send + '_
Asynchronously observes a single chunk.
Return Next::Continue to keep visiting, or Next::Break to stop without consuming
further events; in the latter case on_eof is not called.
Sourcefn into_output(self) -> Self::Output
fn into_output(self) -> Self::Output
Consumes the visitor and returns its final output.
Called after the visitor has stopped observing events. Synchronous because no further stream interaction is required at this point.
Provided Methods§
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§
Source§impl<S: AsyncLineSink> AsyncStreamVisitor for LineAdapter<S>
Async impl of the same LineAdapter struct.
impl<S: AsyncLineSink> AsyncStreamVisitor for LineAdapter<S>
Async impl of the same LineAdapter struct.
Each per-line iteration calls next_line synchronously, materializes the line as a fresh
String via Cow::into_owned, drops the parser borrow, then awaits the inner sink. The
allocation per line is the price of supporting async per-line callbacks on stable Rust —
holding a parser borrow across an .await is forbidden because the next iteration
re-borrows the parser.