@@ -298,7 +298,28 @@ mod mpsc_queue;
298
298
mod spsc_queue;
299
299
300
300
/// The receiving-half of Rust's channel type. This half can only be owned by
301
- /// one thread
301
+ /// one thread.
302
+ ///
303
+ /// Messages sent to the channel can be retrieved using [`recv`].
304
+ ///
305
+ /// [`recv`]: ../../../std/sync/mpsc/struct.Receiver.html#method.recv
306
+ ///
307
+ /// # Examples
308
+ ///
309
+ /// ```rust
310
+ /// use std::sync::mpsc::channel;
311
+ /// use std::thread;
312
+ /// use std::time::Duration;
313
+ /// let (send, recv) = channel();
314
+ /// thread::spawn(move || {
315
+ /// send.send("Hello world!");
316
+ /// thread::sleep(Duration::from_secs(2)); // block for two seconds
317
+ /// send.send("Delayed for 2 seconds");
318
+ /// });
319
+ /// println!("{:?}", recv.recv()); // Received immediately
320
+ /// println!("Waiting...");
321
+ /// println!("{:?}", recv.recv()); // Received after 2 seconds
322
+ /// ```
302
323
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
303
324
pub struct Receiver < T > {
304
325
inner : UnsafeCell < Flavor < T > > ,
@@ -312,30 +333,39 @@ unsafe impl<T: Send> Send for Receiver<T> { }
312
333
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
313
334
impl < T > !Sync for Receiver < T > { }
314
335
315
- /// An iterator over messages on a receiver, this iterator will block
316
- /// whenever `next` is called, waiting for a new message, and `None` will be
317
- /// returned when the corresponding channel has hung up.
336
+ /// An iterator over messages on a receiver, this iterator will block whenever
337
+ /// [`next`] is called, waiting for a new message, and [`None`] will be returned
338
+ /// when the corresponding channel has hung up.
339
+ ///
340
+ /// [`next`]: ../../../std/iter/trait.Iterator.html#method.next
341
+ /// [`None`]: ../../../std/option/enum.Option.html#variant.None
318
342
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
319
343
#[ derive( Debug ) ]
320
344
pub struct Iter < ' a , T : ' a > {
321
345
rx : & ' a Receiver < T >
322
346
}
323
347
324
348
/// An iterator that attempts to yield all pending values for a receiver.
325
- /// `None` will be returned when there are no pending values remaining or
326
- /// if the corresponding channel has hung up.
349
+ /// [ `None`] will be returned when there are no pending values remaining or if
350
+ /// the corresponding channel has hung up.
327
351
///
328
352
/// This Iterator will never block the caller in order to wait for data to
329
- /// become available. Instead, it will return `None`.
353
+ /// become available. Instead, it will return [`None`].
354
+ ///
355
+ /// [`None`]: ../../../std/option/enum.Option.html#variant.None
330
356
#[ stable( feature = "receiver_try_iter" , since = "1.15.0" ) ]
331
357
#[ derive( Debug ) ]
332
358
pub struct TryIter < ' a , T : ' a > {
333
359
rx : & ' a Receiver < T >
334
360
}
335
361
336
362
/// An owning iterator over messages on a receiver, this iterator will block
337
- /// whenever `next` is called, waiting for a new message, and `None` will be
363
+ /// whenever [ `next`] is called, waiting for a new message, and [ `None`] will be
338
364
/// returned when the corresponding channel has hung up.
365
+ ///
366
+ /// [`next`]: ../../../std/iter/trait.Iterator.html#method.next
367
+ /// [`None`]: ../../../std/option/enum.Option.html#variant.None
368
+ ///
339
369
#[ stable( feature = "receiver_into_iter" , since = "1.1.0" ) ]
340
370
#[ derive( Debug ) ]
341
371
pub struct IntoIter < T > {
@@ -344,6 +374,30 @@ pub struct IntoIter<T> {
344
374
345
375
/// The sending-half of Rust's asynchronous channel type. This half can only be
346
376
/// owned by one thread, but it can be cloned to send to other threads.
377
+ ///
378
+ /// Messages can be sent through this channel with [`send`].
379
+ ///
380
+ /// [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
381
+ ///
382
+ /// # Examples
383
+ ///
384
+ /// ```rust
385
+ /// use std::sync::mpsc::channel;
386
+ /// use std::thread;
387
+ /// let (sender, receiver) = channel();
388
+ /// let sender2 = sender.clone();
389
+ /// // First thread owns sender
390
+ /// thread::spawn(move || {
391
+ /// sender.send(1);
392
+ /// });
393
+ /// // Second thread owns sender2
394
+ /// thread::spawn(move || {
395
+ /// sender2.send(2);
396
+ /// });
397
+ /// let msg = receiver.recv().unwrap();
398
+ /// let msg2 = receiver.recv().unwrap();
399
+ /// assert_eq!(3, msg + msg2);
400
+ /// ```
347
401
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
348
402
pub struct Sender < T > {
349
403
inner : UnsafeCell < Flavor < T > > ,
@@ -359,6 +413,10 @@ impl<T> !Sync for Sender<T> { }
359
413
360
414
/// The sending-half of Rust's synchronous channel type. This half can only be
361
415
/// owned by one thread, but it can be cloned to send to other threads.
416
+ ///
417
+ /// [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
418
+ /// [`SyncSender::send`]: ../../../std/sync/mpsc/struct.SyncSender.html#method.send
419
+ ///
362
420
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
363
421
pub struct SyncSender < T > {
364
422
inner : Arc < sync:: Packet < T > > ,
@@ -370,25 +428,32 @@ unsafe impl<T: Send> Send for SyncSender<T> {}
370
428
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
371
429
impl < T > !Sync for SyncSender < T > { }
372
430
373
- /// An error returned from the `send` function on channels.
431
+ /// An error returned from the [ `send`] function on channels.
374
432
///
375
- /// A `send` operation can only fail if the receiving end of a channel is
433
+ /// A [ `send`] operation can only fail if the receiving end of a channel is
376
434
/// disconnected, implying that the data could never be received. The error
377
435
/// contains the data being sent as a payload so it can be recovered.
436
+ ///
437
+ /// [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
378
438
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
379
439
#[ derive( PartialEq , Eq , Clone , Copy ) ]
380
440
pub struct SendError < T > ( #[ stable( feature = "rust1" , since = "1.0.0" ) ] pub T ) ;
381
441
382
- /// An error returned from the `recv` function on a `Receiver`.
442
+ /// An error returned from the [ `recv`] function on a [ `Receiver`] .
383
443
///
384
- /// The `recv` operation can only fail if the sending half of a channel is
444
+ /// The [ `recv`] operation can only fail if the sending half of a channel is
385
445
/// disconnected, implying that no further messages will ever be received.
446
+ ///
447
+ /// [`recv`]: ../../../std/sync/mpsc/struct.Receiver.html#method.recv
448
+ /// [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
386
449
#[ derive( PartialEq , Eq , Clone , Copy , Debug ) ]
387
450
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
388
451
pub struct RecvError ;
389
452
390
- /// This enumeration is the list of the possible reasons that `try_recv` could
453
+ /// This enumeration is the list of the possible reasons that [ `try_recv`] could
391
454
/// not return data when called.
455
+ ///
456
+ /// [`try_recv`]: ../../../std/sync/mpsc/struct.Receiver.html#method.try_recv
392
457
#[ derive( PartialEq , Eq , Clone , Copy , Debug ) ]
393
458
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
394
459
pub enum TryRecvError {
@@ -403,8 +468,10 @@ pub enum TryRecvError {
403
468
Disconnected ,
404
469
}
405
470
406
- /// This enumeration is the list of possible errors that `recv_timeout` could
471
+ /// This enumeration is the list of possible errors that [ `recv_timeout`] could
407
472
/// not return data when called.
473
+ ///
474
+ /// [`recv_timeout`]: ../../../std/sync/mpsc/struct.Receiver.html#method.recv_timeout
408
475
#[ derive( PartialEq , Eq , Clone , Copy , Debug ) ]
409
476
#[ stable( feature = "mpsc_recv_timeout" , since = "1.12.0" ) ]
410
477
pub enum RecvTimeoutError {
@@ -419,7 +486,9 @@ pub enum RecvTimeoutError {
419
486
}
420
487
421
488
/// This enumeration is the list of the possible error outcomes for the
422
- /// `SyncSender::try_send` method.
489
+ /// [`SyncSender::try_send`] method.
490
+ ///
491
+ /// [`SyncSender::try_send`]: ../../../std/sync/mpsc/struct.SyncSender.html#method.try_send
423
492
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
424
493
#[ derive( PartialEq , Eq , Clone , Copy ) ]
425
494
pub enum TrySendError < T > {
@@ -566,10 +635,13 @@ impl<T> Sender<T> {
566
635
/// A successful send occurs when it is determined that the other end of
567
636
/// the channel has not hung up already. An unsuccessful send would be one
568
637
/// where the corresponding receiver has already been deallocated. Note
569
- /// that a return value of `Err` means that the data will never be
570
- /// received, but a return value of `Ok` does *not* mean that the data
638
+ /// that a return value of [ `Err`] means that the data will never be
639
+ /// received, but a return value of [ `Ok`] does *not* mean that the data
571
640
/// will be received. It is possible for the corresponding receiver to
572
- /// hang up immediately after this function returns `Ok`.
641
+ /// hang up immediately after this function returns [`Ok`].
642
+ ///
643
+ /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
644
+ /// [`Ok`]: ../../../std/result/enum.Result.html#variant.Ok
573
645
///
574
646
/// This method will never block the current thread.
575
647
///
@@ -712,23 +784,29 @@ impl<T> SyncSender<T> {
712
784
/// time. If the buffer size is 0, however, it can be guaranteed that the
713
785
/// receiver has indeed received the data if this function returns success.
714
786
///
715
- /// This function will never panic, but it may return `Err` if the
716
- /// `Receiver` has disconnected and is no longer able to receive
787
+ /// This function will never panic, but it may return [ `Err`] if the
788
+ /// [ `Receiver`] has disconnected and is no longer able to receive
717
789
/// information.
790
+ ///
791
+ /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
792
+ /// [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
718
793
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
719
794
pub fn send ( & self , t : T ) -> Result < ( ) , SendError < T > > {
720
795
self . inner . send ( t) . map_err ( SendError )
721
796
}
722
797
723
798
/// Attempts to send a value on this channel without blocking.
724
799
///
725
- /// This method differs from `send` by returning immediately if the
800
+ /// This method differs from [ `send`] by returning immediately if the
726
801
/// channel's buffer is full or no receiver is waiting to acquire some
727
- /// data. Compared with `send`, this function has two failure cases
802
+ /// data. Compared with [ `send`] , this function has two failure cases
728
803
/// instead of one (one for disconnection, one for a full buffer).
729
804
///
730
- /// See `SyncSender::send` for notes about guarantees of whether the
805
+ /// See [ `SyncSender::send`] for notes about guarantees of whether the
731
806
/// receiver has received the data or not if this function is successful.
807
+ ///
808
+ /// [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
809
+ /// [`SyncSender::send`]: ../../../std/sync/mpsc/struct.SyncSender.html#method.send
732
810
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
733
811
pub fn try_send ( & self , t : T ) -> Result < ( ) , TrySendError < T > > {
734
812
self . inner . try_send ( t)
@@ -829,15 +907,18 @@ impl<T> Receiver<T> {
829
907
///
830
908
/// This function will always block the current thread if there is no data
831
909
/// available and it's possible for more data to be sent. Once a message is
832
- /// sent to the corresponding `Sender`, then this receiver will wake up and
910
+ /// sent to the corresponding [ `Sender`] , then this receiver will wake up and
833
911
/// return that message.
834
912
///
835
- /// If the corresponding `Sender` has disconnected, or it disconnects while
836
- /// this call is blocking, this call will wake up and return `Err` to
913
+ /// If the corresponding [ `Sender`] has disconnected, or it disconnects while
914
+ /// this call is blocking, this call will wake up and return [ `Err`] to
837
915
/// indicate that no more messages can ever be received on this channel.
838
916
/// However, since channels are buffered, messages sent before the disconnect
839
917
/// will still be properly received.
840
918
///
919
+ /// [`Sender`]: ../../../std/sync/mpsc/struct.Sender.html
920
+ /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
921
+ ///
841
922
/// # Examples
842
923
///
843
924
/// ```
@@ -917,15 +998,18 @@ impl<T> Receiver<T> {
917
998
///
918
999
/// This function will always block the current thread if there is no data
919
1000
/// available and it's possible for more data to be sent. Once a message is
920
- /// sent to the corresponding `Sender`, then this receiver will wake up and
1001
+ /// sent to the corresponding [ `Sender`] , then this receiver will wake up and
921
1002
/// return that message.
922
1003
///
923
- /// If the corresponding `Sender` has disconnected, or it disconnects while
924
- /// this call is blocking, this call will wake up and return `Err` to
1004
+ /// If the corresponding [ `Sender`] has disconnected, or it disconnects while
1005
+ /// this call is blocking, this call will wake up and return [ `Err`] to
925
1006
/// indicate that no more messages can ever be received on this channel.
926
1007
/// However, since channels are buffered, messages sent before the disconnect
927
1008
/// will still be properly received.
928
1009
///
1010
+ /// [`Sender`]: ../../../std/sync/mpsc/struct.Sender.html
1011
+ /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
1012
+ ///
929
1013
/// # Examples
930
1014
///
931
1015
/// ```no_run
@@ -1003,16 +1087,37 @@ impl<T> Receiver<T> {
1003
1087
}
1004
1088
1005
1089
/// Returns an iterator that will block waiting for messages, but never
1006
- /// `panic!`. It will return `None` when the channel has hung up.
1090
+ /// [`panic!`]. It will return [`None`] when the channel has hung up.
1091
+ ///
1092
+ /// [`panic!`]: ../../../std/macro.panic.html
1093
+ /// [`None`]: ../../../std/option/enum.Option.html#variant.None
1094
+ ///
1095
+ /// # Examples
1096
+ ///
1097
+ /// ```rust
1098
+ /// use std::sync::mpsc::channel;
1099
+ /// use std::thread;
1100
+ /// let (send, recv) = channel();
1101
+ /// thread::spawn(move || {
1102
+ /// send.send(1u8);
1103
+ /// send.send(2u8);
1104
+ /// send.send(3u8);
1105
+ /// });
1106
+ /// for x in recv.iter() {
1107
+ /// println!("Got: {}", x);
1108
+ /// }
1109
+ /// ```
1007
1110
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1008
1111
pub fn iter ( & self ) -> Iter < T > {
1009
1112
Iter { rx : self }
1010
1113
}
1011
1114
1012
1115
/// Returns an iterator that will attempt to yield all pending values.
1013
1116
/// It will return `None` if there are no more pending values or if the
1014
- /// channel has hung up. The iterator will never `panic!` or block the
1117
+ /// channel has hung up. The iterator will never [ `panic!`] or block the
1015
1118
/// user by waiting for values.
1119
+ ///
1120
+ /// [`panic!`]: ../../../std/macro.panic.html
1016
1121
#[ stable( feature = "receiver_try_iter" , since = "1.15.0" ) ]
1017
1122
pub fn try_iter ( & self ) -> TryIter < T > {
1018
1123
TryIter { rx : self }
0 commit comments