1- use std:: marker:: PhantomData ;
21use std:: pin:: Pin ;
3- use std:: future:: Future ;
4-
5- use pin_project_lite:: pin_project;
62
73use crate :: stream:: Stream ;
84use 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 >
6768where
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 >
7975where
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+ 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 ) )
10083 }
10184}
0 commit comments