Skip to content

Commit 71a6441

Browse files
committed
Auto merge of #26715 - steveklabnik:gh26497, r=huonw
Add an example, plus some text that covers the buffering nature of channels. Fixes #26497
2 parents 99ca63f + 5564172 commit 71a6441

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/libstd/sync/mpsc/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,48 @@ impl<T> Receiver<T> {
768768
/// If the corresponding `Sender` has disconnected, or it disconnects while
769769
/// this call is blocking, this call will wake up and return `Err` to
770770
/// indicate that no more messages can ever be received on this channel.
771+
/// However, since channels are buffered, messages sent before the disconnect
772+
/// will still be properly received.
773+
///
774+
/// # Examples
775+
///
776+
/// ```
777+
/// use std::sync::mpsc;
778+
/// use std::thread;
779+
///
780+
/// let (send, recv) = mpsc::channel();
781+
/// let handle = thread::spawn(move || {
782+
/// send.send(1u8).unwrap();
783+
/// });
784+
///
785+
/// handle.join().unwrap();
786+
///
787+
/// assert_eq!(Ok(1), recv.recv());
788+
/// ```
789+
///
790+
/// Buffering behavior:
791+
///
792+
/// ```
793+
/// use std::sync::mpsc;
794+
/// use std::thread;
795+
/// use std::sync::mpsc::RecvError;
796+
///
797+
/// let (send, recv) = mpsc::channel();
798+
/// let handle = thread::spawn(move || {
799+
/// send.send(1u8).unwrap();
800+
/// send.send(2).unwrap();
801+
/// send.send(3).unwrap();
802+
/// drop(send);
803+
/// });
804+
///
805+
/// // wait for the thread to join so we ensure the sender is dropped
806+
/// handle.join().unwrap();
807+
///
808+
/// assert_eq!(Ok(1), recv.recv());
809+
/// assert_eq!(Ok(2), recv.recv());
810+
/// assert_eq!(Ok(3), recv.recv());
811+
/// assert_eq!(Err(RecvError), recv.recv());
812+
/// ```
771813
#[stable(feature = "rust1", since = "1.0.0")]
772814
pub fn recv(&self) -> Result<T, RecvError> {
773815
loop {

0 commit comments

Comments
 (0)