@@ -4,17 +4,26 @@ use futures_core::future::{FusedFuture, Future};
4
4
use futures_core:: stream:: { FusedStream , Stream } ;
5
5
#[ cfg( feature = "sink" ) ]
6
6
use futures_sink:: Sink ;
7
- use pin_project:: pin_project;
8
7
9
8
/// Combines two different futures, streams, or sinks having the same associated types into a single
10
9
/// type.
11
- #[ pin_project( project = EitherProj ) ]
12
10
#[ derive( Debug , Clone ) ]
13
11
pub enum Either < A , B > {
14
12
/// First branch of the type
15
- Left ( # [ pin ] A ) ,
13
+ Left ( A ) ,
16
14
/// Second branch of the type
17
- Right ( #[ pin] B ) ,
15
+ Right ( B ) ,
16
+ }
17
+
18
+ impl < A , B > Either < A , B > {
19
+ fn project ( self : Pin < & mut Self > ) -> Either < Pin < & mut A > , Pin < & mut B > > {
20
+ unsafe {
21
+ match self . get_unchecked_mut ( ) {
22
+ Either :: Left ( a) => Either :: Left ( Pin :: new_unchecked ( a) ) ,
23
+ Either :: Right ( b) => Either :: Right ( Pin :: new_unchecked ( b) ) ,
24
+ }
25
+ }
26
+ }
18
27
}
19
28
20
29
impl < A , B , T > Either < ( T , A ) , ( T , B ) > {
60
69
61
70
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
62
71
match self . project ( ) {
63
- EitherProj :: Left ( x) => x. poll ( cx) ,
64
- EitherProj :: Right ( x) => x. poll ( cx) ,
72
+ Either :: Left ( x) => x. poll ( cx) ,
73
+ Either :: Right ( x) => x. poll ( cx) ,
65
74
}
66
75
}
67
76
}
88
97
89
98
fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
90
99
match self . project ( ) {
91
- EitherProj :: Left ( x) => x. poll_next ( cx) ,
92
- EitherProj :: Right ( x) => x. poll_next ( cx) ,
100
+ Either :: Left ( x) => x. poll_next ( cx) ,
101
+ Either :: Right ( x) => x. poll_next ( cx) ,
93
102
}
94
103
}
95
104
}
@@ -117,29 +126,29 @@ where
117
126
118
127
fn poll_ready ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
119
128
match self . project ( ) {
120
- EitherProj :: Left ( x) => x. poll_ready ( cx) ,
121
- EitherProj :: Right ( x) => x. poll_ready ( cx) ,
129
+ Either :: Left ( x) => x. poll_ready ( cx) ,
130
+ Either :: Right ( x) => x. poll_ready ( cx) ,
122
131
}
123
132
}
124
133
125
134
fn start_send ( self : Pin < & mut Self > , item : Item ) -> Result < ( ) , Self :: Error > {
126
135
match self . project ( ) {
127
- EitherProj :: Left ( x) => x. start_send ( item) ,
128
- EitherProj :: Right ( x) => x. start_send ( item) ,
136
+ Either :: Left ( x) => x. start_send ( item) ,
137
+ Either :: Right ( x) => x. start_send ( item) ,
129
138
}
130
139
}
131
140
132
141
fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
133
142
match self . project ( ) {
134
- EitherProj :: Left ( x) => x. poll_flush ( cx) ,
135
- EitherProj :: Right ( x) => x. poll_flush ( cx) ,
143
+ Either :: Left ( x) => x. poll_flush ( cx) ,
144
+ Either :: Right ( x) => x. poll_flush ( cx) ,
136
145
}
137
146
}
138
147
139
148
fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
140
149
match self . project ( ) {
141
- EitherProj :: Left ( x) => x. poll_close ( cx) ,
142
- EitherProj :: Right ( x) => x. poll_close ( cx) ,
150
+ Either :: Left ( x) => x. poll_close ( cx) ,
151
+ Either :: Right ( x) => x. poll_close ( cx) ,
143
152
}
144
153
}
145
154
}
@@ -176,8 +185,8 @@ mod if_std {
176
185
buf : & mut [ u8 ] ,
177
186
) -> Poll < Result < usize > > {
178
187
match self . project ( ) {
179
- EitherProj :: Left ( x) => x. poll_read ( cx, buf) ,
180
- EitherProj :: Right ( x) => x. poll_read ( cx, buf) ,
188
+ Either :: Left ( x) => x. poll_read ( cx, buf) ,
189
+ Either :: Right ( x) => x. poll_read ( cx, buf) ,
181
190
}
182
191
}
183
192
@@ -187,8 +196,8 @@ mod if_std {
187
196
bufs : & mut [ IoSliceMut < ' _ > ] ,
188
197
) -> Poll < Result < usize > > {
189
198
match self . project ( ) {
190
- EitherProj :: Left ( x) => x. poll_read_vectored ( cx, bufs) ,
191
- EitherProj :: Right ( x) => x. poll_read_vectored ( cx, bufs) ,
199
+ Either :: Left ( x) => x. poll_read_vectored ( cx, bufs) ,
200
+ Either :: Right ( x) => x. poll_read_vectored ( cx, bufs) ,
192
201
}
193
202
}
194
203
}
@@ -204,8 +213,8 @@ mod if_std {
204
213
buf : & [ u8 ] ,
205
214
) -> Poll < Result < usize > > {
206
215
match self . project ( ) {
207
- EitherProj :: Left ( x) => x. poll_write ( cx, buf) ,
208
- EitherProj :: Right ( x) => x. poll_write ( cx, buf) ,
216
+ Either :: Left ( x) => x. poll_write ( cx, buf) ,
217
+ Either :: Right ( x) => x. poll_write ( cx, buf) ,
209
218
}
210
219
}
211
220
@@ -215,22 +224,22 @@ mod if_std {
215
224
bufs : & [ IoSlice < ' _ > ] ,
216
225
) -> Poll < Result < usize > > {
217
226
match self . project ( ) {
218
- EitherProj :: Left ( x) => x. poll_write_vectored ( cx, bufs) ,
219
- EitherProj :: Right ( x) => x. poll_write_vectored ( cx, bufs) ,
227
+ Either :: Left ( x) => x. poll_write_vectored ( cx, bufs) ,
228
+ Either :: Right ( x) => x. poll_write_vectored ( cx, bufs) ,
220
229
}
221
230
}
222
231
223
232
fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) > > {
224
233
match self . project ( ) {
225
- EitherProj :: Left ( x) => x. poll_flush ( cx) ,
226
- EitherProj :: Right ( x) => x. poll_flush ( cx) ,
234
+ Either :: Left ( x) => x. poll_flush ( cx) ,
235
+ Either :: Right ( x) => x. poll_flush ( cx) ,
227
236
}
228
237
}
229
238
230
239
fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) > > {
231
240
match self . project ( ) {
232
- EitherProj :: Left ( x) => x. poll_close ( cx) ,
233
- EitherProj :: Right ( x) => x. poll_close ( cx) ,
241
+ Either :: Left ( x) => x. poll_close ( cx) ,
242
+ Either :: Right ( x) => x. poll_close ( cx) ,
234
243
}
235
244
}
236
245
}
@@ -246,8 +255,8 @@ mod if_std {
246
255
pos : SeekFrom ,
247
256
) -> Poll < Result < u64 > > {
248
257
match self . project ( ) {
249
- EitherProj :: Left ( x) => x. poll_seek ( cx, pos) ,
250
- EitherProj :: Right ( x) => x. poll_seek ( cx, pos) ,
258
+ Either :: Left ( x) => x. poll_seek ( cx, pos) ,
259
+ Either :: Right ( x) => x. poll_seek ( cx, pos) ,
251
260
}
252
261
}
253
262
}
@@ -259,15 +268,15 @@ mod if_std {
259
268
{
260
269
fn poll_fill_buf ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < & [ u8 ] > > {
261
270
match self . project ( ) {
262
- EitherProj :: Left ( x) => x. poll_fill_buf ( cx) ,
263
- EitherProj :: Right ( x) => x. poll_fill_buf ( cx) ,
271
+ Either :: Left ( x) => x. poll_fill_buf ( cx) ,
272
+ Either :: Right ( x) => x. poll_fill_buf ( cx) ,
264
273
}
265
274
}
266
275
267
276
fn consume ( self : Pin < & mut Self > , amt : usize ) {
268
277
match self . project ( ) {
269
- EitherProj :: Left ( x) => x. consume ( amt) ,
270
- EitherProj :: Right ( x) => x. consume ( amt) ,
278
+ Either :: Left ( x) => x. consume ( amt) ,
279
+ Either :: Right ( x) => x. consume ( amt) ,
271
280
}
272
281
}
273
282
}
0 commit comments