Skip to content

Commit 2bdf368

Browse files
committed
Auto merge of #41103 - projektir:channel_error_docs, r=GuillaumeGomez
Channel error docs r? @steveklabnik I'm going to need some help on this one, a few ambiguities.
2 parents 22bae87 + 28a232a commit 2bdf368

File tree

2 files changed

+87
-51
lines changed

2 files changed

+87
-51
lines changed

src/libstd/sync/mpsc/mod.rs

+84-50
Original file line numberDiff line numberDiff line change
@@ -436,81 +436,97 @@ unsafe impl<T: Send> Send for SyncSender<T> {}
436436
#[stable(feature = "rust1", since = "1.0.0")]
437437
impl<T> !Sync for SyncSender<T> {}
438438

439-
/// An error returned from the [`send`] function on channels.
439+
/// An error returned from the [`Sender::send`] or [`SyncSender::send`]
440+
/// function on **channel**s.
440441
///
441-
/// A [`send`] operation can only fail if the receiving end of a channel is
442+
/// A **send** operation can only fail if the receiving end of a channel is
442443
/// disconnected, implying that the data could never be received. The error
443444
/// contains the data being sent as a payload so it can be recovered.
444445
///
445-
/// [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
446+
/// [`Sender::send`]: struct.Sender.html#method.send
447+
/// [`SyncSender::send`]: struct.SyncSender.html#method.send
446448
#[stable(feature = "rust1", since = "1.0.0")]
447449
#[derive(PartialEq, Eq, Clone, Copy)]
448450
pub struct SendError<T>(#[stable(feature = "rust1", since = "1.0.0")] pub T);
449451

450452
/// An error returned from the [`recv`] function on a [`Receiver`].
451453
///
452-
/// The [`recv`] operation can only fail if the sending half of a channel is
453-
/// disconnected, implying that no further messages will ever be received.
454+
/// The [`recv`] operation can only fail if the sending half of a
455+
/// [`channel`] (or [`sync_channel`]) is disconnected, implying that no further
456+
/// messages will ever be received.
454457
///
455-
/// [`recv`]: ../../../std/sync/mpsc/struct.Receiver.html#method.recv
456-
/// [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
458+
/// [`recv`]: struct.Receiver.html#method.recv
459+
/// [`Receiver`]: struct.Receiver.html
460+
/// [`channel`]: fn.channel.html
461+
/// [`sync_channel`]: fn.sync_channel.html
457462
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
458463
#[stable(feature = "rust1", since = "1.0.0")]
459464
pub struct RecvError;
460465

461466
/// This enumeration is the list of the possible reasons that [`try_recv`] could
462-
/// not return data when called.
467+
/// not return data when called. This can occur with both a [`channel`] and
468+
/// a [`sync_channel`].
463469
///
464-
/// [`try_recv`]: ../../../std/sync/mpsc/struct.Receiver.html#method.try_recv
470+
/// [`try_recv`]: struct.Receiver.html#method.try_recv
471+
/// [`channel`]: fn.channel.html
472+
/// [`sync_channel`]: fn.sync_channel.html
465473
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
466474
#[stable(feature = "rust1", since = "1.0.0")]
467475
pub enum TryRecvError {
468-
/// This channel is currently empty, but the sender(s) have not yet
476+
/// This **channel** is currently empty, but the **Sender**(s) have not yet
469477
/// disconnected, so data may yet become available.
470478
#[stable(feature = "rust1", since = "1.0.0")]
471479
Empty,
472480

473-
/// This channel's sending half has become disconnected, and there will
474-
/// never be any more data received on this channel
481+
/// The **channel**'s sending half has become disconnected, and there will
482+
/// never be any more data received on it.
475483
#[stable(feature = "rust1", since = "1.0.0")]
476484
Disconnected,
477485
}
478486

479-
/// This enumeration is the list of possible errors that [`recv_timeout`] could
480-
/// not return data when called.
487+
/// This enumeration is the list of possible errors that made [`recv_timeout`]
488+
/// unable to return data when called. This can occur with both a [`channel`] and
489+
/// a [`sync_channel`].
481490
///
482-
/// [`recv_timeout`]: ../../../std/sync/mpsc/struct.Receiver.html#method.recv_timeout
491+
/// [`recv_timeout`]: struct.Receiver.html#method.recv_timeout
492+
/// [`channel`]: fn.channel.html
493+
/// [`sync_channel`]: fn.sync_channel.html
483494
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
484495
#[stable(feature = "mpsc_recv_timeout", since = "1.12.0")]
485496
pub enum RecvTimeoutError {
486-
/// This channel is currently empty, but the sender(s) have not yet
497+
/// This **channel** is currently empty, but the **Sender**(s) have not yet
487498
/// disconnected, so data may yet become available.
488499
#[stable(feature = "mpsc_recv_timeout", since = "1.12.0")]
489500
Timeout,
490-
/// This channel's sending half has become disconnected, and there will
491-
/// never be any more data received on this channel
501+
/// The **channel**'s sending half has become disconnected, and there will
502+
/// never be any more data received on it.
492503
#[stable(feature = "mpsc_recv_timeout", since = "1.12.0")]
493504
Disconnected,
494505
}
495506

496507
/// This enumeration is the list of the possible error outcomes for the
497-
/// [`SyncSender::try_send`] method.
508+
/// [`try_send`] method.
498509
///
499-
/// [`SyncSender::try_send`]: ../../../std/sync/mpsc/struct.SyncSender.html#method.try_send
510+
/// [`try_send`]: struct.SyncSender.html#method.try_send
500511
#[stable(feature = "rust1", since = "1.0.0")]
501512
#[derive(PartialEq, Eq, Clone, Copy)]
502513
pub enum TrySendError<T> {
503-
/// The data could not be sent on the channel because it would require that
514+
/// The data could not be sent on the [`sync_channel`] because it would require that
504515
/// the callee block to send the data.
505516
///
506517
/// If this is a buffered channel, then the buffer is full at this time. If
507-
/// this is not a buffered channel, then there is no receiver available to
518+
/// this is not a buffered channel, then there is no [`Receiver`] available to
508519
/// acquire the data.
520+
///
521+
/// [`sync_channel`]: fn.sync_channel.html
522+
/// [`Receiver`]: struct.Receiver.html
509523
#[stable(feature = "rust1", since = "1.0.0")]
510524
Full(#[stable(feature = "rust1", since = "1.0.0")] T),
511525

512-
/// This channel's receiving half has disconnected, so the data could not be
526+
/// This [`sync_channel`]'s receiving half has disconnected, so the data could not be
513527
/// sent. The data is returned back to the callee in this case.
528+
///
529+
/// [`sync_channel`]: fn.sync_channel.html
514530
#[stable(feature = "rust1", since = "1.0.0")]
515531
Disconnected(#[stable(feature = "rust1", since = "1.0.0")] T),
516532
}
@@ -544,36 +560,46 @@ impl<T> UnsafeFlavor<T> for Receiver<T> {
544560
}
545561

546562
/// Creates a new asynchronous channel, returning the sender/receiver halves.
547-
/// All data sent on the sender will become available on the receiver, and no
548-
/// send will block the calling thread (this channel has an "infinite buffer").
563+
/// All data sent on the [`Sender`] will become available on the [`Receiver`] in
564+
/// the same order as it was sent, and no [`send`] will block the calling thread
565+
/// (this channel has an "infinite buffer", unlike [`sync_channel`], which will
566+
/// block after its buffer limit is reached). [`recv`] will block until a message
567+
/// is available.
568+
///
569+
/// The [`Sender`] can be cloned to [`send`] to the same channel multiple times, but
570+
/// only one [`Receiver`] is supported.
549571
///
550572
/// If the [`Receiver`] is disconnected while trying to [`send`] with the
551-
/// [`Sender`], the [`send`] method will return an error.
573+
/// [`Sender`], the [`send`] method will return a [`SendError`]. Similarly, If the
574+
/// [`Sender`] is disconnected while trying to [`recv`], the [`recv`] method will
575+
/// return a [`RecvError`].
552576
///
553-
/// [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
554-
/// [`Sender`]: ../../../std/sync/mpsc/struct.Sender.html
555-
/// [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
577+
/// [`send`]: struct.Sender.html#method.send
578+
/// [`recv`]: struct.Receiver.html#method.recv
579+
/// [`Sender`]: struct.Sender.html
580+
/// [`Receiver`]: struct.Receiver.html
581+
/// [`sync_channel`]: fn.sync_channel.html
582+
/// [`SendError`]: struct.SendError.html
583+
/// [`RecvError`]: struct.RecvError.html
556584
///
557585
/// # Examples
558586
///
559587
/// ```
560588
/// use std::sync::mpsc::channel;
561589
/// use std::thread;
562590
///
563-
/// // tx is the sending half (tx for transmission), and rx is the receiving
564-
/// // half (rx for receiving).
565-
/// let (tx, rx) = channel();
591+
/// let (sender, receiver) = channel();
566592
///
567593
/// // Spawn off an expensive computation
568594
/// thread::spawn(move|| {
569595
/// # fn expensive_computation() {}
570-
/// tx.send(expensive_computation()).unwrap();
596+
/// sender.send(expensive_computation()).unwrap();
571597
/// });
572598
///
573599
/// // Do some useful work for awhile
574600
///
575601
/// // Let's see what that answer was
576-
/// println!("{:?}", rx.recv().unwrap());
602+
/// println!("{:?}", receiver.recv().unwrap());
577603
/// ```
578604
#[stable(feature = "rust1", since = "1.0.0")]
579605
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
@@ -582,43 +608,51 @@ pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
582608
}
583609

584610
/// Creates a new synchronous, bounded channel.
585-
///
586-
/// Like asynchronous channels, the [`Receiver`] will block until a message
587-
/// becomes available. These channels differ greatly in the semantics of the
588-
/// sender from asynchronous channels, however.
611+
/// All data sent on the [`SyncSender`] will become available on the [`Receiver`]
612+
/// in the same order as it was sent. Like asynchronous [`channel`]s, the
613+
/// [`Receiver`] will block until a message becomes available. `sync_channel`
614+
/// differs greatly in the semantics of the sender, however.
589615
///
590616
/// This channel has an internal buffer on which messages will be queued.
591617
/// `bound` specifies the buffer size. When the internal buffer becomes full,
592618
/// future sends will *block* waiting for the buffer to open up. Note that a
593619
/// buffer size of 0 is valid, in which case this becomes "rendezvous channel"
594-
/// where each [`send`] will not return until a recv is paired with it.
620+
/// where each [`send`] will not return until a [`recv`] is paired with it.
621+
///
622+
/// The [`SyncSender`] can be cloned to [`send`] to the same channel multiple
623+
/// times, but only one [`Receiver`] is supported.
595624
///
596-
/// Like asynchronous channels, if the [`Receiver`] is disconnected while
597-
/// trying to [`send`] with the [`SyncSender`], the [`send`] method will
598-
/// return an error.
625+
/// Like asynchronous channels, if the [`Receiver`] is disconnected while trying
626+
/// to [`send`] with the [`SyncSender`], the [`send`] method will return a
627+
/// [`SendError`]. Similarly, If the [`SyncSender`] is disconnected while trying
628+
/// to [`recv`], the [`recv`] method will return a [`RecvError`].
599629
///
600-
/// [`send`]: ../../../std/sync/mpsc/struct.SyncSender.html#method.send
601-
/// [`SyncSender`]: ../../../std/sync/mpsc/struct.SyncSender.html
602-
/// [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
630+
/// [`channel`]: fn.channel.html
631+
/// [`send`]: struct.SyncSender.html#method.send
632+
/// [`recv`]: struct.Receiver.html#method.recv
633+
/// [`SyncSender`]: struct.SyncSender.html
634+
/// [`Receiver`]: struct.Receiver.html
635+
/// [`SendError`]: struct.SendError.html
636+
/// [`RecvError`]: struct.RecvError.html
603637
///
604638
/// # Examples
605639
///
606640
/// ```
607641
/// use std::sync::mpsc::sync_channel;
608642
/// use std::thread;
609643
///
610-
/// let (tx, rx) = sync_channel(1);
644+
/// let (sender, receiver) = sync_channel(1);
611645
///
612646
/// // this returns immediately
613-
/// tx.send(1).unwrap();
647+
/// sender.send(1).unwrap();
614648
///
615649
/// thread::spawn(move|| {
616650
/// // this will block until the previous message has been received
617-
/// tx.send(2).unwrap();
651+
/// sender.send(2).unwrap();
618652
/// });
619653
///
620-
/// assert_eq!(rx.recv().unwrap(), 1);
621-
/// assert_eq!(rx.recv().unwrap(), 2);
654+
/// assert_eq!(receiver.recv().unwrap(), 1);
655+
/// assert_eq!(receiver.recv().unwrap(), 2);
622656
/// ```
623657
#[stable(feature = "rust1", since = "1.0.0")]
624658
pub fn sync_channel<T>(bound: usize) -> (SyncSender<T>, Receiver<T>) {

src/libstd/sys_common/poison.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ pub struct PoisonError<T> {
7373
}
7474

7575
/// An enumeration of possible errors which can occur while calling the
76-
/// `try_lock` method.
76+
/// [`try_lock`] method.
77+
///
78+
/// [`try_lock`]: struct.Mutex.html#method.try_lock
7779
#[stable(feature = "rust1", since = "1.0.0")]
7880
pub enum TryLockError<T> {
7981
/// The lock could not be acquired because another thread failed while holding

0 commit comments

Comments
 (0)