1
1
use core:: fmt;
2
2
use core:: marker:: PhantomData ;
3
- use core:: mem;
4
3
use core:: pin:: Pin ;
5
4
use futures_core:: future:: Future ;
6
5
use futures_core:: stream:: Stream ;
@@ -13,8 +12,8 @@ use pin_utils::{unsafe_pinned, unsafe_unpinned};
13
12
pub struct With < Si , Item , U , Fut , F > {
14
13
sink : Si ,
15
14
f : F ,
16
- state : State < Fut , Item > ,
17
- _phantom : PhantomData < fn ( U ) > ,
15
+ state : Option < Fut > ,
16
+ _phantom : PhantomData < fn ( U ) -> Item > ,
18
17
}
19
18
20
19
impl < Si , Item , U , Fut , F > Unpin for With < Si , Item , U , Fut , F >
@@ -27,7 +26,6 @@ impl<Si, Item, U, Fut, F> fmt::Debug for With<Si, Item, U, Fut, F>
27
26
where
28
27
Si : fmt:: Debug ,
29
28
Fut : fmt:: Debug ,
30
- Item : fmt:: Debug ,
31
29
{
32
30
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
33
31
f. debug_struct ( "With" )
@@ -44,45 +42,22 @@ where Si: Sink<Item>,
44
42
{
45
43
unsafe_pinned ! ( sink: Si ) ;
46
44
unsafe_unpinned ! ( f: F ) ;
47
- unsafe_pinned ! ( state: State <Fut , Item >) ;
45
+ unsafe_pinned ! ( state: Option <Fut >) ;
48
46
49
47
pub ( super ) fn new < E > ( sink : Si , f : F ) -> Self
50
48
where
51
49
Fut : Future < Output = Result < Item , E > > ,
52
50
E : From < Si :: Error > ,
53
51
{
54
52
With {
55
- state : State :: Empty ,
53
+ state : None ,
56
54
sink,
57
55
f,
58
56
_phantom : PhantomData ,
59
57
}
60
58
}
61
59
}
62
60
63
- #[ derive( Debug ) ]
64
- enum State < Fut , T > {
65
- Empty ,
66
- Process ( Fut ) ,
67
- Buffered ( T ) ,
68
- }
69
-
70
- impl < Fut , T > State < Fut , T > {
71
- #[ allow( clippy:: wrong_self_convention) ]
72
- fn as_pin_mut ( self : Pin < & mut Self > ) -> State < Pin < & mut Fut > , Pin < & mut T > > {
73
- unsafe {
74
- match self . get_unchecked_mut ( ) {
75
- State :: Empty =>
76
- State :: Empty ,
77
- State :: Process ( fut) =>
78
- State :: Process ( Pin :: new_unchecked ( fut) ) ,
79
- State :: Buffered ( item) =>
80
- State :: Buffered ( Pin :: new_unchecked ( item) ) ,
81
- }
82
- }
83
- }
84
- }
85
-
86
61
// Forwarding impl of Stream from the underlying sink
87
62
impl < S , Item , U , Fut , F > Stream for With < S , Item , U , Fut , F >
88
63
where S : Stream + Sink < Item > ,
@@ -132,23 +107,18 @@ impl<Si, Item, U, Fut, F, E> With<Si, Item, U, Fut, F>
132
107
self . sink
133
108
}
134
109
110
+ /// Completes the processing of previous item if any.
135
111
fn poll (
136
112
mut self : Pin < & mut Self > ,
137
113
cx : & mut Context < ' _ > ,
138
114
) -> Poll < Result < ( ) , E > > {
139
- let buffered = match self . as_mut ( ) . state ( ) . as_pin_mut ( ) {
140
- State :: Empty => return Poll :: Ready ( Ok ( ( ) ) ) ,
141
- State :: Process ( fut) => Some ( ready ! ( fut. poll( cx) ) ?) ,
142
- State :: Buffered ( _) => None ,
115
+ let item = match self . as_mut ( ) . state ( ) . as_pin_mut ( ) {
116
+ None => return Poll :: Ready ( Ok ( ( ) ) ) ,
117
+ Some ( fut) => ready ! ( fut. poll( cx) ) ?,
143
118
} ;
144
- if let Some ( buffered) = buffered {
145
- self . as_mut ( ) . state ( ) . set ( State :: Buffered ( buffered) ) ;
146
- }
147
- if let State :: Buffered ( item) = unsafe { mem:: replace ( self . as_mut ( ) . state ( ) . get_unchecked_mut ( ) , State :: Empty ) } {
148
- Poll :: Ready ( self . as_mut ( ) . sink ( ) . start_send ( item) . map_err ( Into :: into) )
149
- } else {
150
- unreachable ! ( )
151
- }
119
+ self . as_mut ( ) . state ( ) . set ( None ) ;
120
+ self . as_mut ( ) . sink ( ) . start_send ( item) ?;
121
+ Poll :: Ready ( Ok ( ( ) ) )
152
122
}
153
123
}
154
124
@@ -161,18 +131,20 @@ impl<Si, Item, U, Fut, F, E> Sink<U> for With<Si, Item, U, Fut, F>
161
131
type Error = E ;
162
132
163
133
fn poll_ready (
164
- self : Pin < & mut Self > ,
134
+ mut self : Pin < & mut Self > ,
165
135
cx : & mut Context < ' _ > ,
166
136
) -> Poll < Result < ( ) , Self :: Error > > {
167
- self . poll ( cx)
137
+ ready ! ( self . as_mut( ) . poll( cx) ) ?;
138
+ ready ! ( self . as_mut( ) . sink( ) . poll_ready( cx) ?) ;
139
+ Poll :: Ready ( Ok ( ( ) ) )
168
140
}
169
141
170
142
fn start_send (
171
143
mut self : Pin < & mut Self > ,
172
144
item : U ,
173
145
) -> Result < ( ) , Self :: Error > {
174
- let item = ( self . as_mut ( ) . f ( ) ) ( item) ;
175
- self . as_mut ( ) . state ( ) . set ( State :: Process ( item ) ) ;
146
+ let future = ( self . as_mut ( ) . f ( ) ) ( item) ;
147
+ self . as_mut ( ) . state ( ) . set ( Some ( future ) ) ;
176
148
Ok ( ( ) )
177
149
}
178
150
0 commit comments