pub struct OwnedPermit<T> { /* private fields */ }Expand description
An owned permit to send one value into the channel.
The same as Permit, except that it holds the Sender it was reserved
from instead of borrowing it, so it has no lifetime. Returned by
Sender::reserve_owned and Sender::try_reserve_owned. It counts as a
sender while it lives, so the channel does not end for the receiver while a
permit is held. send and release hand
the sender back; dropping the permit unused gives the slot back and drops
the sender.
§Progress
send is lock-free, not wait-free, for the reason
Permit::send is: it pushes onto the message queue. release
and the drop return the slot as dropping a Permit does (one atomic add
when no sender waits, lock-free otherwise), and the drop then counts the
sender out, which is wait-free. same_channel and
same_channel_as_sender are wait-free.
use nuclei::sync::mpsc;
let (tx, mut rx) = mpsc::channel(1);
let permit = tx.try_reserve_owned().unwrap();
let tx = permit.send(123);
assert_eq!(rx.try_recv(), Ok(123));
tx.try_send(456).unwrap();Implementations§
Source§impl<T> OwnedPermit<T>
impl<T> OwnedPermit<T>
Sourcepub fn send(self, value: T) -> Sender<T>
pub fn send(self, value: T) -> Sender<T>
Sends a value through the reserved slot, returning the sender the permit was reserved from.
As for Permit::send, it succeeds even if the receiver was closed
after the reservation.
use nuclei::sync::mpsc;
let (tx, mut rx) = mpsc::channel(1);
let permit = tx.try_reserve_owned().unwrap();
let tx = permit.send(456);
assert_eq!(rx.try_recv(), Ok(456));
tx.try_send(789).unwrap();Sourcepub fn release(self) -> Sender<T>
pub fn release(self) -> Sender<T>
Gives the reserved slot back without sending, returning the sender.
use nuclei::sync::mpsc;
let (tx, _rx) = mpsc::channel(1);
let permit = tx.clone().try_reserve_owned().unwrap();
assert!(tx.try_send(123).is_err(), "the permit holds the only slot");
let tx2 = permit.release();
tx.try_send(789).unwrap();
drop(tx2);Sourcepub fn same_channel(&self, other: &Self) -> bool
pub fn same_channel(&self, other: &Self) -> bool
Whether two permits belong to the same channel.
use nuclei::sync::mpsc;
let (tx, _rx) = mpsc::channel::<()>(2);
let a = tx.clone().try_reserve_owned().unwrap();
let b = tx.clone().try_reserve_owned().unwrap();
assert!(a.same_channel(&b));
let (tx2, _rx2) = mpsc::channel::<()>(1);
let c = tx2.try_reserve_owned().unwrap();
assert!(!c.same_channel(&a));Sourcepub fn same_channel_as_sender(&self, sender: &Sender<T>) -> bool
pub fn same_channel_as_sender(&self, sender: &Sender<T>) -> bool
Whether this permit belongs to the same channel as sender.
use nuclei::sync::mpsc;
let (tx, _rx) = mpsc::channel::<()>(1);
let permit = tx.clone().try_reserve_owned().unwrap();
assert!(permit.same_channel_as_sender(&tx));
let (tx2, _rx2) = mpsc::channel::<()>(1);
assert!(!permit.same_channel_as_sender(&tx2));