Struct kernel::types::ScopeGuard

source ·
pub struct ScopeGuard<T, F: FnOnce(T)>(/* private fields */);
Expand description

Runs a cleanup function/closure when dropped.

The ScopeGuard::dismiss function prevents the cleanup function from running.

Examples

In the example below, we have multiple exit paths and we want to log regardless of which one is taken:

fn example1(arg: bool) {
    let _log = ScopeGuard::new(|| pr_info!("example1 completed\n"));

    if arg {
        return;
    }

    pr_info!("Do something...\n");
}

In the example below, we want to log the same message on all early exits but a different one on the main exit path:

fn example2(arg: bool) {
    let log = ScopeGuard::new(|| pr_info!("example2 returned early\n"));

    if arg {
        return;
    }

    // (Other early returns...)

    log.dismiss();
    pr_info!("example2 no early return\n");
}

In the example below, we need a mutable object (the vector) to be accessible within the log function, so we wrap it in the ScopeGuard:

fn example3(arg: bool) -> Result {
    let mut vec =
        ScopeGuard::new_with_data(Vec::new(), |v| pr_info!("vec had {} elements\n", v.len()));

    vec.try_push(10u8)?;
    if arg {
        return Ok(());
    }
    vec.try_push(20u8)?;
    Ok(())
}

Invariants

The value stored in the struct is nearly always Some(_), except between ScopeGuard::dismiss and ScopeGuard::drop: in this case, it will be None as the value will have been returned to the caller. Since ScopeGuard::dismiss consumes the guard, callers won’t be able to use it anymore.

Implementations§

source§

impl<T, F: FnOnce(T)> ScopeGuard<T, F>

source

pub fn new_with_data(data: T, cleanup_func: F) -> Self

Creates a new guarded object wrapping the given data and with the given cleanup function.

source

pub fn dismiss(self) -> T

Prevents the cleanup function from running and returns the guarded data.

source§

impl ScopeGuard<(), fn(_: ())>

source

pub fn new(cleanup: impl FnOnce()) -> ScopeGuard<(), impl FnOnce(())>

Creates a new guarded object with the given cleanup function.

Trait Implementations§

source§

impl<T, F: FnOnce(T)> Deref for ScopeGuard<T, F>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T, F: FnOnce(T)> DerefMut for ScopeGuard<T, F>

source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
source§

impl<T, F: FnOnce(T)> Drop for ScopeGuard<T, F>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, F> RefUnwindSafe for ScopeGuard<T, F>where F: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, F> Send for ScopeGuard<T, F>where F: Send, T: Send,

§

impl<T, F> Sync for ScopeGuard<T, F>where F: Sync, T: Sync,

§

impl<T, F> Unpin for ScopeGuard<T, F>where F: Unpin, T: Unpin,

§

impl<T, F> UnwindSafe for ScopeGuard<T, F>where F: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, E> Init<T, E> for T

source§

unsafe fn __init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
source§

impl<T, U> Into<U> for Twhere 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, E> PinInit<T, E> for T

source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.