TryNextWithContext

Trait TryNextWithContext 

Source
pub trait TryNextWithContext<C, S = ()>
where S: Default + Copy,
{ 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 of try_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§

Source

type Item

The type of items yielded by this source.

Source

type Error

The error type that may be returned when producing the next item fails.

Required Methods§

Source

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§

Source

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.

Source

fn stats(&self) -> S

Implementations on Foreign Types§

Source§

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 implementing Read.
  • 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.

Source§

type Item = u8

The type of item yielded — a single u8 byte.

Source§

type Error = Error

The error type produced by this iterator — io::Error.

Source§

fn try_next_with_context( &mut self, _context: &mut C, ) -> Result<Option<Self::Item>, Self::Error>

Attempts to read the next byte from the buffer using an optional context.

§Returns
  • Ok(Some(byte)) if a byte was read successfully.
  • Ok(None) if EOF was reached.
  • Err(e) if an I/O error occurred.

Implementors§