Skip to main content

Receiver

Struct Receiver 

Source
pub struct Receiver<T> { /* private fields */ }
Expand description

Receive a value from the associated Sender.

This is created by the channel function. Messages sent to the channel can be retrieved using recv. recv method blocks thread.

Implementations§

Source§

impl<T> Receiver<T>

Source

pub fn recv(self) -> Result<T, RecvError>

Attempts to wait for a value on this receiver, returning an error if the corresponding channel has hung up.

This function will always block the current thread if there is no data available. Once a message is sent to the corresponding Sender, this receiver will wake up and return that message.

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.

§Example
let (tx, rx) = sync_oneshot::channel();

let th_handle = std::thread::spawn(move || {
    tx.send(5).unwrap();
});

th_handle.join().unwrap();

assert_eq!(5, rx.recv().unwrap());
Source

pub fn try_recv(&mut self) -> Result<T, TryRecvError>

Attempts to return a pending value on this receiver without blocking.

This method will never block the caller in order to wait for data to become available. Instead, this will always return immediately with a possible option of pending data on the channel.

This is useful for a flavor of “optimistic check” before deciding to block on a receiver.

Compared with recv, this function has two failure cases instead of one (one for disconnection, one for an empty buffer).

Source

pub fn close(&mut self)

Prevents the associated Sender handle from sending a value.

Any send operation which happens after calling close is guaranteed to fail. After calling close, try_recv should be called to receive a value if one was sent before the call to close completed.

This function is useful to perform a graceful shutdown and ensure that a value will not be sent into the channel and never received.

close is no-op if a message is already received or the channel is already closed.

§Examples

Prevent a value from being sent

use sync_oneshot::TryRecvError;

let (tx, mut rx) = sync_oneshot::channel();

assert!(!tx.is_closed());

rx.close();

assert!(tx.is_closed());
assert!(tx.send("never received").is_err());

match rx.try_recv() {
    Err(TryRecvError::Closed) => {}
    _ => unreachable!(),
}

Receive a value sent before calling close

let (tx, mut rx) = sync_oneshot::channel();

assert!(tx.send("will receive").is_ok());

rx.close();

let msg = rx.try_recv().unwrap();
assert_eq!(msg, "will receive");

Trait Implementations§

Source§

impl<T: Debug> Debug for Receiver<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for Receiver<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Send for Receiver<T>
where T: Send,

Source§

impl<T> Sync for Receiver<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> Freeze for Receiver<T>

§

impl<T> !RefUnwindSafe for Receiver<T>

§

impl<T> Unpin for Receiver<T>

§

impl<T> UnsafeUnpin for Receiver<T>

§

impl<T> !UnwindSafe for Receiver<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.