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>
impl<T: FnOnce()> Defer<T>
Sourcepub fn new(deferred: T) -> Self
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more