pub trait AsyncIterator {
type Item;
// Required method
fn next_async(&mut self) -> impl Future<Output = Option<Self::Item>>;
// Provided methods
fn sync_iter(
&mut self,
) -> impl Future<Output = SyncIter<IntoIter<Self::Item>>>
where Self: Sized { ... }
fn async_size_hint(&self) -> (usize, Option<usize>) { ... }
fn async_collect<B>(self) -> impl Future<Output = B>
where Self: Sized,
B: FromIterator<Self::Item> { ... }
}Expand description
Trait for asynchronous iteration.
This trait is similar to the standard Iterator trait, but designed to work in asynchronous
contexts where next() returns a Future. It provides core methods for driving async iteration,
collecting results, and converting to a synchronous iterator.
Implementors must define next_async, which yields the next item wrapped in a Future.
Required Associated Types§
Required Methods§
Sourcefn next_async(&mut self) -> impl Future<Output = Option<Self::Item>>
fn next_async(&mut self) -> impl Future<Output = Option<Self::Item>>
Asynchronously returns the next item in the iterator.
Returns None when iteration is finished.
§Examples
use async_iter_ext::AsyncIterator;
async fn iterate<I: AsyncIterator>(mut iter: I) {
while let Some(item) = iter.next_async().await {
// process item
}
}Provided Methods§
Sourcefn sync_iter(&mut self) -> impl Future<Output = SyncIter<IntoIter<Self::Item>>>where
Self: Sized,
fn sync_iter(&mut self) -> impl Future<Output = SyncIter<IntoIter<Self::Item>>>where
Self: Sized,
Converts this async iterator into a synchronous SyncIter using async collection.
This method collects all remaining items into a Vec and returns a SyncIter over them.
§Notes
- This is useful when you want to move from an async context to sync processing.
§Examples
use async_iter_ext::AsyncIterator;
async fn convert<I: AsyncIterator>(iter: &mut I) {
let sync = iter.sync_iter().await;
for item in sync {
// use item
}
}Sourcefn async_size_hint(&self) -> (usize, Option<usize>)
fn async_size_hint(&self) -> (usize, Option<usize>)
Provides a hint about the size of the remaining items.
Returns a tuple where the first element is a lower bound and the second is an optional upper bound.
This is used to optimize certain operations such as preallocating capacity.
Default implementation returns (0, None).
Sourcefn async_collect<B>(self) -> impl Future<Output = B>
fn async_collect<B>(self) -> impl Future<Output = B>
Collects all items of the async iterator into a container type.
Works like the standard Iterator::collect, but in an async context.
The target container must implement FromIterator<Self::Item>.
§Examples
use async_iter_ext::AsyncIterator;
async fn collect_items<I: AsyncIterator>(iter: I) -> Vec<I::Item> {
iter.async_collect::<Vec<_>>().await
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
Source§impl<B, I, F, Fut> AsyncIterator for AsyncMap<I, F>
Implements the AsyncIterator trait for AsyncMap, yielding the result of applying
the async mapping function f to each item from the underlying iterator iter.
impl<B, I, F, Fut> AsyncIterator for AsyncMap<I, F>
Implements the AsyncIterator trait for AsyncMap, yielding the result of applying
the async mapping function f to each item from the underlying iterator iter.
The next_async() method awaits the next item and then applies the mapping function,
awaiting its result before yielding it downstream.