pub trait TryNext<S = ()>{
type Item;
type Error;
// Required method
fn try_next(&mut self) -> Result<Option<Self::Item>, Self::Error>;
// Provided methods
fn try_collect(&mut self) -> Result<Vec<Self::Item>, Self::Error> { ... }
fn stats(&self) -> S { ... }
}Expand description
Context-free, fallible producer.
A trait for types that can produce items one at a time, 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. See the
module-level documentation for details and examples.
S- Optional stats type.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn try_collect(&mut self) -> Result<Vec<Self::Item>, Self::Error>
fn try_collect(&mut self) -> Result<Vec<Self::Item>, Self::Error>
Collects all remaining items into a Vec.
The method repeatedly calls try_next 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> TryNext for BufReader<R>where
R: Read,
impl<R> TryNext for BufReader<R>where
R: Read,
This version does not use a context, and simply reads the next byte sequentially from the buffer.
§Example
use std::io::BufReader;
use try_next::TryNext;
let data = b"hi";
let mut reader = BufReader::new(&data[..]);
assert_eq!(reader.try_next().unwrap(), Some(b'h'));
assert_eq!(reader.try_next().unwrap(), Some(b'i'));
assert_eq!(reader.try_next().unwrap(), None);