pub trait StreamVisitor: Send + 'static {
type Output: Send + 'static;
// Required methods
fn on_chunk(&mut self, chunk: Chunk) -> Next;
fn into_output(self) -> Self::Output;
// Provided methods
fn on_gap(&mut self) { ... }
fn on_eof(&mut self) { ... }
}Expand description
A synchronous visitor that observes stream events and produces a final value.
StreamVisitor is the synchronous counterpart to AsyncStreamVisitor. Implement it on a
type that needs to react to chunks, gaps, and EOF without .await-ing between events, then
drive it via consume_with to obtain a Consumer handle that owns the
resulting tokio task.
All built-in consumer factories (inspect_*, collect_*, wait_for_line) construct a
built-in visitor and call consume_with internally; this trait is what users implement to
plug in custom logic without wrapping a closure in shared mutable state.
§Lifecycle
on_chunkis invoked for every observed chunk. ReturnNext::Continueto keep going orNext::Breakto stop early.on_gapis invoked when the stream backend reports that chunks were dropped (e.g., best-effort delivery overflow). Use it to reset partial-line buffers or other accumulated state.on_eofis invoked exactly once when the stream ends naturally. It is not invoked when the visitor returnedNext::Break, nor when the consumer task is cancelled or aborted.into_outputconsumesselfand returns the value theConsumer’swait/cancelmethods yield.
§Example
/// Counts chunks and stops after `limit`.
struct CountUntil { count: usize, limit: usize }
impl StreamVisitor for CountUntil {
type Output = usize;
fn on_chunk(&mut self, _chunk: Chunk) -> Next {
self.count += 1;
if self.count >= self.limit { Next::Break } else { Next::Continue }
}
fn into_output(self) -> usize { self.count }
}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) -> Next
fn on_chunk(&mut self, chunk: Chunk) -> Next
Invoked for every chunk observed on the stream.
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 (via EOF, Break, or cancellation).
The returned value is what the owning Consumer’s wait/cancel
methods yield.
Provided Methods§
Sourcefn on_gap(&mut self)
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.
The default implementation does nothing. Override it to reset any partial-line buffers or other accumulated state that would be invalidated by the gap.
Whether gaps can occur depends on the guarantees chosen for the backend.
Sourcefn on_eof(&mut self)
fn on_eof(&mut self)
Invoked exactly once when the stream ends (EOF or write side dropped).
Not called when the visitor returned Next::Break from
on_chunk, nor when the consumer task is cancelled or aborted
before the stream ends.