|
|
|
@ -3,7 +3,7 @@ use std::pin::Pin;
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
use crate::stream::IntoStream;
|
|
|
|
|
|
|
|
|
|
/// Extend a collection with the contents of a stream.
|
|
|
|
|
/// Extends a collection with the contents of a stream.
|
|
|
|
|
///
|
|
|
|
|
/// Streams produce a series of values asynchronously, and collections can also be thought of as a
|
|
|
|
|
/// series of values. The `Extend` trait bridges this gap, allowing you to extend a collection
|
|
|
|
@ -17,11 +17,11 @@ use crate::stream::IntoStream;
|
|
|
|
|
/// # async_std::task::block_on(async {
|
|
|
|
|
/// #
|
|
|
|
|
/// use async_std::prelude::*;
|
|
|
|
|
/// use async_std::stream::{self, Extend};
|
|
|
|
|
/// use async_std::stream;
|
|
|
|
|
///
|
|
|
|
|
/// let mut v: Vec<usize> = vec![1, 2];
|
|
|
|
|
/// let s = stream::repeat(3usize).take(3);
|
|
|
|
|
/// v.stream_extend(s).await;
|
|
|
|
|
/// stream::Extend::extend(&mut v, s).await;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(v, vec![1, 2, 3, 3, 3]);
|
|
|
|
|
/// #
|
|
|
|
@ -31,7 +31,7 @@ use crate::stream::IntoStream;
|
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
|
|
|
|
|
pub trait Extend<A> {
|
|
|
|
|
/// Extends a collection with the contents of a stream.
|
|
|
|
|
fn stream_extend<'a, T: IntoStream<Item = A> + 'a>(
|
|
|
|
|
fn extend<'a, T: IntoStream<Item = A> + 'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
stream: T,
|
|
|
|
|
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
|
|
|
|
@ -39,15 +39,37 @@ pub trait Extend<A> {
|
|
|
|
|
A: 'a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Extend<()> for () {
|
|
|
|
|
fn stream_extend<'a, T: IntoStream<Item = ()> + 'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
stream: T,
|
|
|
|
|
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
|
|
|
|
let stream = stream.into_stream();
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
pin_utils::pin_mut!(stream);
|
|
|
|
|
while let Some(_) = stream.next().await {}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
/// Extends a collection with the contents of a stream.
|
|
|
|
|
///
|
|
|
|
|
/// Streams produce a series of values asynchronously, and collections can also be thought of as a
|
|
|
|
|
/// series of values. The [`Extend`] trait bridges this gap, allowing you to extend a collection
|
|
|
|
|
/// asynchronously by including the contents of that stream. When extending a collection with an
|
|
|
|
|
/// already existing key, that entry is updated or, in the case of collections that permit multiple
|
|
|
|
|
/// entries with equal keys, that entry is inserted.
|
|
|
|
|
///
|
|
|
|
|
/// [`Extend`]: trait.Extend.html
|
|
|
|
|
///
|
|
|
|
|
/// ## Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # async_std::task::block_on(async {
|
|
|
|
|
/// #
|
|
|
|
|
/// use async_std::prelude::*;
|
|
|
|
|
/// use async_std::stream;
|
|
|
|
|
///
|
|
|
|
|
/// let mut v: Vec<usize> = vec![1, 2];
|
|
|
|
|
/// let s = stream::repeat(3usize).take(3);
|
|
|
|
|
/// stream::extend(&mut v, s).await;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(v, vec![1, 2, 3, 3, 3]);
|
|
|
|
|
/// #
|
|
|
|
|
/// # })
|
|
|
|
|
/// ```
|
|
|
|
|
pub async fn extend<'a, C, A, T>(collection: &mut C, stream: T)
|
|
|
|
|
where
|
|
|
|
|
C: Extend<A>,
|
|
|
|
|
A: 'a,
|
|
|
|
|
T: IntoStream<Item = A> + 'a,
|
|
|
|
|
{
|
|
|
|
|
Extend::extend(collection, stream).await
|
|
|
|
|
}
|
|
|
|
|