Skip to content

Rename stream_extend to extend #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
3 commits merged into from Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/collections/binary_heap/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::BinaryHeap;
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
use crate::stream::{self, IntoStream};

impl<T: Ord> Extend<T> for BinaryHeap<T> {
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
impl<T: Ord> stream::Extend<T> for BinaryHeap<T> {
fn extend<'a, S: IntoStream<Item = T> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/binary_heap/from_stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BinaryHeap;
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

impl<T: Ord> FromStream<T> for BinaryHeap<T> {
#[inline]
Expand All @@ -17,7 +17,7 @@ impl<T: Ord> FromStream<T> for BinaryHeap<T> {
pin_utils::pin_mut!(stream);

let mut out = BinaryHeap::new();
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/collections/btree_map/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::BTreeMap;
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
use crate::stream::{self, IntoStream};

impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
fn stream_extend<'a, S: IntoStream<Item = (K, V)> + 'a>(
impl<K: Ord, V> stream::Extend<(K, V)> for BTreeMap<K, V> {
fn extend<'a, S: IntoStream<Item = (K, V)> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/btree_map/from_stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

impl<K: Ord, V> FromStream<(K, V)> for BTreeMap<K, V> {
#[inline]
Expand All @@ -17,7 +17,7 @@ impl<K: Ord, V> FromStream<(K, V)> for BTreeMap<K, V> {
pin_utils::pin_mut!(stream);

let mut out = BTreeMap::new();
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/collections/btree_set/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::BTreeSet;
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
use crate::stream::{self, IntoStream};

impl<T: Ord> Extend<T> for BTreeSet<T> {
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
impl<T: Ord> stream::Extend<T> for BTreeSet<T> {
fn extend<'a, S: IntoStream<Item = T> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/btree_set/from_stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeSet;
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

impl<T: Ord> FromStream<T> for BTreeSet<T> {
#[inline]
Expand All @@ -17,7 +17,7 @@ impl<T: Ord> FromStream<T> for BTreeSet<T> {
pin_utils::pin_mut!(stream);

let mut out = BTreeSet::new();
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/collections/hash_map/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::hash::{BuildHasher, Hash};
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
use crate::stream::{self, IntoStream};

impl<K, V, H> Extend<(K, V)> for HashMap<K, V, H>
impl<K, V, H> stream::Extend<(K, V)> for HashMap<K, V, H>
where
K: Eq + Hash,
H: BuildHasher + Default,
{
fn stream_extend<'a, S: IntoStream<Item = (K, V)> + 'a>(
fn extend<'a, S: IntoStream<Item = (K, V)> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/hash_map/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::hash::{BuildHasher, Hash};
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

impl<K, V, H> FromStream<(K, V)> for HashMap<K, V, H>
where
Expand All @@ -22,7 +22,7 @@ where
pin_utils::pin_mut!(stream);

let mut out = HashMap::with_hasher(Default::default());
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/collections/hash_set/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::hash::{BuildHasher, Hash};
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
use crate::stream::{self, IntoStream};

impl<T, H> Extend<T> for HashSet<T, H>
impl<T, H> stream::Extend<T> for HashSet<T, H>
where
T: Eq + Hash,
H: BuildHasher + Default,
{
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
fn extend<'a, S: IntoStream<Item = T> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/hash_set/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashSet;
use std::hash::{BuildHasher, Hash};
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

impl<T, H> FromStream<T> for HashSet<T, H>
where
Expand All @@ -22,7 +22,7 @@ where
pin_utils::pin_mut!(stream);

let mut out = HashSet::with_hasher(Default::default());
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/collections/linked_list/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::LinkedList;
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
use crate::stream::{self, IntoStream};

impl<T> Extend<T> for LinkedList<T> {
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
impl<T> stream::Extend<T> for LinkedList<T> {
fn extend<'a, S: IntoStream<Item = T> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/linked_list/from_stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::LinkedList;
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

impl<T> FromStream<T> for LinkedList<T> {
#[inline]
Expand All @@ -17,7 +17,7 @@ impl<T> FromStream<T> for LinkedList<T> {
pin_utils::pin_mut!(stream);

let mut out = LinkedList::new();
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/collections/vec_deque/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::VecDeque;
use std::pin::Pin;

use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
use crate::stream::{self, IntoStream};

impl<T> Extend<T> for VecDeque<T> {
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
impl<T> stream::Extend<T> for VecDeque<T> {
fn extend<'a, S: IntoStream<Item = T> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/vec_deque/from_stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::VecDeque;
use std::pin::Pin;

use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

impl<T> FromStream<T> for VecDeque<T> {
#[inline]
Expand All @@ -17,7 +17,7 @@ impl<T> FromStream<T> for VecDeque<T> {
pin_utils::pin_mut!(stream);

let mut out = VecDeque::new();
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/path/pathbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::path::Path;
#[cfg(feature = "unstable")]
use crate::prelude::*;
#[cfg(feature = "unstable")]
use crate::stream::{Extend, FromStream, IntoStream};
use crate::stream::{self, FromStream, IntoStream};

/// This struct is an async version of [`std::path::PathBuf`].
///
Expand Down Expand Up @@ -241,8 +241,8 @@ impl AsRef<std::path::Path> for PathBuf {
}

#[cfg(feature = "unstable")]
impl<P: AsRef<Path>> Extend<P> for PathBuf {
fn stream_extend<'a, S: IntoStream<Item = P>>(
impl<P: AsRef<Path>> stream::Extend<P> for PathBuf {
fn extend<'a, S: IntoStream<Item = P>>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
Expand Down Expand Up @@ -275,7 +275,7 @@ impl<'b, P: AsRef<Path> + 'b> FromStream<P> for PathBuf {
pin_utils::pin_mut!(stream);

let mut out = Self::new();
out.stream_extend(stream).await;
stream::extend(&mut out, stream).await;
out
})
}
Expand Down
52 changes: 37 additions & 15 deletions src/stream/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
/// #
Expand All @@ -31,23 +31,45 @@ 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>>
where
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
}
5 changes: 2 additions & 3 deletions src/stream/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ use std::pin::Pin;
///
/// ```
/// use async_std::prelude::*;
/// use async_std::stream::{Extend, FromStream, IntoStream};
/// use async_std::stream;
/// use async_std::stream::{self, FromStream, IntoStream};
/// use std::pin::Pin;
///
/// // A sample collection, that's just a wrapper over Vec<T>
Expand Down Expand Up @@ -77,7 +76,7 @@ use std::pin::Pin;
/// let mut c = MyCollection::new();
///
/// let mut v = vec![];
/// v.stream_extend(stream).await;
/// stream::extend(&mut v, stream).await;
///
/// for i in v {
/// c.add(i);
Expand Down
2 changes: 1 addition & 1 deletion src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ cfg_unstable! {

pub use double_ended_stream::DoubleEndedStream;
pub use exact_size_stream::ExactSizeStream;
pub use extend::Extend;
pub use extend::{extend, Extend};
pub use from_stream::FromStream;
pub use fused_stream::FusedStream;
pub use interval::{interval, Interval};
Expand Down
Loading