1
1
use super :: Compat ;
2
- use futures:: Future as Future01 ;
3
- use futures:: Poll as Poll01 ;
4
- use futures:: task as task01;
5
- use futures:: Async as Async01 ;
6
- use futures_core:: TryFuture as TryFuture03 ;
7
- use futures_core:: task as task03;
8
- use std:: marker:: Unpin ;
9
- use std:: mem:: PinMut ;
10
- use std:: sync:: Arc ;
2
+ use futures:: {
3
+ task as task01, Async as Async01 , AsyncSink as AsyncSink01 ,
4
+ Future as Future01 , Poll as Poll01 , Sink as Sink01 ,
5
+ StartSend as StartSend01 , Stream as Stream01 ,
6
+ } ;
7
+ use futures_core:: {
8
+ task as task03, TryFuture as TryFuture03 , TryStream as TryStream03 ,
9
+ } ;
10
+ use futures_sink:: Sink as Sink03 ;
11
+ use std:: { marker:: Unpin , mem:: PinMut , sync:: Arc } ;
11
12
12
13
impl < Fut , Ex > Future01 for Compat < Fut , Ex >
13
- where Fut : TryFuture03 + Unpin ,
14
- Ex : task03:: Executor
14
+ where
15
+ Fut : TryFuture03 + Unpin ,
16
+ Ex : task03:: Executor ,
15
17
{
16
18
type Item = Fut :: Ok ;
17
19
type Error = Fut :: Error ;
18
20
19
21
fn poll ( & mut self ) -> Poll01 < Self :: Item , Self :: Error > {
20
- let waker = current_as_waker ( ) ;
21
- let mut cx = task03:: Context :: new ( & waker, self . executor . as_mut ( ) . unwrap ( ) ) ;
22
- match PinMut :: new ( & mut self . future ) . try_poll ( & mut cx) {
22
+ with_context ( self , |inner, cx| match inner. try_poll ( cx) {
23
23
task03:: Poll :: Ready ( Ok ( t) ) => Ok ( Async01 :: Ready ( t) ) ,
24
24
task03:: Poll :: Pending => Ok ( Async01 :: NotReady ) ,
25
25
task03:: Poll :: Ready ( Err ( e) ) => Err ( e) ,
26
- }
26
+ } )
27
+ }
28
+ }
29
+
30
+ impl < St , Ex > Stream01 for Compat < St , Ex >
31
+ where
32
+ St : TryStream03 + Unpin ,
33
+ Ex : task03:: Executor ,
34
+ {
35
+ type Item = St :: Ok ;
36
+ type Error = St :: Error ;
37
+
38
+ fn poll ( & mut self ) -> Poll01 < Option < Self :: Item > , Self :: Error > {
39
+ with_context ( self , |inner, cx| match inner. try_poll_next ( cx) {
40
+ task03:: Poll :: Ready ( None ) => Ok ( Async01 :: Ready ( None ) ) ,
41
+ task03:: Poll :: Ready ( Some ( Ok ( t) ) ) => Ok ( Async01 :: Ready ( Some ( t) ) ) ,
42
+ task03:: Poll :: Pending => Ok ( Async01 :: NotReady ) ,
43
+ task03:: Poll :: Ready ( Some ( Err ( e) ) ) => Err ( e) ,
44
+ } )
45
+ }
46
+ }
47
+
48
+ impl < T , E > Sink01 for Compat < T , E >
49
+ where
50
+ T : Sink03 + Unpin ,
51
+ E : task03:: Executor ,
52
+ {
53
+ type SinkItem = T :: SinkItem ;
54
+ type SinkError = T :: SinkError ;
55
+
56
+ fn start_send (
57
+ & mut self ,
58
+ item : Self :: SinkItem ,
59
+ ) -> StartSend01 < Self :: SinkItem , Self :: SinkError > {
60
+ with_context ( self , |mut inner, cx| {
61
+ match inner. reborrow ( ) . poll_ready ( cx) {
62
+ task03:: Poll :: Ready ( Ok ( ( ) ) ) => {
63
+ inner. start_send ( item) . map ( |( ) | AsyncSink01 :: Ready )
64
+ }
65
+ task03:: Poll :: Pending => Ok ( AsyncSink01 :: NotReady ( item) ) ,
66
+ task03:: Poll :: Ready ( Err ( e) ) => Err ( e) ,
67
+ }
68
+ } )
69
+ }
70
+
71
+ fn poll_complete ( & mut self ) -> Poll01 < ( ) , Self :: SinkError > {
72
+ with_context ( self , |inner, cx| match inner. poll_flush ( cx) {
73
+ task03:: Poll :: Ready ( Ok ( ( ) ) ) => Ok ( Async01 :: Ready ( ( ) ) ) ,
74
+ task03:: Poll :: Pending => Ok ( Async01 :: NotReady ) ,
75
+ task03:: Poll :: Ready ( Err ( e) ) => Err ( e) ,
76
+ } )
77
+ }
78
+
79
+ fn close ( & mut self ) -> Poll01 < ( ) , Self :: SinkError > {
80
+ with_context ( self , |inner, cx| match inner. poll_close ( cx) {
81
+ task03:: Poll :: Ready ( Ok ( ( ) ) ) => Ok ( Async01 :: Ready ( ( ) ) ) ,
82
+ task03:: Poll :: Pending => Ok ( Async01 :: NotReady ) ,
83
+ task03:: Poll :: Ready ( Err ( e) ) => Err ( e) ,
84
+ } )
27
85
}
28
86
}
29
87
@@ -39,3 +97,15 @@ impl task03::Wake for Current {
39
97
arc_self. 0 . notify ( ) ;
40
98
}
41
99
}
100
+
101
+ fn with_context < T , E , R , F > ( compat : & mut Compat < T , E > , f : F ) -> R
102
+ where
103
+ T : Unpin ,
104
+ E : task03:: Executor ,
105
+ F : FnOnce ( PinMut < T > , & mut task03:: Context ) -> R ,
106
+ {
107
+ let waker = current_as_waker ( ) ;
108
+ let executor = compat. executor . as_mut ( ) . unwrap ( ) ;
109
+ let mut cx = task03:: Context :: new ( & waker, executor) ;
110
+ f ( PinMut :: new ( & mut compat. inner ) , & mut cx)
111
+ }
0 commit comments