Skip to content

Commit 89c35ae

Browse files
committed
Add links and examples to std::sync::mpsc docs (#29377)
This change adds links to to `Receiver`, `Iter`, `TryIter`, `IntoIter`, `Sender`, `SyncSender`, `SendError`, `RecvError`, `TryRecvError`, `RecvTimeoutError`, `TrySendError`, `Sender::send`, `SyncSender::send`, `SyncSender::try_send`, `Receiver::recv`, `Receiver::recv_timeout`, `Receiver::iter`, and `Receiver::try_iter`. Examples added to `Receiver`, `Sender`, `Receiver::iter`.
1 parent 5a6ebdf commit 89c35ae

File tree

1 file changed

+136
-31
lines changed

1 file changed

+136
-31
lines changed

src/libstd/sync/mpsc/mod.rs

Lines changed: 136 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,28 @@ mod mpsc_queue;
298298
mod spsc_queue;
299299

300300
/// 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+
/// ```
302323
#[stable(feature = "rust1", since = "1.0.0")]
303324
pub struct Receiver<T> {
304325
inner: UnsafeCell<Flavor<T>>,
@@ -312,30 +333,39 @@ unsafe impl<T: Send> Send for Receiver<T> { }
312333
#[stable(feature = "rust1", since = "1.0.0")]
313334
impl<T> !Sync for Receiver<T> { }
314335

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
318342
#[stable(feature = "rust1", since = "1.0.0")]
319343
#[derive(Debug)]
320344
pub struct Iter<'a, T: 'a> {
321345
rx: &'a Receiver<T>
322346
}
323347

324348
/// 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.
327351
///
328352
/// 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
330356
#[stable(feature = "receiver_try_iter", since = "1.15.0")]
331357
#[derive(Debug)]
332358
pub struct TryIter<'a, T: 'a> {
333359
rx: &'a Receiver<T>
334360
}
335361

336362
/// 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
338364
/// 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+
///
339369
#[stable(feature = "receiver_into_iter", since = "1.1.0")]
340370
#[derive(Debug)]
341371
pub struct IntoIter<T> {
@@ -344,6 +374,30 @@ pub struct IntoIter<T> {
344374

345375
/// The sending-half of Rust's asynchronous channel type. This half can only be
346376
/// 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+
/// ```
347401
#[stable(feature = "rust1", since = "1.0.0")]
348402
pub struct Sender<T> {
349403
inner: UnsafeCell<Flavor<T>>,
@@ -359,6 +413,10 @@ impl<T> !Sync for Sender<T> { }
359413

360414
/// The sending-half of Rust's synchronous channel type. This half can only be
361415
/// 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+
///
362420
#[stable(feature = "rust1", since = "1.0.0")]
363421
pub struct SyncSender<T> {
364422
inner: Arc<sync::Packet<T>>,
@@ -370,25 +428,32 @@ unsafe impl<T: Send> Send for SyncSender<T> {}
370428
#[stable(feature = "rust1", since = "1.0.0")]
371429
impl<T> !Sync for SyncSender<T> {}
372430

373-
/// An error returned from the `send` function on channels.
431+
/// An error returned from the [`send`] function on channels.
374432
///
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
376434
/// disconnected, implying that the data could never be received. The error
377435
/// contains the data being sent as a payload so it can be recovered.
436+
///
437+
/// [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
378438
#[stable(feature = "rust1", since = "1.0.0")]
379439
#[derive(PartialEq, Eq, Clone, Copy)]
380440
pub struct SendError<T>(#[stable(feature = "rust1", since = "1.0.0")] pub T);
381441

382-
/// An error returned from the `recv` function on a `Receiver`.
442+
/// An error returned from the [`recv`] function on a [`Receiver`].
383443
///
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
385445
/// 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
386449
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
387450
#[stable(feature = "rust1", since = "1.0.0")]
388451
pub struct RecvError;
389452

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
391454
/// not return data when called.
455+
///
456+
/// [`try_recv`]: ../../../std/sync/mpsc/struct.Receiver.html#method.try_recv
392457
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
393458
#[stable(feature = "rust1", since = "1.0.0")]
394459
pub enum TryRecvError {
@@ -403,8 +468,10 @@ pub enum TryRecvError {
403468
Disconnected,
404469
}
405470

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
407472
/// not return data when called.
473+
///
474+
/// [`recv_timeout`]: ../../../std/sync/mpsc/struct.Receiver.html#method.recv_timeout
408475
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
409476
#[stable(feature = "mpsc_recv_timeout", since = "1.12.0")]
410477
pub enum RecvTimeoutError {
@@ -419,7 +486,9 @@ pub enum RecvTimeoutError {
419486
}
420487

421488
/// 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
423492
#[stable(feature = "rust1", since = "1.0.0")]
424493
#[derive(PartialEq, Eq, Clone, Copy)]
425494
pub enum TrySendError<T> {
@@ -566,10 +635,13 @@ impl<T> Sender<T> {
566635
/// A successful send occurs when it is determined that the other end of
567636
/// the channel has not hung up already. An unsuccessful send would be one
568637
/// 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
571640
/// 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
573645
///
574646
/// This method will never block the current thread.
575647
///
@@ -712,23 +784,29 @@ impl<T> SyncSender<T> {
712784
/// time. If the buffer size is 0, however, it can be guaranteed that the
713785
/// receiver has indeed received the data if this function returns success.
714786
///
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
717789
/// information.
790+
///
791+
/// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
792+
/// [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
718793
#[stable(feature = "rust1", since = "1.0.0")]
719794
pub fn send(&self, t: T) -> Result<(), SendError<T>> {
720795
self.inner.send(t).map_err(SendError)
721796
}
722797

723798
/// Attempts to send a value on this channel without blocking.
724799
///
725-
/// This method differs from `send` by returning immediately if the
800+
/// This method differs from [`send`] by returning immediately if the
726801
/// 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
728803
/// instead of one (one for disconnection, one for a full buffer).
729804
///
730-
/// See `SyncSender::send` for notes about guarantees of whether the
805+
/// See [`SyncSender::send`] for notes about guarantees of whether the
731806
/// 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
732810
#[stable(feature = "rust1", since = "1.0.0")]
733811
pub fn try_send(&self, t: T) -> Result<(), TrySendError<T>> {
734812
self.inner.try_send(t)
@@ -829,15 +907,18 @@ impl<T> Receiver<T> {
829907
///
830908
/// This function will always block the current thread if there is no data
831909
/// 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
833911
/// return that message.
834912
///
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
837915
/// indicate that no more messages can ever be received on this channel.
838916
/// However, since channels are buffered, messages sent before the disconnect
839917
/// will still be properly received.
840918
///
919+
/// [`Sender`]: ../../../std/sync/mpsc/struct.Sender.html
920+
/// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
921+
///
841922
/// # Examples
842923
///
843924
/// ```
@@ -917,15 +998,18 @@ impl<T> Receiver<T> {
917998
///
918999
/// This function will always block the current thread if there is no data
9191000
/// 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
9211002
/// return that message.
9221003
///
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
9251006
/// indicate that no more messages can ever be received on this channel.
9261007
/// However, since channels are buffered, messages sent before the disconnect
9271008
/// will still be properly received.
9281009
///
1010+
/// [`Sender`]: ../../../std/sync/mpsc/struct.Sender.html
1011+
/// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
1012+
///
9291013
/// # Examples
9301014
///
9311015
/// ```no_run
@@ -1003,16 +1087,37 @@ impl<T> Receiver<T> {
10031087
}
10041088

10051089
/// 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+
/// ```
10071110
#[stable(feature = "rust1", since = "1.0.0")]
10081111
pub fn iter(&self) -> Iter<T> {
10091112
Iter { rx: self }
10101113
}
10111114

10121115
/// Returns an iterator that will attempt to yield all pending values.
10131116
/// 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
10151118
/// user by waiting for values.
1119+
///
1120+
/// [`panic!`]: ../../../std/macro.panic.html
10161121
#[stable(feature = "receiver_try_iter", since = "1.15.0")]
10171122
pub fn try_iter(&self) -> TryIter<T> {
10181123
TryIter { rx: self }

0 commit comments

Comments
 (0)