async_iterator/
from_iterator.rs

1use crate::IntoIterator;
2
3#[cfg(any(feature = "alloc", feature = "std"))]
4use crate::Iterator;
5
6/// Conversion from an [`Iterator`].
7
8pub trait FromIterator<A>: Sized {
9    /// Creates a value from an iterator.
10    async fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
11}
12
13#[cfg(any(feature = "alloc", feature = "std"))]
14impl<T> FromIterator<T> for std::vec::Vec<T> {
15    async fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> std::vec::Vec<T> {
16        let mut iter = iter.into_iter().await;
17        let mut output = std::vec::Vec::with_capacity(iter.size_hint().1.unwrap_or_default());
18        while let Some(item) = iter.next().await {
19            output.push(item);
20        }
21        output
22    }
23}