@@ -4,17 +4,26 @@ use futures_core::future::{FusedFuture, Future};
44use futures_core:: stream:: { FusedStream , Stream } ;
55#[ cfg( feature = "sink" ) ]
66use futures_sink:: Sink ;
7- use pin_project:: pin_project;
87
98/// Combines two different futures, streams, or sinks having the same associated types into a single
109/// type.
11- #[ pin_project( project = EitherProj ) ]
1210#[ derive( Debug , Clone ) ]
1311pub enum Either < A , B > {
1412 /// First branch of the type
15- Left ( # [ pin ] A ) ,
13+ Left ( A ) ,
1614 /// 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+ }
1827}
1928
2029impl < A , B , T > Either < ( T , A ) , ( T , B ) > {
6069
6170 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
6271 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) ,
6574 }
6675 }
6776}
8897
8998 fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
9099 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) ,
93102 }
94103 }
95104}
@@ -117,29 +126,29 @@ where
117126
118127 fn poll_ready ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
119128 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) ,
122131 }
123132 }
124133
125134 fn start_send ( self : Pin < & mut Self > , item : Item ) -> Result < ( ) , Self :: Error > {
126135 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) ,
129138 }
130139 }
131140
132141 fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
133142 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) ,
136145 }
137146 }
138147
139148 fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
140149 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) ,
143152 }
144153 }
145154}
@@ -176,8 +185,8 @@ mod if_std {
176185 buf : & mut [ u8 ] ,
177186 ) -> Poll < Result < usize > > {
178187 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) ,
181190 }
182191 }
183192
@@ -187,8 +196,8 @@ mod if_std {
187196 bufs : & mut [ IoSliceMut < ' _ > ] ,
188197 ) -> Poll < Result < usize > > {
189198 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) ,
192201 }
193202 }
194203 }
@@ -204,8 +213,8 @@ mod if_std {
204213 buf : & [ u8 ] ,
205214 ) -> Poll < Result < usize > > {
206215 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) ,
209218 }
210219 }
211220
@@ -215,22 +224,22 @@ mod if_std {
215224 bufs : & [ IoSlice < ' _ > ] ,
216225 ) -> Poll < Result < usize > > {
217226 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) ,
220229 }
221230 }
222231
223232 fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) > > {
224233 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) ,
227236 }
228237 }
229238
230239 fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) > > {
231240 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) ,
234243 }
235244 }
236245 }
@@ -246,8 +255,8 @@ mod if_std {
246255 pos : SeekFrom ,
247256 ) -> Poll < Result < u64 > > {
248257 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) ,
251260 }
252261 }
253262 }
@@ -259,15 +268,15 @@ mod if_std {
259268 {
260269 fn poll_fill_buf ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < & [ u8 ] > > {
261270 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) ,
264273 }
265274 }
266275
267276 fn consume ( self : Pin < & mut Self > , amt : usize ) {
268277 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) ,
271280 }
272281 }
273282 }
0 commit comments