Struct DeferGroup

Source
pub struct DeferGroup<'a>(/* private fields */);
Expand description

A utility struct for explicitly scoped deferred execution of closures.

The DeferGroup allows you to add closures (functions) that will be executed when the DeferGroup instance goes out of scope. It is particularly useful for resource cleanup or deferred actions.

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

§Example

use defer_rs::DeferGroup;

let mut defer_group = DeferGroup::new();

// Add a function to be executed when `defer_group` goes out of scope
defer_group.add(Box::new(|| {
    println!("Deferred action: Cleaning up resources...");
}));

// Some other code...

// The deferred (queued) actions will be executed here, when the `defer_group` is dropped.

See also: defer_scope!, defer_scope_init!, Defer, and defer!.

Implementations§

Source§

impl<'a> DeferGroup<'a>

Source

pub fn new() -> Self

Creates a new DeferGroup.

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

§Example
use defer_rs::DeferGroup;

let mut defer_group = DeferGroup::new();
// Add deferred actions...
Source

pub fn add(&mut self, f: Box<dyn FnOnce() + 'a>)

Adds a deferred closure to the start (0-index) of the DeferGroup queue.

The closures queued in DeferGroup will be executed first to last when the the DeferGroup instance goes out of scope.

§Example
use defer_rs::DeferGroup;

let mut defer_group = DeferGroup::new();
{
    defer_group.add(Box::new(|| {
        println!("This will be printed 2nd");        
    }));
    defer_group.add(Box::new(|| {
        println!("This will be printed 1st");
    }));
}
Source

pub fn push(&mut self, f: Box<dyn FnOnce() + 'a>)

Pushes a deferred closure to the end of the DeferGroup queue.

The closures queued in DeferGroup will be executed first to last when the the DeferGroup instance goes out of scope.

§Example
use defer_rs::DeferGroup;

let mut defer_group = DeferGroup::new();
{
    defer_group.push(Box::new(|| {
        println!("This will be printed 1st");
    }));
    defer_group.push(Box::new(|| {
        println!("This will be printed 2nd");        
    }));
}    

Trait Implementations§

Source§

impl<'a> Drop for DeferGroup<'a>

Source§

fn drop(self: &mut DeferGroup<'a>)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for DeferGroup<'a>

§

impl<'a> !RefUnwindSafe for DeferGroup<'a>

§

impl<'a> !Send for DeferGroup<'a>

§

impl<'a> !Sync for DeferGroup<'a>

§

impl<'a> Unpin for DeferGroup<'a>

§

impl<'a> !UnwindSafe for DeferGroup<'a>

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.