Skip to main content

OwnedPermit

Struct OwnedPermit 

Source
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>

Source

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();
Source

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);
Source

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));
Source

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));

Trait Implementations§

Source§

impl<T> Debug for OwnedPermit<T>

Source§

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

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

impl<T> Drop for OwnedPermit<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> Freeze for OwnedPermit<T>

§

impl<T> RefUnwindSafe for OwnedPermit<T>

§

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

§

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

§

impl<T> Unpin for OwnedPermit<T>

§

impl<T> UnsafeUnpin for OwnedPermit<T>

§

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