pub struct Defer<F: FnOnce()> { /* private fields */ }Expand description
A type that calls a function when dropped
This is like the defer keyword found in some languages.
§Examples
use xtk_core::{defer, defer::{Defer}};
let _d1 = defer!(|| println!("Hello!"));
let _d2 = defer!(|| println!("Defer order works, unless manually drop()'d"));
println!("This will print before the deferred code!");Implementations§
Source§impl<F: FnOnce()> Defer<F>
impl<F: FnOnce()> Defer<F>
Sourcepub const fn new(f: F) -> Self
pub const fn new(f: F) -> Self
Construct a Defer
The return value must be stored in a temporary variable so the deferred function will run at the correct time.
Examples found in repository?
examples/defer.rs (line 15)
5fn main() {
6 // defer can take in a closure or function pointer
7 let _d1 = defer!(|| println!("Hello!"));
8
9 // or create a closure from a block
10 let _d2 = defer!({
11 println!("This is another deferred block");
12 });
13
14 // or you can create it using the `Defer` struct
15 let _d3 = Defer::new(|| println!("This is our final deferred code"));
16}Trait Implementations§
Auto Trait Implementations§
impl<F> Freeze for Defer<F>where
F: Freeze,
impl<F> RefUnwindSafe for Defer<F>where
F: RefUnwindSafe,
impl<F> Send for Defer<F>where
F: Send,
impl<F> Sync for Defer<F>where
F: Sync,
impl<F> Unpin for Defer<F>where
F: Unpin,
impl<F> UnsafeUnpin for Defer<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for Defer<F>where
F: 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