async_iterator/
extend.rs

1use crate::IntoIterator;
2
3#[cfg(any(feature = "alloc", feature = "std"))]
4use crate::Iterator;
5
6/// Extend a collection with the contents of an iterator.
7
8pub trait Extend<A> {
9    /// Extends a collection with the contents of an iterator.
10    async fn extend<T>(&mut self, iter: T)
11    where
12        T: IntoIterator<Item = A>;
13}
14
15#[cfg(any(feature = "alloc", feature = "std"))]
16impl<T> Extend<T> for std::vec::Vec<T> {
17    async fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
18        let mut iter = iter.into_iter().await;
19        self.reserve(iter.size_hint().1.unwrap_or_default());
20        while let Some(item) = iter.next().await {
21            self.push(item);
22        }
23    }
24}