pub struct Defer<S> { /* private fields */ }
Expand description

A simple, thread-safe utility designed to make it easier to deal with double-&mut errors.

fn broken(&mut self) {
    if let Some(val) = self.hold_mutable_ref() {
        self.do_mutable_thing(val); // error: self borrowed mutably twice
    }
}
fn working(&mut self) {
    if let Some(val) = self.hold_mutable_ref() {
        self.do_later.defer(val); // no error thanks to interior mutability
    }
    self.do_later.execute(|val| {
        self.do_mutable_thing(val); // runs only if `defer` was called earlier
    });
}

Implementations

Constructs a new Defer.

Returns true if a deferred state is stored

Stores the provided deferred state atomically. Will replace any existing value. Check status with is_deferred first if you don’t want to replace the value. Panics if the lock is held elsewhere. Use try_defer to invoke safely in multi-threaded contexts.

Stores a deferred state only if one isn’t already pending. Unlike defer, this will not panic if the lock is currently held elsewhere. Returns true if the state was stored, false otherwise.

Runs the given closure with a deferred state if and only if one is currently stored. Returns true if the closure was executed. Panics if the lock is held elsewhere. Use try_execute to invoke safely in multi-threaded or re-entrant contexts.

Runs the given closure with a deferred state if and only if one is currently stored. Unlike execute, this will not panic if the lock is already held elsewhere. Returns Err(()) if the lock was held, this means the closure wasn’t run. If the lock wasn’t already held, this returns ’Ok(true)if the closure was ran andOk(false)` if not.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.