pub struct Permit<'a, T> { /* private fields */ }Expand description
Permits to send one value into the channel.
Returned by Sender::reserve and Sender::try_reserve; it guarantees
room for one message before the message exists. The send through it never
waits and never fails, even when the receiver was closed in the meantime
(see Receiver::close). Dropping it without
sending gives the slot back.
§Progress
send is lock-free, not wait-free: it pushes onto the
message queue, which retries when it loses a slot to another sender.
Dropping the permit is one atomic add on the semaphore when no sender
waits, and lock-free when it serves waiting senders.
use nuclei::sync::mpsc;
let (tx, mut rx) = mpsc::channel(1);
let permit = tx.try_reserve().unwrap();
permit.send("reserved");
assert_eq!(rx.try_recv(), Ok("reserved"));Implementations§
Source§impl<'a, T> Permit<'a, T>
impl<'a, T> Permit<'a, T>
Sourcepub fn send(self, value: T)
pub fn send(self, value: T)
Sends a value through the reserved slot, consuming the permit.
The send succeeds even if the receiver was closed after the slot was reserved: closing keeps what is reserved receivable, so a clean shutdown loses nothing. (If the receiver was dropped, the value is dropped when the last sender is.)
use nuclei::sync::mpsc;
let (tx, mut rx) = mpsc::channel(1);
let permit = tx.try_reserve().unwrap();
rx.close();
permit.send(456);
assert_eq!(rx.try_recv(), Ok(456));