Struct permit::Permit[][src]

pub struct Permit { /* fields omitted */ }

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.
top_permit.revoke();
// Give the threads time to finish
// and drop their permits.
let _ = top_permit.try_wait_for(
    core::time::Duration::from_secs(3));

Implementations

impl Permit[src]

#[must_use]pub fn new() -> Self[src]

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.

#[must_use]pub fn new_sub(&self) -> Self[src]

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.

#[must_use]pub fn is_revoked(&self) -> bool[src]

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

#[must_use]pub fn ok(&self) -> Option<()>[src]

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

pub fn revoke(&self)[src]

Revokes this permit and all subordinate permits.

#[must_use]pub fn has_subs(&self) -> bool[src]

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

This includes direct subordinates and their subordinates, recursively.

pub fn wait(&self)[src]

Wait indefinitely for all subordinate permits to drop.

This waits for all direct subordinates and their subordinates, recursively.

pub fn try_wait_for(&self, duration: Duration) -> Result<(), DeadlineExceeded>[src]

Wait for all subordinate permits to drop.

This waits for all direct subordinates and their subordinates, recursively.

Errors

Returns DeadlineExceeded if the subordinate permits are not all dropped before duration passes.

pub fn try_wait_until(&self, deadline: Instant) -> Result<(), DeadlineExceeded>[src]

Wait for all subordinate permits to drop.

This waits for all direct subordinates and their subordinates, recursively.

Errors

Returns DeadlineExceeded if the subordinate permits are not all dropped before deadline passes.

Trait Implementations

impl Clone for Permit[src]

impl Default for Permit[src]

impl Drop for Permit[src]

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.