1
- use std:: marker:: PhantomData ;
2
1
use std:: pin:: Pin ;
3
- use std:: future:: Future ;
4
-
5
- use pin_project_lite:: pin_project;
6
2
7
3
use crate :: stream:: Stream ;
8
4
use crate :: task:: { Context , Poll } ;
9
5
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 ,
24
15
}
25
16
17
+ impl < F > Unpin for RepeatWith < F > { }
18
+
26
19
/// Creates a new stream that repeats elements of type `A` endlessly by applying the provided closure.
27
20
///
28
21
/// # Examples
@@ -35,7 +28,7 @@ pin_project! {
35
28
/// use async_std::prelude::*;
36
29
/// use async_std::stream;
37
30
///
38
- /// let s = stream::repeat_with(|| async { 1 } );
31
+ /// let s = stream::repeat_with(|| 1 );
39
32
///
40
33
/// pin_utils::pin_mut!(s);
41
34
///
@@ -54,48 +47,38 @@ pin_project! {
54
47
/// use async_std::prelude::*;
55
48
/// use async_std::stream;
56
49
///
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);
58
57
///
59
58
/// pin_utils::pin_mut!(s);
60
59
///
61
60
/// 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));
63
64
/// assert_eq!(s.next().await, None);
64
65
/// # })
65
66
/// ```
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 >
67
68
where
68
- F : FnMut ( ) -> Fut ,
69
- Fut : Future < Output = A > ,
69
+ F : FnMut ( ) -> T ,
70
70
{
71
- RepeatWith {
72
- f : repeater,
73
- future : None ,
74
- __a : PhantomData ,
75
- }
71
+ RepeatWith { f : repeater }
76
72
}
77
73
78
- impl < F , Fut , A > Stream for RepeatWith < F , Fut , A >
74
+ impl < T , F > Stream for RepeatWith < F >
79
75
where
80
- F : FnMut ( ) -> Fut ,
81
- Fut : Future < Output = A > ,
76
+ F : FnMut ( ) -> T ,
82
77
{
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 ;
96
79
97
- this . future . set ( Some ( fut ) ) ;
98
- }
99
- }
80
+ fn poll_next ( mut self : Pin < & mut Self > , _ : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
81
+ let item = ( & mut self . f ) ( ) ;
82
+ Poll :: Ready ( Some ( item ) )
100
83
}
101
84
}
0 commit comments