Skip to main content

Permit

Struct Permit 

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

Source

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

Trait Implementations§

Source§

impl<T> Debug for Permit<'_, T>

Source§

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

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

impl<T> Drop for Permit<'_, 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<'a, T> Freeze for Permit<'a, T>

§

impl<'a, T> RefUnwindSafe for Permit<'a, T>

§

impl<'a, T> Send for Permit<'a, T>
where T: Send,

§

impl<'a, T> Sync for Permit<'a, T>
where T: Send,

§

impl<'a, T> Unpin for Permit<'a, T>

§

impl<'a, T> UnsafeUnpin for Permit<'a, T>

§

impl<'a, T> UnwindSafe for Permit<'a, 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.