Sender

Struct Sender 

Source
pub struct Sender<T> { /* private fields */ }
🔬This is a nightly-only experimental API. (oneshot_channel #143674)
Expand description

The sending half of a oneshot channel.

§Examples

#![feature(oneshot_channel)]
use std::sync::oneshot;
use std::thread;

let (sender, receiver) = oneshot::channel();

thread::spawn(move || {
    sender.send("Hello from thread!").unwrap();
});

assert_eq!(receiver.recv().unwrap(), "Hello from thread!");

Sender cannot be sent between threads if it is sending non-Send types.

ⓘ
#![feature(oneshot_channel)]
use std::sync::oneshot;
use std::thread;
use std::ptr;

let (sender, receiver) = oneshot::channel();

struct NotSend(*mut ());
thread::spawn(move || {
    sender.send(NotSend(ptr::null_mut()));
});

let reply = receiver.try_recv().unwrap();

Implementations§

Source§

impl<T> Sender<T>

Source

pub fn send(self, t: T) -> Result<(), SendError<T>>

🔬This is a nightly-only experimental API. (oneshot_channel #143674)

Attempts to send a value through this channel. This can only fail if the corresponding Receiver<T> has been dropped.

This method is non-blocking (wait-free).

§Examples
#![feature(oneshot_channel)]
use std::sync::oneshot;
use std::thread;

let (tx, rx) = oneshot::channel();

thread::spawn(move || {
    // Perform some computation.
    let result = 2 + 2;
    tx.send(result).unwrap();
});

assert_eq!(rx.recv().unwrap(), 4);

Trait Implementations§

Source§

impl<T> Debug for Sender<T>

Source§

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

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

impl<T> Sync for Sender<T>

Auto Trait Implementations§

§

impl<T> Freeze for Sender<T>

§

impl<T> RefUnwindSafe for Sender<T>

§

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

§

impl<T> Unpin for Sender<T>

§

impl<T> UnwindSafe for Sender<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.