1
1
use std:: pin:: Pin ;
2
2
use std:: mem;
3
3
4
- use crate :: future:: Future ;
5
4
use crate :: stream:: Stream ;
6
- use crate :: task:: { Context , Poll , ready } ;
5
+ use crate :: task:: { Context , Poll } ;
7
6
8
7
use pin_project_lite:: pin_project;
9
8
@@ -18,42 +17,26 @@ use pin_project_lite::pin_project;
18
17
/// use async_std::prelude::*;
19
18
/// use async_std::stream;
20
19
///
21
- /// let s = stream::successors(Some(22), |&val| {
22
- /// async move {
23
- /// Some(val + 1)
24
- /// }
25
- /// });
20
+ /// let s = stream::successors(Some(22), |&val| Some(val + 1) );
26
21
///
27
22
/// pin_utils::pin_mut!(s);
28
23
/// assert_eq!(s.next().await, Some(22));
29
24
/// assert_eq!(s.next().await, Some(23));
30
25
/// assert_eq!(s.next().await, Some(24));
31
26
/// assert_eq!(s.next().await, Some(25));
32
27
///
33
- ///
34
- ///let never = stream::successors(None, |_| {
35
- /// async move {
36
- /// Some(1)
37
- /// }
38
- /// });
39
- ///
40
- /// pin_utils::pin_mut!(never);
41
- /// assert_eq!(never.next().await, None);
42
- /// assert_eq!(never.next().await, None);
43
28
/// #
44
29
/// # }) }
45
30
///
46
31
/// ```
47
32
#[ cfg( feature = "unstable" ) ]
48
33
#[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
49
- pub fn successors < F , Fut , T > ( first : Option < T > , succ : F ) -> Successors < F , Fut , T >
34
+ pub fn successors < F , T > ( first : Option < T > , succ : F ) -> Successors < F , T >
50
35
where
51
- F : FnMut ( & T ) -> Fut ,
52
- Fut : Future < Output = Option < T > > ,
36
+ F : FnMut ( & T ) -> Option < T > ,
53
37
{
54
38
Successors {
55
- succ : succ,
56
- future : None ,
39
+ succ,
57
40
slot : first,
58
41
}
59
42
}
@@ -68,39 +51,29 @@ pin_project! {
68
51
#[ cfg( feature = "unstable" ) ]
69
52
#[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
70
53
#[ derive( Debug ) ]
71
- pub struct Successors <F , Fut , T >
54
+ pub struct Successors <F , T >
72
55
where
73
- Fut : Future < Output = Option <T >> ,
56
+ F : FnMut ( & T ) -> Option <T >
74
57
{
75
58
succ: F ,
76
- #[ pin]
77
- future: Option <Fut >,
78
59
slot: Option <T >,
79
60
}
80
61
}
81
62
82
- impl < F , Fut , T > Stream for Successors < F , Fut , T >
63
+ impl < F , T > Stream for Successors < F , T >
83
64
where
84
- Fut : Future < Output = Option < T > > ,
85
- F : FnMut ( & T ) -> Fut ,
65
+ F : FnMut ( & T ) -> Option < T > ,
86
66
{
87
67
type Item = T ;
88
68
89
- fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
90
- let mut this = self . project ( ) ;
69
+ fn poll_next ( self : Pin < & mut Self > , _cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
70
+ let this = self . project ( ) ;
91
71
92
72
if this. slot . is_none ( ) {
93
73
return Poll :: Ready ( None ) ;
94
74
}
95
75
96
- if this. future . is_none ( ) {
97
- let fut = ( this. succ ) ( this. slot . as_ref ( ) . unwrap ( ) ) ;
98
- this. future . set ( Some ( fut) ) ;
99
- }
100
-
101
- let mut next = ready ! ( this. future. as_mut( ) . as_pin_mut( ) . unwrap( ) . poll( cx) ) ;
102
-
103
- this. future . set ( None ) ;
76
+ let mut next = ( this. succ ) ( & this. slot . as_ref ( ) . unwrap ( ) ) ;
104
77
105
78
// 'swapping' here means 'slot' will hold the next value and next will be th one from the previous iteration
106
79
mem:: swap ( this. slot , & mut next) ;
0 commit comments