Skip to main content

Sender

Struct Sender 

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

Sends a value to the associated Receiver.

A Sender and a Receiver are created together by channel. Every operation is wait-free; see the module docs.

use nuclei::sync::oneshot;

let (tx, rx) = oneshot::channel();
tx.send(3).expect("the receiver is still there");
assert_eq!(rx.blocking_recv(), Ok(3));

To use a Sender from a destructor, put it in an Option and call Option::take.

Implementations§

Source§

impl<T> Sender<T>

Source

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

Attempts to send a value on this channel, returning it back if it could not be sent.

This method consumes self, since only one value may ever be sent on a oneshot channel. It never waits, so it can be used in both synchronous and asynchronous code.

A successful send means the other end had not hung up when the value was published; an unsuccessful one means the Receiver was already closed or dropped, and the value comes back in the Err. Ok does not promise the value will be received: the receiver can hang up right after this returns.

Wait-free: one write and one atomic read-modify-write, plus the wake of the receiver’s task.

use nuclei::sync::oneshot;

let (tx, rx) = oneshot::channel();
drop(rx);
assert_eq!(tx.send(3), Err(3));
Source

pub async fn closed(&mut self)

Waits for the associated Receiver to close.

A receiver closes by Receiver::close or by being dropped. This is useful with a select to abort a computation whose result nobody wants any more.

use nuclei::sync::oneshot;
use std::future::Future;
use std::pin::pin;
use std::task::{Context, Poll, Waker};

let (mut tx, rx) = oneshot::channel::<()>();
let mut cx = Context::from_waker(Waker::noop());
{
    let mut closed = pin!(tx.closed());
    assert_eq!(closed.as_mut().poll(&mut cx), Poll::Pending);
    drop(rx);
    assert_eq!(closed.as_mut().poll(&mut cx), Poll::Ready(()));
}
Source

pub fn is_closed(&self) -> bool

Whether the associated Receiver has closed or been dropped.

When this is true, a call to send always fails.

use nuclei::sync::oneshot;

let (tx, rx) = oneshot::channel::<u8>();
assert!(!tx.is_closed());
drop(rx);
assert!(tx.is_closed());
Source

pub fn poll_closed(&mut self, cx: &mut Context<'_>) -> Poll<()>

Checks whether the channel is closed and, if it is not, arranges for the waker of cx to be woken when it closes.

Only the waker of the most recent call is kept.

Returns Poll::Pending while the channel is open and Poll::Ready once the receiver closed. Wait-free.

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> Drop for Sender<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Sender<T>

§

impl<T> !UnwindSafe for Sender<T>

§

impl<T> Freeze for Sender<T>

§

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

§

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

§

impl<T> Unpin for Sender<T>

§

impl<T> UnsafeUnpin 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.

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

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.