pub trait AsyncIterTools: AsyncIterator {
// Provided methods
fn for_each_async<F, Fut>(self, f: F) -> impl Future<Output = ()>
where Self: Sized,
F: Fn(Self::Item) -> Fut,
Fut: Future<Output = ()> { ... }
fn map_async<B, F, Fut>(self, f: F) -> AsyncMap<Self, F> ⓘ
where Self: Sized,
F: Fn(Self::Item) -> Fut,
Fut: Future<Output = B> { ... }
fn filter_async<F, Fut>(self, f: F) -> AsyncFilter<Self, F> ⓘ
where Self: Sized,
F: Fn(Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self::Item: Clone { ... }
}Provided Methods§
Sourcefn for_each_async<F, Fut>(self, f: F) -> impl Future<Output = ()>
fn for_each_async<F, Fut>(self, f: F) -> impl Future<Output = ()>
Calls an async closure on each element of an iterator
This is equivalent to using a for loop on the iterator, although break and continue are not possible from a closure.
§Examples
Basic usage:
use std::time::Duration;
use async_iter_ext::AsyncIterTools;
use async_std::task;
task::block_on(async {
let items = [1, 2, 3, 4];
items
.iter()
.for_each_async(|item| {
async move {
// Simulate some async work
task::sleep(Duration::from_millis(100)).await;
println!("Processing {}", item);
}
})
.await;
});Sourcefn map_async<B, F, Fut>(self, f: F) -> AsyncMap<Self, F> ⓘ
fn map_async<B, F, Fut>(self, f: F) -> AsyncMap<Self, F> ⓘ
Applies an async closure to each item of the iterator, returning a new iterator of the results of each async computation.
This is similar to the standard Iterator::map method, but works with asynchronous
closures that return Futures.
§Examples
use std::time::Duration;
use async_iter_ext::{AsyncIterTools, AsyncIterator};
use async_std::task;
let multiplied_by_two = task::block_on(async {
let items = [1, 2, 3, 4];
items
.iter()
.map_async(|item| {
async move {
// Simulate async transformation
task::sleep(Duration::from_millis(100)).await;
item * 2
}
})
.async_collect::<Vec<_>>()
.await
});Sourcefn filter_async<F, Fut>(self, f: F) -> AsyncFilter<Self, F> ⓘ
fn filter_async<F, Fut>(self, f: F) -> AsyncFilter<Self, F> ⓘ
Filters the items of an iterator using an asynchronous predicate.
This works like the standard Iterator::filter, but allows the predicate
to be asynchronous by returning a Future<Output = bool>.
⚠️ Warning: The item type must implement
Clonebecause filtering requires that the item is cloned when the predicate is run.
§Examples
use async_iter_ext::{AsyncIterTools, AsyncIterator};
use async_std::task;
task::block_on(async {
let items = [1, 2, 3, 4, 5, 6];
let filtered = items
.iter()
.filter_async(|item| {
async move {
// Keep even numbers only
item % 2 == 0
}
})
.async_collect::<Vec<_>>();
});Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.