Struct DropGuard

pub struct DropGuard<T, F>
where
    F: FnOnce(T), { pub(in ::mem::drop_guard) inner: ManuallyDrop<T>, pub(in ::mem::drop_guard) f: ManuallyDrop<F> }

Wrap a value and run a closure when dropped.

This is useful for quickly creating destructors inline.

Examples

# #![allow(unused)]
#![feature(drop_guard)]

use std::mem::DropGuard;

{
    // Create a new guard around a string that will
    // print its value when dropped.
    let s = String::from("Chashu likes tuna");
    let mut s = DropGuard::new(s, |s| println!("{s}"));

    // Modify the string contained in the guard.
    s.push_str("!!!");

    // The guard will be dropped here, printing:
    // "Chashu likes tuna!!!"
}

Fields

inner: ManuallyDrop<T>
f: ManuallyDrop<F>

Implementations

impl<T, F> DropGuard<T, F> where F: FnOnce(T),

const fn new(inner: T, f: F) -> Self

Create a new instance of DropGuard.

Example

# #![allow(unused)]
#![feature(drop_guard)]

use std::mem::DropGuard;

let value = String::from("Chashu likes tuna");
let guard = DropGuard::new(value, |s| println!("{s}"));
const fn dismiss(guard: Self) -> T
where
    F: ,

Consumes the DropGuard, returning the wrapped value.

This will not execute the closure. It is typically preferred to call this function instead of mem::forget because it will return the stored value and drop variables captured by the closure instead of leaking their owned resources.

Example

# #![allow(unused)]
#![feature(drop_guard)]

use std::mem::DropGuard;

let value = String::from("Nori likes chicken");
let guard = DropGuard::new(value, |s| println!("{s}"));
assert_eq!(DropGuard::dismiss(guard), "Nori likes chicken");

Trait Implementations

impl<T, F> Debug for DropGuard<T, F> where T: Debug, F: FnOnce(T),

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

type Target = T;
fn deref(&self) -> &T

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

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

impl<T, F> Drop for DropGuard<T, F> where F: ~const FnOnce(T),

fn drop(&mut self)

Auto Trait Implementations

impl<T, F> Freeze for DropGuard<T, F> where ManuallyDrop<T>: Freeze, ManuallyDrop<F>: Freeze,

impl<T, F> RefUnwindSafe for DropGuard<T, F> where ManuallyDrop<T>: RefUnwindSafe, ManuallyDrop<F>: RefUnwindSafe,

impl<T, F> Send for DropGuard<T, F> where ManuallyDrop<T>: Send, ManuallyDrop<F>: Send,

impl<T, F> Sync for DropGuard<T, F> where ManuallyDrop<T>: Sync, ManuallyDrop<F>: Sync,

impl<T, F> Unpin for DropGuard<T, F> where ManuallyDrop<T>: Unpin, ManuallyDrop<F>: Unpin,

impl<T, F> UnsafeUnpin for DropGuard<T, F> where ManuallyDrop<T>: UnsafeUnpin, ManuallyDrop<F>: UnsafeUnpin,

impl<T, F> UnwindSafe for DropGuard<T, F> where ManuallyDrop<T>: UnwindSafe, ManuallyDrop<F>: UnwindSafe,

Blanket Implementations

impl<P, T> Receiver for DropGuard<T, F> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<T> Any for DropGuard<T, F> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for DropGuard<T, F> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for DropGuard<T, F> where T: ?Sized,

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

impl<T> From<T> for DropGuard<T, F>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for DropGuard<T, F> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for DropGuard<T, F>

impl<T, U> Into<U> for DropGuard<T, F> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for DropGuard<T, F> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for DropGuard<T, F> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>