pub struct Guard<T, F>where
F: FnOnce(T),{ /* private fields */ }Expand description
Wrap a value and run a closure when dropped.
This is useful for quickly creating desructors inline.
§Examples
{
// Create a new guard around a string that will
// print its value when dropped.
let s = String::from("Chashu likes tuna");
let mut s = Guard::new(s, |s| println!("{s}"));
// Modify the string contained in the guard.
s.push_str("!!!");
// The guard will be dropped here.
}Implementations§
Source§impl<T, F> Guard<T, F>where
F: FnOnce(T),
impl<T, F> Guard<T, F>where
F: FnOnce(T),
Sourcepub fn new(inner: T, f: F) -> Self
pub fn new(inner: T, f: F) -> Self
Create a new instance of Guard.
§Example
let value = String::from("Chashu likes tuna");
let guard = Guard::new(value, |s| println!("{s}"));Sourcepub fn into_inner(guard: Self) -> T
pub fn into_inner(guard: Self) -> T
Consumes the Guard, returning the wrapped value.
This will not execute the closure. This is implemented as a static
method to prevent any potential conflicts with any other methods called
into_inner inherited via the Deref and DerefMut impls.
§Example
let value = String::from("Nori likes chicken");
let guard = Guard::new(value, |s| println!("{s}"));
assert_eq!(Guard::into_inner(guard), "Nori likes chicken");Trait Implementations§
Source§impl<T: Ord, F> Ord for Guard<T, F>
impl<T: Ord, F> Ord for Guard<T, F>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialOrd, F> PartialOrd for Guard<T, F>where
F: FnOnce(T) + PartialOrd,
impl<T: PartialOrd, F> PartialOrd for Guard<T, F>where
F: FnOnce(T) + PartialOrd,
impl<T: Eq, F> Eq for Guard<T, F>
impl<T, F> StructuralPartialEq for Guard<T, F>where
F: FnOnce(T),
Auto Trait Implementations§
impl<T, F> Freeze for Guard<T, F>
impl<T, F> RefUnwindSafe for Guard<T, F>where
T: RefUnwindSafe,
F: RefUnwindSafe,
impl<T, F> Send for Guard<T, F>
impl<T, F> Sync for Guard<T, F>
impl<T, F> Unpin for Guard<T, F>
impl<T, F> UnwindSafe for Guard<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