@@ -290,6 +290,10 @@ pub(crate) fn write(fd: RawFd, buf: &[u8], timeout: Option<Duration>) -> Result<
290
290
}
291
291
}
292
292
293
+ /// Creates a new asynchronous channel, returning the sender/receiver halves.
294
+ ///
295
+ /// All data sent on the Sender will become available on the [Receiver] in the same order as it was sent,
296
+ /// and no `send` will block the calling fiber, `recv` will block until a message is available.
293
297
pub fn channel < T > ( capacity : usize ) -> ( Sender < T > , Receiver < T > ) {
294
298
let chan = Rc :: new ( Chan {
295
299
buffer : RefCell :: new ( VecDeque :: with_capacity ( capacity) ) ,
@@ -301,9 +305,14 @@ pub fn channel<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
301
305
( Sender ( chan. clone ( ) ) , Receiver ( chan) )
302
306
}
303
307
308
+ /// The sending-half of channel.
309
+ ///
310
+ /// Messages can be sent through this channel with `send`. Can be cloned.
304
311
pub struct Sender < T > ( Rc < Chan < T > > ) ;
305
312
306
313
impl < T > Sender < T > {
314
+ /// Attempts to send a value on this channel, returning it back if it could not be sent.
315
+ /// This method will never block.
307
316
pub fn send ( & self , value : T ) -> Result < ( ) , io:: Error > {
308
317
if !self . 0 . rx_is_active . get ( ) {
309
318
return Err ( io:: ErrorKind :: NotConnected . into ( ) ) ;
@@ -338,9 +347,11 @@ impl<T> Drop for Sender<T> {
338
347
}
339
348
}
340
349
350
+ /// The receiving half of channel.
341
351
pub struct Receiver < T > ( Rc < Chan < T > > ) ;
342
352
343
353
impl < T > Receiver < T > {
354
+ /// Attempts to wait for a value on this receiver, returning `None` if the corresponding channel has hung up.
344
355
pub fn recv ( & self ) -> Option < T > {
345
356
if self . 0 . buffer . borrow ( ) . len ( ) == 0 {
346
357
if self . 0 . tx_count . get ( ) . is_zero ( ) {
0 commit comments