Struct permit::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 copy 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

§

type Output = ()

The type of value produced on completion.
source§

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

Attempt 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 !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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Fwhere F: Future,

§

type Output = <F as Future>::Output

The output that the future will produce on completion.
§

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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.