@@ -3,7 +3,7 @@ use std::pin::Pin;
3
3
use crate :: prelude:: * ;
4
4
use crate :: stream:: IntoStream ;
5
5
6
- /// Extend a collection with the contents of a stream.
6
+ /// Extends a collection with the contents of a stream.
7
7
///
8
8
/// Streams produce a series of values asynchronously, and collections can also be thought of as a
9
9
/// series of values. The `Extend` trait bridges this gap, allowing you to extend a collection
@@ -17,11 +17,11 @@ use crate::stream::IntoStream;
17
17
/// # async_std::task::block_on(async {
18
18
/// #
19
19
/// use async_std::prelude::*;
20
- /// use async_std::stream::{self, Extend} ;
20
+ /// use async_std::stream;
21
21
///
22
22
/// let mut v: Vec<usize> = vec![1, 2];
23
23
/// let s = stream::repeat(3usize).take(3);
24
- /// v.stream_extend( s).await;
24
+ /// stream::Extend::extend(&mut v, s).await;
25
25
///
26
26
/// assert_eq!(v, vec![1, 2, 3, 3, 3]);
27
27
/// #
@@ -31,23 +31,45 @@ use crate::stream::IntoStream;
31
31
#[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
32
32
pub trait Extend < A > {
33
33
/// Extends a collection with the contents of a stream.
34
- fn stream_extend < ' a , T : IntoStream < Item = A > + ' a > (
34
+ fn extend < ' a , T : IntoStream < Item = A > + ' a > (
35
35
& ' a mut self ,
36
36
stream : T ,
37
37
) -> Pin < Box < dyn Future < Output = ( ) > + ' a > >
38
38
where
39
39
A : ' a ;
40
40
}
41
41
42
- impl Extend < ( ) > for ( ) {
43
- fn stream_extend < ' a , T : IntoStream < Item = ( ) > + ' a > (
44
- & ' a mut self ,
45
- stream : T ,
46
- ) -> Pin < Box < dyn Future < Output = ( ) > + ' a > > {
47
- let stream = stream. into_stream ( ) ;
48
- Box :: pin ( async move {
49
- pin_utils:: pin_mut!( stream) ;
50
- while let Some ( _) = stream. next ( ) . await { }
51
- } )
52
- }
42
+ /// Extends a collection with the contents of a stream.
43
+ ///
44
+ /// Streams produce a series of values asynchronously, and collections can also be thought of as a
45
+ /// series of values. The [`Extend`] trait bridges this gap, allowing you to extend a collection
46
+ /// asynchronously by including the contents of that stream. When extending a collection with an
47
+ /// already existing key, that entry is updated or, in the case of collections that permit multiple
48
+ /// entries with equal keys, that entry is inserted.
49
+ ///
50
+ /// [`Extend`]: trait.Extend.html
51
+ ///
52
+ /// ## Examples
53
+ ///
54
+ /// ```
55
+ /// # async_std::task::block_on(async {
56
+ /// #
57
+ /// use async_std::prelude::*;
58
+ /// use async_std::stream;
59
+ ///
60
+ /// let mut v: Vec<usize> = vec![1, 2];
61
+ /// let s = stream::repeat(3usize).take(3);
62
+ /// stream::extend(&mut v, s).await;
63
+ ///
64
+ /// assert_eq!(v, vec![1, 2, 3, 3, 3]);
65
+ /// #
66
+ /// # })
67
+ /// ```
68
+ pub async fn extend < ' a , C , A , T > ( collection : & mut C , stream : T )
69
+ where
70
+ C : Extend < A > ,
71
+ A : ' a ,
72
+ T : IntoStream < Item = A > + ' a ,
73
+ {
74
+ Extend :: extend ( collection, stream) . await
53
75
}
0 commit comments