summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-07-02 12:28:11 +0000
committerbors <bors@rust-lang.org>2015-07-02 12:28:11 +0000
commit71a644136183adff664215dd6e6742330214cbac (patch)
treedc8180d6ddf9d441368d6eb4fb22214d84d54d7b /src/libstd
parent99ca63fbd88a34ce8e2b93215d9155f551a8aaad (diff)
parent55641720aa29aab8a82ff907f6ac513180337f93 (diff)
downloadrust-71a644136183adff664215dd6e6742330214cbac.tar.gz
rust-71a644136183adff664215dd6e6742330214cbac.zip
Auto merge of #26715 - steveklabnik:gh26497, r=huonw
Add an example, plus some text that covers the buffering nature of
channels.

Fixes #26497
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sync/mpsc/mod.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs
index 77aeeca7968..1453c91fd4d 100644
--- a/src/libstd/sync/mpsc/mod.rs
+++ b/src/libstd/sync/mpsc/mod.rs
@@ -768,6 +768,48 @@ impl<T> Receiver<T> {
     /// If the corresponding `Sender` has disconnected, or it disconnects while
     /// this call is blocking, this call will wake up and return `Err` to
     /// indicate that no more messages can ever be received on this channel.
+    /// However, since channels are buffered, messages sent before the disconnect
+    /// will still be properly received.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::mpsc;
+    /// use std::thread;
+    ///
+    /// let (send, recv) = mpsc::channel();
+    /// let handle = thread::spawn(move || {
+    ///     send.send(1u8).unwrap();
+    /// });
+    ///
+    /// handle.join().unwrap();
+    ///
+    /// assert_eq!(Ok(1), recv.recv());
+    /// ```
+    ///
+    /// Buffering behavior:
+    ///
+    /// ```
+    /// use std::sync::mpsc;
+    /// use std::thread;
+    /// use std::sync::mpsc::RecvError;
+    ///
+    /// let (send, recv) = mpsc::channel();
+    /// let handle = thread::spawn(move || {
+    ///     send.send(1u8).unwrap();
+    ///     send.send(2).unwrap();
+    ///     send.send(3).unwrap();
+    ///     drop(send);
+    /// });
+    ///
+    /// // wait for the thread to join so we ensure the sender is dropped
+    /// handle.join().unwrap();
+    ///
+    /// assert_eq!(Ok(1), recv.recv());
+    /// assert_eq!(Ok(2), recv.recv());
+    /// assert_eq!(Ok(3), recv.recv());
+    /// assert_eq!(Err(RecvError), recv.recv());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn recv(&self) -> Result<T, RecvError> {
         loop {