11pub use tokio:: sync:: oneshot;
22
33pub mod mpsc {
4- use std:: {
5- sync:: { Arc , Weak } ,
6- task:: { Context , Poll } ,
7- } ;
8- use tokio:: sync:: mpsc:: error:: * ;
9- pub use tokio:: sync:: mpsc:: { self , * } ;
4+ use std:: sync:: { Arc , Weak } ;
105
11- pub struct UnboundedSender < T > ( mpsc:: UnboundedSender < T > , Arc < ( ) > ) ;
12- pub struct UnboundedReceiver < T > ( mpsc:: UnboundedReceiver < T > ) ;
6+ pub use flume:: { SendError , TryRecvError , TrySendError } ;
7+
8+ pub type RecvStream < T > = flume:: r#async:: RecvStream < ' static , T > ;
9+
10+ pub struct Sender < T > ( flume:: Sender < T > ) ;
11+ pub struct Receiver < T > ( flume:: Receiver < T > ) ;
12+
13+ pub struct UnboundedSender < T > ( flume:: Sender < T > , Arc < ( ) > ) ;
14+ pub struct UnboundedReceiver < T > ( flume:: Receiver < T > ) ;
1315
1416 pub type TrackedUnboundedSender < T > = UnboundedSender < Tracked < T > > ;
1517 pub type TrackedUnboundedReceiver < T > = UnboundedReceiver < Tracked < T > > ;
@@ -31,6 +33,12 @@ pub mod mpsc {
3133 }
3234 }
3335
36+ impl < T > Clone for Sender < T > {
37+ fn clone ( & self ) -> Self {
38+ Self ( self . 0 . clone ( ) )
39+ }
40+ }
41+
3442 impl < T > Clone for UnboundedSender < T > {
3543 fn clone ( & self ) -> Self {
3644 Self ( self . 0 . clone ( ) , self . 1 . clone ( ) )
@@ -51,6 +59,34 @@ pub mod mpsc {
5159 }
5260 }
5361
62+ impl < T > Sender < T > {
63+ pub async fn send ( & self , message : T ) -> Result < ( ) , SendError < T > > {
64+ self . 0 . send_async ( message) . await
65+ }
66+
67+ pub fn try_send ( & self , message : T ) -> Result < ( ) , TrySendError < T > > {
68+ self . 0 . try_send ( message)
69+ }
70+ }
71+
72+ impl < T > Receiver < T > {
73+ pub fn is_empty ( & self ) -> bool {
74+ self . 0 . is_empty ( )
75+ }
76+
77+ pub fn len ( & self ) -> usize {
78+ self . 0 . len ( )
79+ }
80+
81+ pub async fn recv ( & mut self ) -> Option < T > {
82+ self . 0 . recv_async ( ) . await . ok ( )
83+ }
84+
85+ pub fn try_recv ( & mut self ) -> Result < T , TryRecvError > {
86+ self . 0 . try_recv ( )
87+ }
88+ }
89+
5490 impl < T > UnboundedSender < T > {
5591 pub fn is_empty ( & self ) -> bool {
5692 self . len ( ) == 0
@@ -82,31 +118,37 @@ pub mod mpsc {
82118 }
83119
84120 pub async fn recv ( & mut self ) -> Option < T > {
85- self . 0 . recv ( ) . await
121+ self . 0 . recv_async ( ) . await . ok ( )
86122 }
87123
88124 pub fn try_recv ( & mut self ) -> Result < T , TryRecvError > {
89125 self . 0 . try_recv ( )
90126 }
91127
92- pub fn poll_recv ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Option < T > > {
93- self . 0 . poll_recv ( cx )
128+ pub fn stream ( & self ) -> RecvStream < T > {
129+ self . 0 . clone ( ) . into_stream ( )
94130 }
95131
96132 pub fn blocking_recv ( & mut self ) -> Option < T > {
97- self . 0 . blocking_recv ( )
133+ self . 0 . recv ( ) . ok ( )
98134 }
99135 }
100136
137+ pub fn channel < T > ( bound : usize ) -> ( Sender < T > , Receiver < T > ) {
138+ let ( tx, rx) = flume:: bounded ( bound) ;
139+
140+ ( Sender ( tx) , Receiver ( rx) )
141+ }
142+
101143 pub fn unbounded_channel < T > ( ) -> ( UnboundedSender < T > , UnboundedReceiver < T > ) {
102- let ( tx, rx) = mpsc :: unbounded_channel ( ) ;
144+ let ( tx, rx) = flume :: unbounded ( ) ;
103145
104146 ( UnboundedSender ( tx, Arc :: new ( ( ) ) ) , UnboundedReceiver ( rx) )
105147 }
106148
107149 pub fn tracked_unbounded_channel < T > (
108150 ) -> ( UnboundedSender < Tracked < T > > , UnboundedReceiver < Tracked < T > > ) {
109- let ( tx, rx) = mpsc :: unbounded_channel ( ) ;
151+ let ( tx, rx) = flume :: unbounded ( ) ;
110152
111153 ( UnboundedSender ( tx, Arc :: new ( ( ) ) ) , UnboundedReceiver ( rx) )
112154 }
@@ -133,21 +175,21 @@ pub mod watch {
133175}
134176
135177#[ allow( dead_code) ]
136- pub struct Aborter ( mpsc :: Receiver < ( ) > , mpsc :: Sender < ( ) > ) ;
178+ pub struct Aborter ( flume :: Receiver < ( ) > , flume :: Sender < ( ) > ) ;
137179
138180#[ derive( Clone ) ]
139- pub struct Aborted ( mpsc :: Sender < ( ) > ) ;
181+ pub struct Aborted ( flume :: Sender < ( ) > ) ;
140182
141183impl Default for Aborter {
142184 fn default ( ) -> Self {
143- let ( tx, rx) = mpsc :: channel ( 1 ) ;
185+ let ( tx, rx) = flume :: bounded ( 0 ) ;
144186 Self ( rx, tx)
145187 }
146188}
147189
148190impl Aborter {
149191 pub fn listener_count ( & self ) -> usize {
150- self . 1 . strong_count ( ) . saturating_sub ( 1 )
192+ self . 0 . sender_count ( ) . saturating_sub ( 1 )
151193 }
152194
153195 /// Simply drops the object. No need to call manually, unless you
@@ -163,6 +205,7 @@ impl Aborter {
163205
164206impl Aborted {
165207 pub async fn wait ( & self ) {
166- self . 0 . closed ( ) . await ;
208+ // it returning an error means receiver was dropped
209+ while self . 0 . send_async ( ( ) ) . await . is_ok ( ) { }
167210 }
168211}
0 commit comments