Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76c5ffe

Browse files
Merge pull request #506 from stjepang/cleanup-stream
Cleanup stream module
2 parents 352f18b + 5438258 commit 76c5ffe

31 files changed

+315
-428
lines changed

‎src/io/mod.rs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
//! [`File`]s:
2020
//!
2121
//! ```no_run
22-
//! use async_std::prelude::*;
2322
//! use async_std::fs::File;
23+
//! use async_std::prelude::*;
2424
//!
2525
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
2626
//! #
@@ -47,9 +47,9 @@
4747
//! coming from:
4848
//!
4949
//! ```no_run
50-
//! use async_std::io::prelude::*;
51-
//! use async_std::io::SeekFrom;
5250
//! use async_std::fs::File;
51+
//! use async_std::io::SeekFrom;
52+
//! use async_std::prelude::*;
5353
//!
5454
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
5555
//! #
@@ -82,9 +82,9 @@
8282
//! methods to any reader:
8383
//!
8484
//! ```no_run
85-
//! use async_std::io::prelude::*;
86-
//! use async_std::io::BufReader;
8785
//! use async_std::fs::File;
86+
//! use async_std::io::BufReader;
87+
//! use async_std::prelude::*;
8888
//!
8989
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
9090
//! #
@@ -104,9 +104,9 @@
104104
//! to [`write`][`Write::write`]:
105105
//!
106106
//! ```no_run
107-
//! use async_std::io::prelude::*;
108-
//! use async_std::io::BufWriter;
109107
//! use async_std::fs::File;
108+
//! use async_std::io::BufWriter;
109+
//! use async_std::io::prelude::*;
110110
//!
111111
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
112112
//! #
@@ -179,9 +179,9 @@
179179
//! lines:
180180
//!
181181
//! ```no_run
182-
//! use async_std::prelude::*;
183-
//! use async_std::io::BufReader;
184182
//! use async_std::fs::File;
183+
//! use async_std::io::BufReader;
184+
//! use async_std::prelude::*;
185185
//!
186186
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
187187
//! #

‎src/stream/extend.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ pub trait Extend<A> {
6565
/// ```
6666
#[cfg(feature = "unstable")]
6767
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
68-
pub async fn extend<'a, C, A,T>(collection: &mut C, stream: T)
68+
pub async fn extend<'a, C, T,S>(collection: &mut C, stream: S)
6969
where
70-
C: Extend<A>,
71-
T: IntoStream<Item = A> + 'a,
70+
C: Extend<T>,
71+
S: IntoStream<Item = T> + 'a,
7272
{
7373
Extend::extend(collection, stream).await
7474
}

‎src/stream/from_fn.rs‎

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
use std::marker::PhantomData;
21
use std::pin::Pin;
3-
use std::future::Future;
4-
5-
use pin_project_lite::pin_project;
62

73
use crate::stream::Stream;
84
use crate::task::{Context, Poll};
95

10-
pin_project! {
11-
/// A stream that yields elements by calling a closure.
12-
///
13-
/// This stream is created by the [`from_fn`] function. See its
14-
/// documentation for more.
15-
///
16-
/// [`from_fn`]: fn.from_fn.html
17-
#[derive(Debug)]
18-
pub struct FromFn<F, Fut, T> {
19-
f: F,
20-
#[pin]
21-
future: Option<Fut>,
22-
__t: PhantomData<T>,
23-
}
6+
/// A stream that yields elements by calling a closure.
7+
///
8+
/// This stream is created by the [`from_fn`] function. See its
9+
/// documentation for more.
10+
///
11+
/// [`from_fn`]: fn.from_fn.html
12+
#[derive(Clone, Debug)]
13+
pub struct FromFn<F> {
14+
f: F,
2415
}
2516

17+
impl<F> Unpin for FromFn<F> {}
18+
2619
/// Creates a new stream where to produce each new element a provided closure is called.
2720
///
2821
/// This allows creating a custom stream with any behaviour without using the more verbose
@@ -34,21 +27,15 @@ pin_project! {
3427
/// # async_std::task::block_on(async {
3528
/// #
3629
/// use async_std::prelude::*;
37-
/// use async_std::sync::{Arc, Mutex};
3830
/// use async_std::stream;
3931
///
40-
/// let count = Arc::new(Mutex::new(0u8));
32+
/// let mut count = 0u8;
4133
/// let s = stream::from_fn(|| {
42-
/// let count = Arc::clone(&count);
43-
///
44-
/// async move {
45-
/// *count.lock().await += 1;
46-
///
47-
/// if *count.lock().await > 3 {
48-
/// None
49-
/// } else {
50-
/// Some(*count.lock().await)
51-
/// }
34+
/// count += 1;
35+
/// if count > 3 {
36+
/// None
37+
/// } else {
38+
/// Some(count)
5239
/// }
5340
/// });
5441
///
@@ -60,38 +47,21 @@ pin_project! {
6047
/// #
6148
/// # })
6249
/// ```
63-
pub fn from_fn<T, F,Fut>(f: F) -> FromFn<F,Fut,T>
50+
pub fn from_fn<T, F>(f: F) -> FromFn<F>
6451
where
65-
F: FnMut() -> Fut,
66-
Fut: Future<Output = Option<T>>,
52+
F: FnMut() -> Option<T>,
6753
{
68-
FromFn {
69-
f,
70-
future: None,
71-
__t: PhantomData,
72-
}
54+
FromFn { f }
7355
}
7456

75-
impl<F,Fut,T> Stream for FromFn<F,Fut,T>
57+
impl<T,F> Stream for FromFn<F>
7658
where
77-
F: FnMut() -> Fut,
78-
Fut: Future<Output = Option<T>>,
59+
F: FnMut() -> Option<T>,
7960
{
8061
type Item = T;
8162

82-
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
83-
let mut this = self.project();
84-
loop {
85-
if this.future.is_some() {
86-
let next =
87-
futures_core::ready!(this.future.as_mut().as_pin_mut().unwrap().poll(cx));
88-
this.future.set(None);
89-
90-
return Poll::Ready(next);
91-
} else {
92-
let fut = (this.f)();
93-
this.future.set(Some(fut));
94-
}
95-
}
63+
fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
64+
let item = (&mut self.f)();
65+
Poll::Ready(item)
9666
}
9767
}

‎src/stream/from_iter.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::stream::Stream;
66
use crate::task::{Context, Poll};
77

88
pin_project! {
9-
/// A stream that created from iterator
9+
/// A stream that was created from iterator.
1010
///
1111
/// This stream is created by the [`from_iter`] function.
1212
/// See it documentation for more.

‎src/stream/mod.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,7 @@ pub use from_iter::{from_iter, FromIter};
306306
pub use once::{once, Once};
307307
pub use repeat::{repeat, Repeat};
308308
pub use repeat_with::{repeat_with, RepeatWith};
309-
pub use stream::{
310-
Chain, Filter, Fuse, Inspect, Scan, Skip, SkipWhile, StepBy, Stream, Take, TakeWhile, Zip,
311-
};
309+
pub use stream::*;
312310

313311
pub(crate) mod stream;
314312

‎src/stream/once.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pin_project! {
3333
/// documentation for more.
3434
///
3535
/// [`once`]: fn.once.html
36-
#[derive(Debug)]
36+
#[derive(Clone,Debug)]
3737
pub struct Once<T> {
3838
value: Option<T>,
3939
}

‎src/stream/repeat.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
/// documentation for more.
3434
///
3535
/// [`repeat`]: fn.repeat.html
36-
#[derive(Debug)]
36+
#[derive(Clone,Debug)]
3737
pub struct Repeat<T> {
3838
item: T,
3939
}

‎src/stream/repeat_with.rs‎

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
use std::marker::PhantomData;
21
use std::pin::Pin;
3-
use std::future::Future;
4-
5-
use pin_project_lite::pin_project;
62

73
use crate::stream::Stream;
84
use crate::task::{Context, Poll};
95

10-
pin_project! {
11-
/// A stream that repeats elements of type `T` endlessly by applying a provided closure.
12-
///
13-
/// This stream is created by the [`repeat_with`] function. See its
14-
/// documentation for more.
15-
///
16-
/// [`repeat_with`]: fn.repeat_with.html
17-
#[derive(Debug)]
18-
pub struct RepeatWith<F, Fut, A> {
19-
f: F,
20-
#[pin]
21-
future: Option<Fut>,
22-
__a: PhantomData<A>,
23-
}
6+
/// A stream that repeats elements of type `T` endlessly by applying a provided closure.
7+
///
8+
/// This stream is created by the [`repeat_with`] function. See its
9+
/// documentation for more.
10+
///
11+
/// [`repeat_with`]: fn.repeat_with.html
12+
#[derive(Clone, Debug)]
13+
pub struct RepeatWith<F> {
14+
f: F,
2415
}
2516

17+
impl<F> Unpin for RepeatWith<F> {}
18+
2619
/// Creates a new stream that repeats elements of type `A` endlessly by applying the provided closure.
2720
///
2821
/// # Examples
@@ -35,7 +28,7 @@ pin_project! {
3528
/// use async_std::prelude::*;
3629
/// use async_std::stream;
3730
///
38-
/// let s = stream::repeat_with(|| async { 1 });
31+
/// let s = stream::repeat_with(|| 1);
3932
///
4033
/// pin_utils::pin_mut!(s);
4134
///
@@ -54,48 +47,38 @@ pin_project! {
5447
/// use async_std::prelude::*;
5548
/// use async_std::stream;
5649
///
57-
/// let s = stream::repeat_with(|| async { 1u8 }).take(2);
50+
/// let mut n = 1;
51+
/// let s = stream::repeat_with(|| {
52+
/// let item = n;
53+
/// n *= 2;
54+
/// item
55+
/// })
56+
/// .take(4);
5857
///
5958
/// pin_utils::pin_mut!(s);
6059
///
6160
/// assert_eq!(s.next().await, Some(1));
62-
/// assert_eq!(s.next().await, Some(1));
61+
/// assert_eq!(s.next().await, Some(2));
62+
/// assert_eq!(s.next().await, Some(4));
63+
/// assert_eq!(s.next().await, Some(8));
6364
/// assert_eq!(s.next().await, None);
6465
/// # })
6566
/// ```
66-
pub fn repeat_with<F,Fut,A>(repeater: F) -> RepeatWith<F,Fut,A>
67+
pub fn repeat_with<T,F>(repeater: F) -> RepeatWith<F>
6768
where
68-
F: FnMut() -> Fut,
69-
Fut: Future<Output = A>,
69+
F: FnMut() -> T,
7070
{
71-
RepeatWith {
72-
f: repeater,
73-
future: None,
74-
__a: PhantomData,
75-
}
71+
RepeatWith { f: repeater }
7672
}
7773

78-
impl<F,Fut,A> Stream for RepeatWith<F,Fut,A>
74+
impl<T,F> Stream for RepeatWith<F>
7975
where
80-
F: FnMut() -> Fut,
81-
Fut: Future<Output = A>,
76+
F: FnMut() -> T,
8277
{
83-
type Item = A;
84-
85-
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
86-
let mut this = self.project();
87-
loop {
88-
if this.future.is_some() {
89-
let res = futures_core::ready!(this.future.as_mut().as_pin_mut().unwrap().poll(cx));
90-
91-
this.future.set(None);
92-
93-
return Poll::Ready(Some(res));
94-
} else {
95-
let fut = (this.f)();
78+
type Item = T;
9679

97-
this.future.set(Some(fut));
98-
}
99-
}
80+
fnpoll_next(mutself:Pin<&mutSelf>, _:&mutContext<'_>) -> Poll<Option<Self::Item>>{
81+
let item = (&mutself.f)();
82+
Poll::Ready(Some(item))
10083
}
10184
}

‎src/stream/stream/chain.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::prelude::*;
77
use crate::task::{Context, Poll};
88

99
pin_project! {
10-
/// Chains two streams one after another.
10+
/// A stream that chains two streams one after another.
1111
///
1212
/// This `struct` is created by the [`chain`] method on [`Stream`]. See its
1313
/// documentation for more.

‎src/stream/stream/cloned.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use pin_project_lite::pin_project;
44
use std::pin::Pin;
55

66
pin_project! {
7+
/// A stream that clones the elements of an underlying stream.
78
#[derive(Debug)]
89
pub struct Cloned<S> {
910
#[pin]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /