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>
impl<T> Sender<T>
Sourcepub fn send(self, value: T) -> Result<(), T>
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));Sourcepub async fn closed(&mut self)
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(()));
}Sourcepub fn poll_closed(&mut self, cx: &mut Context<'_>) -> Poll<()>
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.