File tree 3 files changed +33
-2
lines changed 3 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -239,6 +239,24 @@ impl<T> TrySendError<T> {
239
239
pub fn into_inner ( self ) -> T {
240
240
self . value
241
241
}
242
+
243
+ /// Did the send fail because the channel has been closed?
244
+ pub fn is_closed ( & self ) -> bool {
245
+ if let ErrorKind :: Closed = self . kind {
246
+ true
247
+ } else {
248
+ false
249
+ }
250
+ }
251
+
252
+ /// Did the send fail because the channel was at capacity?
253
+ pub fn is_full ( & self ) -> bool {
254
+ if let ErrorKind :: NoCapacity = self . kind {
255
+ true
256
+ } else {
257
+ false
258
+ }
259
+ }
242
260
}
243
261
244
262
impl < T : fmt:: Debug > fmt:: Display for TrySendError < T > {
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ impl<T> fmt::Debug for UnboundedReceiver<T> {
48
48
#[ derive( Debug ) ]
49
49
pub struct UnboundedSendError ( ( ) ) ;
50
50
51
- /// Error returned by `UnboundedSender::try_send`.
51
+ /// Returned by `UnboundedSender::try_send` when the channel has been closed .
52
52
#[ derive( Debug ) ]
53
53
pub struct UnboundedTrySendError < T > ( T ) ;
54
54
Original file line number Diff line number Diff line change @@ -255,7 +255,7 @@ fn try_send_fail() {
255
255
tx. try_send ( "hello" ) . unwrap ( ) ;
256
256
257
257
// This should fail
258
- assert ! ( tx. try_send( "fail" ) . is_err ( ) ) ;
258
+ assert ! ( tx. try_send( "fail" ) . unwrap_err ( ) . is_full ( ) ) ;
259
259
260
260
assert_eq ! ( rx. next( ) . unwrap( ) . unwrap( ) , "hello" ) ;
261
261
@@ -300,6 +300,19 @@ fn dropping_rx_closes_channel() {
300
300
assert_eq ! ( 1 , Arc :: strong_count( & msg) ) ;
301
301
}
302
302
303
+ #[ test]
304
+ fn dropping_rx_closes_channel_for_try ( ) {
305
+ let ( mut tx, rx) = mpsc:: channel ( 100 ) ;
306
+
307
+ let msg = Arc :: new ( ( ) ) ;
308
+ tx. try_send ( msg. clone ( ) ) . unwrap ( ) ;
309
+
310
+ drop ( rx) ;
311
+ assert ! ( tx. try_send( msg. clone( ) ) . unwrap_err( ) . is_closed( ) ) ;
312
+
313
+ assert_eq ! ( 1 , Arc :: strong_count( & msg) ) ;
314
+ }
315
+
303
316
#[ test]
304
317
fn unconsumed_messagers_are_dropped ( ) {
305
318
let msg = Arc :: new ( ( ) ) ;
You can’t perform that action at this time.
0 commit comments