Struct Defer

Source
pub struct Defer<T: FnOnce()>(/* private fields */);
Expand description

A utility struct for deferred execution of a closure.

The Defer struct allows you to execute a closure once the Defer instance goes out of scope. It is commonly used for resource management, cleanup, or any other deferred actions.

Note: Defer MUST be bound to a variable to function properly; otherwise, it will be dropped immediately, executing the enclosed closure!

§Example

use defer_rs::*;


fn main() {
    let resource = acquire_resource();

    // Create a `Defer` instance with a closure that will be executed when it goes out of scope.
    let cleanup = Defer::new(|| {
        release_resource(resource);
    });

    // ... do some work ...

    // The closure will be executed automatically when `cleanup` goes out of scope.
}

See also: defer!, and DeferGroup.

Implementations§

Source§

impl<T: FnOnce()> Defer<T>

Source

pub fn new(deferred: T) -> Self

Creates a new Defer instance with the given deferred closure.

The closure will be executed when the Defer instance goes out of scope.

Note: Defer MUST be bound to a variable to function properly; otherwise, it will be dropped immediately, executing the enclosed closure!

§Example
use defer_rs::Defer;

let defer_instance = Defer::new(|| {
    println!("Deferred action executed!");
});

// ... other code ...

// The deferred action will be executed when `defer_instance` goes out of scope.

Trait Implementations§

Source§

impl<T: FnOnce()> Drop for Defer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Defer<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Defer<T>
where T: RefUnwindSafe,

§

impl<T> Send for Defer<T>
where T: Send,

§

impl<T> Sync for Defer<T>
where T: Sync,

§

impl<T> Unpin for Defer<T>
where T: Unpin,

§

impl<T> UnwindSafe for Defer<T>
where T: UnwindSafe,

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.

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.