@@ -129,60 +129,63 @@ pub struct Receiver<T> {
129
129
#[ derive( Debug ) ]
130
130
pub struct UnboundedReceiver < T > ( Receiver < T > ) ;
131
131
132
- /// Error type for sending, used when the receiving end of a channel is
133
- /// dropped
134
- #[ derive( Clone , PartialEq , Eq ) ]
135
- pub struct SendError < T > ( T ) ;
132
+ /// The error type of `<Sender<T> as Sink>`
133
+ ///
134
+ /// It will contain a value of type `T` if one was passed to `start_send`
135
+ /// after the channel was closed.
136
+ pub struct ChannelClosed < T > ( Option < T > ) ;
136
137
137
138
/// Error type returned from `try_send`
138
139
#[ derive( Clone , PartialEq , Eq ) ]
139
- pub struct TrySendError < T > {
140
- kind : TrySendErrorKind < T > ,
140
+ pub struct TryChannelClosed < T > {
141
+ kind : TryChannelClosedKind < T > ,
141
142
}
142
143
143
144
#[ derive( Clone , PartialEq , Eq ) ]
144
- enum TrySendErrorKind < T > {
145
+ enum TryChannelClosedKind < T > {
145
146
Full ( T ) ,
146
147
Disconnected ( T ) ,
147
148
}
148
149
149
- impl < T > fmt:: Debug for SendError < T > {
150
+ impl < T > fmt:: Debug for ChannelClosed < T > {
150
151
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
151
- fmt. debug_tuple ( "SendError " )
152
+ fmt. debug_tuple ( "ChannelClosed " )
152
153
. field ( & "..." )
153
154
. finish ( )
154
155
}
155
156
}
156
157
157
- impl < T > fmt:: Display for SendError < T > {
158
+ impl < T > fmt:: Display for ChannelClosed < T > {
158
159
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
159
160
write ! ( fmt, "send failed because receiver is gone" )
160
161
}
161
162
}
162
163
163
- impl < T : Any > Error for SendError < T >
164
+ impl < T : Any > Error for ChannelClosed < T >
164
165
{
165
166
fn description ( & self ) -> & str {
166
167
"send failed because receiver is gone"
167
168
}
168
169
}
169
170
170
- impl < T > SendError < T > {
171
+ impl < T > ChannelClosed < T > {
171
172
/// Returns the message that was attempted to be sent but failed.
172
- pub fn into_inner ( self ) -> T {
173
+ /// This method returns `None` if no item was being sent when the
174
+ /// error occurred.
175
+ pub fn into_inner ( self ) -> Option < T > {
173
176
self . 0
174
177
}
175
178
}
176
179
177
- impl < T > fmt:: Debug for TrySendError < T > {
180
+ impl < T > fmt:: Debug for TryChannelClosed < T > {
178
181
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
179
- fmt. debug_tuple ( "TrySendError " )
182
+ fmt. debug_tuple ( "TryChannelClosed " )
180
183
. field ( & "..." )
181
184
. finish ( )
182
185
}
183
186
}
184
187
185
- impl < T > fmt:: Display for TrySendError < T > {
188
+ impl < T > fmt:: Display for TryChannelClosed < T > {
186
189
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
187
190
if self . is_full ( ) {
188
191
write ! ( fmt, "send failed because channel is full" )
@@ -192,7 +195,7 @@ impl<T> fmt::Display for TrySendError<T> {
192
195
}
193
196
}
194
197
195
- impl < T : Any > Error for TrySendError < T > {
198
+ impl < T : Any > Error for TryChannelClosed < T > {
196
199
fn description ( & self ) -> & str {
197
200
if self . is_full ( ) {
198
201
"send failed because channel is full"
@@ -202,10 +205,10 @@ impl<T: Any> Error for TrySendError<T> {
202
205
}
203
206
}
204
207
205
- impl < T > TrySendError < T > {
208
+ impl < T > TryChannelClosed < T > {
206
209
/// Returns true if this error is a result of the channel being full
207
210
pub fn is_full ( & self ) -> bool {
208
- use self :: TrySendErrorKind :: * ;
211
+ use self :: TryChannelClosedKind :: * ;
209
212
210
213
match self . kind {
211
214
Full ( _) => true ,
@@ -215,7 +218,7 @@ impl<T> TrySendError<T> {
215
218
216
219
/// Returns true if this error is a result of the receiver being dropped
217
220
pub fn is_disconnected ( & self ) -> bool {
218
- use self :: TrySendErrorKind :: * ;
221
+ use self :: TryChannelClosedKind :: * ;
219
222
220
223
match self . kind {
221
224
Disconnected ( _) => true ,
@@ -225,7 +228,7 @@ impl<T> TrySendError<T> {
225
228
226
229
/// Returns the message that was attempted to be sent but failed.
227
230
pub fn into_inner ( self ) -> T {
228
- use self :: TrySendErrorKind :: * ;
231
+ use self :: TryChannelClosedKind :: * ;
229
232
230
233
match self . kind {
231
234
Full ( v) | Disconnected ( v) => v,
@@ -395,47 +398,34 @@ impl<T> Sender<T> {
395
398
/// It is not recommended to call this function from inside of a future,
396
399
/// only from an external thread where you've otherwise arranged to be
397
400
/// notified when the channel is no longer full.
398
- pub fn try_send ( & mut self , msg : T ) -> Result < ( ) , TrySendError < T > > {
401
+ pub fn try_send ( & mut self , msg : T ) -> Result < ( ) , TryChannelClosed < T > > {
399
402
// If the sender is currently blocked, reject the message
400
403
if !self . poll_unparked ( None ) . is_ready ( ) {
401
- return Err ( TrySendError {
402
- kind : TrySendErrorKind :: Full ( msg) ,
404
+ return Err ( TryChannelClosed {
405
+ kind : TryChannelClosedKind :: Full ( msg) ,
403
406
} ) ;
404
407
}
405
408
406
409
// The channel has capacity to accept the message, so send it
407
410
self . do_send ( Some ( msg) , None )
408
- . map_err ( |SendError ( v) | {
409
- TrySendError {
410
- kind : TrySendErrorKind :: Disconnected ( v) ,
411
+ . map_err ( |ChannelClosed ( v) | {
412
+ TryChannelClosed {
413
+ kind : TryChannelClosedKind :: Disconnected ( v. unwrap ( ) ) ,
411
414
}
412
415
} )
413
416
}
414
417
415
418
/// Attempt to start sending a message on the channel.
416
- ///
417
- /// If there is not room on the channel, this function will return
418
- /// `Ok(Err(msg))`, and the current `Task` will be
419
- /// awoken when the channel is ready to receive more messages.
420
- ///
421
- /// On successful completion, this function will return `Ok(Ok(()))`.
422
- pub fn start_send ( & mut self , cx : & mut task:: Context , msg : T ) -> Result < Result < ( ) , T > , SendError < T > > {
423
- // If the sender is currently blocked, reject the message before doing
424
- // any work.
425
- if !self . poll_unparked ( Some ( cx) ) . is_ready ( ) {
426
- return Ok ( Err ( msg) ) ;
427
- }
428
-
429
- // The channel has capacity to accept the message, so send it.
430
- self . do_send ( Some ( msg) , Some ( cx) ) ?;
431
-
432
- Ok ( Ok ( ( ) ) )
419
+ /// This function should only be called after `poll_ready` has responded
420
+ /// that the channel is ready to receive a message.
421
+ pub fn start_send ( & mut self , msg : T ) -> Result < ( ) , ChannelClosed < T > > {
422
+ self . do_send ( Some ( msg) , None )
433
423
}
434
424
435
425
// Do the send without failing
436
426
// None means close
437
427
fn do_send ( & mut self , msg : Option < T > , cx : Option < & mut task:: Context > )
438
- -> Result < ( ) , SendError < T > >
428
+ -> Result < ( ) , ChannelClosed < T > >
439
429
{
440
430
// First, increment the number of messages contained by the channel.
441
431
// This operation will also atomically determine if the sender task
@@ -456,7 +446,7 @@ impl<T> Sender<T> {
456
446
// num-senders + buffer + 1
457
447
//
458
448
if let Some ( msg) = msg {
459
- return Err ( SendError ( msg) ) ;
449
+ return Err ( ChannelClosed ( Some ( msg) ) ) ;
460
450
} else {
461
451
return Ok ( ( ) ) ;
462
452
}
@@ -482,10 +472,10 @@ impl<T> Sender<T> {
482
472
// Do the send without parking current task.
483
473
//
484
474
// To be called from unbounded sender.
485
- fn do_send_nb ( & self , msg : T ) -> Result < ( ) , SendError < T > > {
475
+ fn do_send_nb ( & self , msg : T ) -> Result < ( ) , ChannelClosed < T > > {
486
476
match self . inc_num_messages ( false ) {
487
477
Some ( park_self) => assert ! ( !park_self) ,
488
- None => return Err ( SendError ( msg) ) ,
478
+ None => return Err ( ChannelClosed ( Some ( msg) ) ) ,
489
479
} ;
490
480
491
481
self . queue_push_and_signal ( Some ( msg) ) ;
@@ -601,15 +591,15 @@ impl<T> Sender<T> {
601
591
///
602
592
/// Returns `Ok(Async::Ready(_))` if there is sufficient capacity, or returns
603
593
/// `Ok(Async::Pending)` if the channel is not guaranteed to have capacity. Returns
604
- /// `Err(SendError (_))` if the receiver has been dropped.
594
+ /// `Err(ChannelClosed (_))` if the receiver has been dropped.
605
595
///
606
596
/// # Panics
607
597
///
608
598
/// This method will panic if called from outside the context of a task or future.
609
- pub fn poll_ready ( & mut self , cx : & mut task:: Context ) -> Poll < ( ) , SendError < ( ) > > {
599
+ pub fn poll_ready ( & mut self , cx : & mut task:: Context ) -> Poll < ( ) , ChannelClosed < T > > {
610
600
let state = decode_state ( self . inner . state . load ( SeqCst ) ) ;
611
601
if !state. is_open {
612
- return Err ( SendError ( ( ) ) ) ;
602
+ return Err ( ChannelClosed ( None ) ) ;
613
603
}
614
604
615
605
Ok ( self . poll_unparked ( Some ( cx) ) )
@@ -643,24 +633,24 @@ impl<T> Sender<T> {
643
633
}
644
634
645
635
impl < T > UnboundedSender < T > {
636
+ /// Check if the channel is ready to receive a message.
637
+ pub fn poll_ready ( & mut self , cx : & mut task:: Context ) -> Poll < ( ) , ChannelClosed < T > > {
638
+ self . 0 . poll_ready ( cx)
639
+ }
640
+
646
641
/// Attempt to start sending a message on the channel.
647
- ///
648
- /// If there is not room on the channel, this function will return
649
- /// `Ok(Err(msg))`, and the current `Task` will be
650
- /// awoken when the channel is ready to receive more messages.
651
- ///
652
- /// On a successful `start_send`, this function will return
653
- /// `Ok(Ok(())`.
654
- pub fn start_send ( & mut self , cx : & mut task:: Context , msg : T ) -> Result < Result < ( ) , T > , SendError < T > > {
655
- self . 0 . start_send ( cx, msg)
642
+ /// This function should only be called after `poll_ready` has been used to
643
+ /// verify that the channel is ready to receive a message.
644
+ pub fn start_send ( & mut self , msg : T ) -> Result < ( ) , ChannelClosed < T > > {
645
+ self . 0 . start_send ( msg)
656
646
}
657
647
658
648
/// Sends the provided message along this channel.
659
649
///
660
650
/// This is an unbounded sender, so this function differs from `Sink::send`
661
651
/// by ensuring the return type reflects that the channel is always ready to
662
652
/// receive messages.
663
- pub fn unbounded_send ( & self , msg : T ) -> Result < ( ) , SendError < T > > {
653
+ pub fn unbounded_send ( & self , msg : T ) -> Result < ( ) , ChannelClosed < T > > {
664
654
self . 0 . do_send_nb ( msg)
665
655
}
666
656
}
0 commit comments