1
- use crate :: { Sink , Poll } ;
1
+ use crate :: { Poll , Sink } ;
2
+ use futures_channel:: mpsc:: { SendError , Sender , TrySendError , UnboundedSender } ;
2
3
use futures_core:: task:: Context ;
3
- use futures_channel:: mpsc:: { Sender , SendError , TrySendError , UnboundedSender } ;
4
4
use std:: pin:: Pin ;
5
5
6
6
impl < T > Sink < T > for Sender < T > {
7
7
type SinkError = SendError ;
8
8
9
- fn poll_ready ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
9
+ fn poll_ready (
10
+ mut self : Pin < & mut Self > ,
11
+ cx : & mut Context < ' _ > ,
12
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
10
13
( * self ) . poll_ready ( cx)
11
14
}
12
15
13
- fn start_send ( mut self : Pin < & mut Self > , msg : T ) -> Result < ( ) , Self :: SinkError > {
16
+ fn start_send (
17
+ mut self : Pin < & mut Self > ,
18
+ msg : T ,
19
+ ) -> Result < ( ) , Self :: SinkError > {
14
20
( * self ) . start_send ( msg)
15
21
}
16
22
17
- fn poll_flush ( self : Pin < & mut Self > , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
18
- Poll :: Ready ( Ok ( ( ) ) )
23
+ fn poll_flush (
24
+ mut self : Pin < & mut Self > ,
25
+ cx : & mut Context < ' _ > ,
26
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
27
+ match ( * self ) . poll_ready ( cx) {
28
+ Poll :: Ready ( Err ( ref e) ) if e. is_disconnected ( ) => {
29
+ // If the receiver disconnected, we consider the sink to be flushed.
30
+ Poll :: Ready ( Ok ( ( ) ) )
31
+ }
32
+ x => x,
33
+ }
19
34
}
20
35
21
- fn poll_close ( mut self : Pin < & mut Self > , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
36
+ fn poll_close (
37
+ mut self : Pin < & mut Self > ,
38
+ _: & mut Context < ' _ > ,
39
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
22
40
self . disconnect ( ) ;
23
41
Poll :: Ready ( Ok ( ( ) ) )
24
42
}
@@ -27,19 +45,31 @@ impl<T> Sink<T> for Sender<T> {
27
45
impl < T > Sink < T > for UnboundedSender < T > {
28
46
type SinkError = SendError ;
29
47
30
- fn poll_ready ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
48
+ fn poll_ready (
49
+ self : Pin < & mut Self > ,
50
+ cx : & mut Context < ' _ > ,
51
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
31
52
UnboundedSender :: poll_ready ( & * self , cx)
32
53
}
33
54
34
- fn start_send ( mut self : Pin < & mut Self > , msg : T ) -> Result < ( ) , Self :: SinkError > {
55
+ fn start_send (
56
+ mut self : Pin < & mut Self > ,
57
+ msg : T ,
58
+ ) -> Result < ( ) , Self :: SinkError > {
35
59
UnboundedSender :: start_send ( & mut * self , msg)
36
60
}
37
61
38
- fn poll_flush ( self : Pin < & mut Self > , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
62
+ fn poll_flush (
63
+ self : Pin < & mut Self > ,
64
+ _: & mut Context < ' _ > ,
65
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
39
66
Poll :: Ready ( Ok ( ( ) ) )
40
67
}
41
68
42
- fn poll_close ( mut self : Pin < & mut Self > , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
69
+ fn poll_close (
70
+ mut self : Pin < & mut Self > ,
71
+ _: & mut Context < ' _ > ,
72
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
43
73
self . disconnect ( ) ;
44
74
Poll :: Ready ( Ok ( ( ) ) )
45
75
}
@@ -48,7 +78,10 @@ impl<T> Sink<T> for UnboundedSender<T> {
48
78
impl < T > Sink < T > for & UnboundedSender < T > {
49
79
type SinkError = SendError ;
50
80
51
- fn poll_ready ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
81
+ fn poll_ready (
82
+ self : Pin < & mut Self > ,
83
+ cx : & mut Context < ' _ > ,
84
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
52
85
UnboundedSender :: poll_ready ( * self , cx)
53
86
}
54
87
@@ -57,11 +90,17 @@ impl<T> Sink<T> for &UnboundedSender<T> {
57
90
. map_err ( TrySendError :: into_send_error)
58
91
}
59
92
60
- fn poll_flush ( self : Pin < & mut Self > , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
93
+ fn poll_flush (
94
+ self : Pin < & mut Self > ,
95
+ _: & mut Context < ' _ > ,
96
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
61
97
Poll :: Ready ( Ok ( ( ) ) )
62
98
}
63
99
64
- fn poll_close ( self : Pin < & mut Self > , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: SinkError > > {
100
+ fn poll_close (
101
+ self : Pin < & mut Self > ,
102
+ _: & mut Context < ' _ > ,
103
+ ) -> Poll < Result < ( ) , Self :: SinkError > > {
65
104
self . close_channel ( ) ;
66
105
Poll :: Ready ( Ok ( ( ) ) )
67
106
}
0 commit comments