Skip to main content

AsyncIterator

Trait AsyncIterator 

Source
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§

Source

type Item

The type of the elements being iterated over.

Required Methods§

Source

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§

Source

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
    }
}
Source

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).

Source

fn async_collect<B>(self) -> impl Future<Output = B>
where Self: Sized, B: FromIterator<Self::Item>,

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>
where I: AsyncIterator, F: FnMut(I::Item) -> Fut + Send, Fut: Future<Output = B> + Send,

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.

Source§

type Item = B

Source§

impl<I, F, Fut> AsyncIterator for AsyncFilter<I, F>
where I: AsyncIterator, F: FnMut(I::Item) -> Fut, Fut: Future<Output = bool>, I::Item: Clone,

Source§

impl<T> AsyncIterator for T
where T: Iterator + ?Sized,

Blanket implementation of AsyncIterator for all synchronous Iterator types.

This enables any standard iterator to be used as an async iterator by immediately returning the next item.

Source§

type Item = <T as Iterator>::Item