Permit

Struct Permit 

Source
pub struct Permit { /* private fields */ }
Expand description

A struct for cancelling operations.

Use new_sub() to make a subordinate permit. Call revoke() to revoke a permit and its subordinate permits, recursively.

§Example

Graceful shutdown:

let top_permit = permit::Permit::new();
// Start some worker threads.
for _ in 0..5 {
    let permit = top_permit.new_sub();
    std::thread::spawn(move || {
        while !permit.is_revoked() {
            // ...
        }
    });
}
wait_for_shutdown_signal();
// Revoke all thread permits and wait for them to
// finish and drop their permits.
top_permit
    .revoke()
    .wait_subs_timeout(core::time::Duration::from_secs(3))
    .unwrap();

Implementations§

Source§

impl Permit

Source

pub fn new() -> Self

Makes a new permit.

This permit is not subordinate to any other permit. It has no superior.

Dropping the permit revokes it and any subordinate permits.

Source

pub fn new_sub(&self) -> Self

Make a new permit that is subordinate to this permit.

Call revoke() to revoke a permit and its subordinate permits, recursively.

Dropping the permit revokes it and any subordinate permits.

Source

pub fn is_revoked(&self) -> bool

Returns true if revoke() has previously been called on this permit or any of its superiors.

Source

pub fn ok(&self) -> Option<()>

Returns Some(()) if revoke() has not been called on this permit or any of its superiors.

Source

pub fn revoke(&self) -> &Self

Revokes this permit and all subordinate permits.

Source

pub fn has_subs(&self) -> bool

Returns true if this permit has any subordinate permits that have not been dropped.

This includes direct subordinates and their subordinates, recursively.

Source

pub fn sleep(&self, duration: Duration) -> Result<(), PermitRevoked>

Waits until duration time passes or the permit is revoked.

§Errors

Returns Err when the permit is revoked.

Source

pub fn sleep_until(&self, deadline: Instant) -> Result<(), PermitRevoked>

Waits until deadline or the permit is revoked.

§Errors

Returns Err when the permit is revoked.

Source

pub fn wait_subs_timeout( &self, duration: Duration, ) -> Result<(), DeadlineExceeded>

Waits for all direct subordinate permits to drop.

§Errors

Returns DeadlineExceeded when duration passes and the permit has a subordinate permit.

Source

pub fn wait_subs_deadline( &self, deadline: Instant, ) -> Result<(), DeadlineExceeded>

Waits for all direct subordinate permits to drop.

§Errors

Returns DeadlineExceeded when deadline passes and the permit has a subordinate permit.

Trait Implementations§

Source§

impl Clone for Permit

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Permit

Source§

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

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

impl Default for Permit

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Permit

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Future for Permit

Source§

type Output = ()

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations§

§

impl Freeze for Permit

§

impl RefUnwindSafe for Permit

§

impl Send for Permit

§

impl Sync for Permit

§

impl Unpin for Permit

§

impl UnwindSafe for Permit

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.

Source§

impl<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.