Skip to main content

ps_mpmc/receiver/methods/
recv.rs

1use std::sync::mpsc::RecvError;
2
3use crate::Receiver;
4
5impl<T> Receiver<T> {
6    /// Blocks the current thread until a message is received or the channel is
7    /// disconnected.
8    ///
9    /// ## Blocking
10    /// - If no message is ready and at least one Sender still
11    ///   exists, execution is suspended until either
12    ///   - a message is sent, or
13    ///   - every Sender is dropped.
14    ///
15    /// ## Buffered
16    /// - Messages sent *before* the last Sender is dropped are still delivered.
17    ///
18    /// # Errors
19    /// Once all senders are gone, and all buffered messages are consumed,
20    /// all subsequent calls return [`RecvError`].
21    ///
22    /// # Safety
23    /// Channels are prone to deadlocks. You shall ensure a message will be sent
24    /// via a corresponding Sender, or that all corresponding Senders will be dropped.
25    ///
26    /// # Examples
27    /// ```
28    /// use ps_mpmc::channel;
29    ///
30    /// let (tx, rx) = channel::<i32>().into_parts();
31    ///
32    /// std::thread::spawn(move || { tx.send(42).unwrap(); });
33    ///
34    /// assert_eq!(rx.recv(), Ok(42));
35    /// assert_eq!(rx.recv(), Err(std::sync::mpsc::RecvError));
36    /// ```
37    ///
38    /// See also [`std::sync::mpsc::Receiver::recv`] for details of the underlying implementation.
39    pub fn recv(&self) -> Result<T, RecvError> {
40        self.inner.lock().recv()
41    }
42}