pub trait TryNextWithContext {
type Item;
type Error;
type Context;
// Required method
fn try_next_with_context(
&mut self,
context: &mut Self::Context,
) -> Result<Option<Self::Item>, Self::Error>;
// Provided method
fn try_collect_with_context(
&mut self,
context: &mut Self::Context,
) -> Result<Vec<Self::Item>, Self::Error> { ... }
}
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 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.
See the module-level documentation for details and examples.
Required Associated Types§
Required Methods§
Sourcefn try_next_with_context(
&mut self,
context: &mut Self::Context,
) -> Result<Option<Self::Item>, Self::Error>
fn try_next_with_context( &mut self, context: &mut Self::Context, ) -> 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 Self::Context,
) -> Result<Vec<Self::Item>, Self::Error>
fn try_collect_with_context( &mut self, context: &mut Self::Context, ) -> 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.