pub struct DropGuard<T, F>where
F: FnOnce(T),{
inner: ManuallyDrop<T>,
f: ManuallyDrop<F>,
}🔬This is a nightly-only experimental API. (
drop_guard #144426)Expand description
Wrap a value and run a closure when dropped.
This is useful for quickly creating destructors inline.
§Examples
#![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>🔬This is a nightly-only experimental API. (
§drop_guard #144426)f: ManuallyDrop<F>🔬This is a nightly-only experimental API. (
drop_guard #144426)Implementations§
Source§impl<T, F> DropGuard<T, F>where
F: FnOnce(T),
impl<T, F> DropGuard<T, F>where
F: FnOnce(T),
Sourcepub const fn new(inner: T, f: F) -> Self
🔬This is a nightly-only experimental API. (drop_guard #144426)
pub const fn new(inner: T, f: F) -> Self
drop_guard #144426)Create a new instance of DropGuard.
§Example
Sourcepub fn into_inner(guard: Self) -> T
🔬This is a nightly-only experimental API. (drop_guard #144426)
pub fn into_inner(guard: Self) -> T
drop_guard #144426)Consumes the DropGuard, returning the wrapped value.
This will not execute the closure. This is implemented as an associated
function to prevent any potential conflicts with any other methods called
into_inner from the Deref and DerefMut impls.
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
Trait Implementations§
Auto Trait Implementations§
impl<T, F> Freeze for DropGuard<T, F>
impl<T, F> RefUnwindSafe for DropGuard<T, F>where
T: RefUnwindSafe,
F: RefUnwindSafe,
impl<T, F> Send for DropGuard<T, F>
impl<T, F> Sync for DropGuard<T, F>
impl<T, F> Unpin for DropGuard<T, F>
impl<T, F> UnsafeUnpin for DropGuard<T, F>where
T: UnsafeUnpin,
F: UnsafeUnpin,
impl<T, F> UnwindSafe for DropGuard<T, F>where
T: UnwindSafe,
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
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)] const SIZE: usize = _
#[doc(hidden)] const SIZE: usize = _
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)] const ALIGN: usize = _
#[doc(hidden)] const ALIGN: usize = _
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)] const IS_ZST: bool = _
#[doc(hidden)] const IS_ZST: bool = _
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)] const LAYOUT: Layout = _
#[doc(hidden)] const LAYOUT: Layout = _
🔬This is a nightly-only experimental API. (
sized_type_properties)Source§#[doc(hidden)] const MAX_SLICE_LEN: usize = _
#[doc(hidden)] const MAX_SLICE_LEN: usize = _
🔬This is a nightly-only experimental API. (
sized_type_properties)The largest safe length for a
[Self]. Read more