Skip to content

Commit ecd7f57

Browse files
committed
from/into stream
Signed-off-by: Yoshua Wuyts <[email protected]> update examples Signed-off-by: Yoshua Wuyts <[email protected]> impl collect Signed-off-by: Yoshua Wuyts <[email protected]> compiles! Signed-off-by: Yoshua Wuyts <[email protected]> layout base for collect into vec Signed-off-by: Yoshua Wuyts <[email protected]> fmt Signed-off-by: Yoshua Wuyts <[email protected]> progress Signed-off-by: Yoshua Wuyts <[email protected]> compiles! Signed-off-by: Yoshua Wuyts <[email protected]> define failing test Signed-off-by: Yoshua Wuyts <[email protected]> cargo fmt Signed-off-by: Yoshua Wuyts <[email protected]> stuck again Signed-off-by: Yoshua Wuyts <[email protected]> fix trait bounds! Signed-off-by: Yoshua Wuyts <[email protected]> cargo fmt Signed-off-by: Yoshua Wuyts <[email protected]> hide dyn fut impl Signed-off-by: Yoshua Wuyts <[email protected]> dyn ret for vec Signed-off-by: Yoshua Wuyts <[email protected]> cargo fmt Signed-off-by: Yoshua Wuyts <[email protected]> collect docs Signed-off-by: Yoshua Wuyts <[email protected]> remove macro from vec::from_stream Signed-off-by: Yoshua Wuyts <[email protected]> shorten collect trait bound Signed-off-by: Yoshua Wuyts <[email protected]> Remove some Unpin and Send bounds Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent f27f927 commit ecd7f57

File tree

7 files changed

+161
-1
lines changed

7 files changed

+161
-1
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ pub mod stream;
4242
pub mod sync;
4343
pub mod task;
4444

45+
mod vec;
46+
4547
pub(crate) mod utils;

src/stream/from_stream.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::IntoStream;
2+
3+
use std::pin::Pin;
4+
5+
/// Conversion from a `Stream`.
6+
///
7+
/// By implementing `FromStream` for a type, you define how it will be created from a stream.
8+
/// This is common for types which describe a collection of some kind.
9+
///
10+
/// See also: [`IntoStream`].
11+
///
12+
/// [`IntoStream`]: trait.IntoStream.html
13+
pub trait FromStream<T> {
14+
/// Creates a value from a stream.
15+
///
16+
/// # Examples
17+
///
18+
/// Basic usage:
19+
///
20+
/// ```
21+
/// // use async_std::stream::FromStream;
22+
///
23+
/// // let _five_fives = async_std::stream::repeat(5).take(5);
24+
/// ```
25+
fn from_stream<'a, S: IntoStream<Item = T> + 'a>(
26+
stream: S,
27+
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>;
28+
}

src/stream/into_stream.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use futures::stream::Stream;
2+
3+
/// Conversion into a `Stream`.
4+
///
5+
/// By implementing `IntoIterator` for a type, you define how it will be
6+
/// converted to an iterator. This is common for types which describe a
7+
/// collection of some kind.
8+
///
9+
/// [`from_stream`]: #tymethod.from_stream
10+
/// [`Stream`]: trait.Stream.html
11+
/// [`collect`]: trait.Stream.html#method.collect
12+
///
13+
/// See also: [`FromStream`].
14+
///
15+
/// [`FromStream`]: trait.FromStream.html
16+
pub trait IntoStream {
17+
/// The type of the elements being iterated over.
18+
type Item;
19+
20+
/// Which kind of stream are we turning this into?
21+
type IntoStream: Stream<Item = Self::Item>;
22+
23+
/// Creates a stream from a value.
24+
fn into_stream(self) -> Self::IntoStream;
25+
}
26+
27+
impl<I: Stream> IntoStream for I {
28+
type Item = I::Item;
29+
type IntoStream = I;
30+
31+
#[inline]
32+
fn into_stream(self) -> I {
33+
self
34+
}
35+
}

src/stream/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
//! ```
2323
2424
pub use empty::{empty, Empty};
25+
pub use from_stream::FromStream;
26+
pub use into_stream::IntoStream;
2527
pub use once::{once, Once};
2628
pub use repeat::{repeat, Repeat};
2729
pub use stream::{Stream, Take};
2830

2931
mod empty;
32+
mod from_stream;
33+
mod into_stream;
3034
mod min_by;
3135
mod once;
3236
mod repeat;

src/stream/stream.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::pin::Pin;
2626

2727
use cfg_if::cfg_if;
2828

29+
use super::from_stream::FromStream;
2930
use super::min_by::MinBy;
3031
use crate::future::Future;
3132
use crate::task::{Context, Poll};
@@ -51,6 +52,21 @@ cfg_if! {
5152
}
5253
}
5354

55+
cfg_if! {
56+
if #[cfg(feature = "docs")] {
57+
#[doc(hidden)]
58+
pub struct DynFuture<'a, T>(std::marker::PhantomData<&'a T>);
59+
60+
macro_rules! dyn_ret {
61+
($a:lifetime, $o:ty) => (DynFuture<$a, $o>);
62+
}
63+
} else {
64+
macro_rules! dyn_ret {
65+
($a:lifetime, $o:ty) => (Pin<Box<dyn core::future::Future<Output = $o> + 'a>>)
66+
}
67+
}
68+
}
69+
5470
/// An asynchronous stream of values.
5571
///
5672
/// This trait is an async version of [`std::iter::Iterator`].
@@ -227,7 +243,6 @@ pub trait Stream {
227243
///
228244
/// Basic usage:
229245
///
230-
/// ```
231246
/// # fn main() { async_std::task::block_on(async {
232247
/// #
233248
/// use async_std::prelude::*;
@@ -266,6 +281,48 @@ pub trait Stream {
266281
f,
267282
}
268283
}
284+
285+
/// Transforms a stream into a collection.
286+
///
287+
/// `collect()` can take anything streamable, and turn it into a relevant
288+
/// collection. This is one of the more powerful methods in the async
289+
/// standard library, used in a variety of contexts.
290+
///
291+
/// The most basic pattern in which `collect()` is used is to turn one
292+
/// collection into another. You take a collection, call [`stream`] on it,
293+
/// do a bunch of transformations, and then `collect()` at the end.
294+
///
295+
/// Because `collect()` is so general, it can cause problems with type
296+
/// inference. As such, `collect()` is one of the few times you'll see
297+
/// the syntax affectionately known as the 'turbofish': `::<>`. This
298+
/// helps the inference algorithm understand specifically which collection
299+
/// you're trying to collect into.
300+
///
301+
/// # Examples
302+
///
303+
/// ```
304+
/// # fn main() { async_std::task::block_on(async {
305+
/// #
306+
/// use async_std::prelude::*;
307+
/// use async_std::stream;
308+
///
309+
/// let s = stream::repeat(9u8).take(3);
310+
/// let buf: Vec<u8> = s.collect().await;
311+
///
312+
/// assert_eq!(buf, vec![9; 3]);
313+
/// #
314+
/// # }) }
315+
/// ```
316+
///
317+
/// [`stream`]: trait.Stream.html#tymethod.next
318+
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead (TODO)"]
319+
fn collect<'a, B>(self) -> dyn_ret!('a, B)
320+
where
321+
Self: futures::stream::Stream + Sized + 'a,
322+
B: FromStream<<Self as futures::stream::Stream>::Item>,
323+
{
324+
FromStream::from_stream(self)
325+
}
269326
}
270327

271328
impl<T: futures_core::stream::Stream + Unpin + ?Sized> Stream for T {

src/vec/from_stream.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::stream::{FromStream, IntoStream, Stream};
2+
3+
use std::pin::Pin;
4+
5+
impl<T> FromStream<T> for Vec<T> {
6+
#[inline]
7+
fn from_stream<'a, S: IntoStream<Item = T>>(
8+
stream: S,
9+
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
10+
where
11+
<S as IntoStream>::IntoStream: 'a,
12+
{
13+
let stream = stream.into_stream();
14+
15+
Pin::from(Box::new(async move {
16+
pin_utils::pin_mut!(stream);
17+
18+
let mut out = vec![];
19+
while let Some(item) = stream.next().await {
20+
out.push(item);
21+
}
22+
out
23+
}))
24+
}
25+
}

src/vec/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! The Rust core allocation and collections library
2+
//!
3+
//! This library provides smart pointers and collections for managing
4+
//! heap-allocated values.
5+
6+
mod from_stream;
7+
8+
#[doc(inline)]
9+
pub use std::vec::Vec;

0 commit comments

Comments
 (0)