pub trait TryNextWithContext<C, S = ()>{
type Item;
type Error;
// Required method
fn try_next_with_context(
&mut self,
context: &mut C,
) -> Result<Option<Self::Item>, Self::Error>;
// Provided methods
fn try_collect_with_context(
&mut self,
context: &mut C,
) -> Result<Vec<Self::Item>, Self::Error> { ... }
fn stats(&self) -> S { ... }
}Expand description
Context-aware, fallible producer.
A trait for types that can produce items one at a time with the help of an external context, where fetching the next item may fail.
This trait is synchronous — each call to try_next
blocks until an item is produced or an error occurs.
The context type C allows the caller to provide additional
state or resources used during iteration. It can hold shared
mutable state, configuration data, or external resources such as file
handles, buffers, or network clients. Each call to try_next
receives a mutable reference to this context.
§Type Parameters
C- The type of the external context passed to each call oftry_next_with_context. It represents the environment or state that the producer can use or mutate while producing items. For example, this might be a reader, a buffer pool, or a user-defined structure containing shared resources.S- Optional stats type.
Required Associated Types§
Required Methods§
Sourcefn try_next_with_context(
&mut self,
context: &mut C,
) -> Result<Option<Self::Item>, Self::Error>
fn try_next_with_context( &mut self, context: &mut C, ) -> Result<Option<Self::Item>, Self::Error>
Attempts to produce the next item, using the provided mutable context.
Returns:
Ok(Some(item))— a new item was produced,Ok(None)— the source is exhausted,Err(e)— iteration failed with an error.
Provided Methods§
Sourcefn try_collect_with_context(
&mut self,
context: &mut C,
) -> Result<Vec<Self::Item>, Self::Error>
fn try_collect_with_context( &mut self, context: &mut C, ) -> Result<Vec<Self::Item>, Self::Error>
Collects all remaining items into a Vec, using the given context.
The method repeatedly calls try_next_with_context
until None or an error is returned, collecting all successful items into a vector.
If any call returns Err(e), iteration stops immediately and that error is returned.
§Feature
This method is only available when the alloc feature is enabled.
fn stats(&self) -> S
Implementations on Foreign Types§
Source§impl<R, C> TryNextWithContext<C> for BufReader<R>where
R: Read,
Implements TryNextWithContext for BufReader.
impl<R, C> TryNextWithContext<C> for BufReader<R>where
R: Read,
Implements TryNextWithContext for BufReader.
This implementation enables reading the next byte from a buffered
reader while optionally using a mutable external context C.
The context parameter allows stateful processing during iteration, even though this implementation does not directly use it.
§Type Parameters
R— The underlying reader type implementingRead.C— The context type passed into [try_next_with_context].
§Errors
Returns any I/O error encountered during reading. Returns Ok(None)
when the reader reaches the end of the input.