Skip to main content

Defer

Struct Defer 

Source
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>

Source

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§

Source§

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> 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.