DropGuard

Struct DropGuard 

Source
pub struct DropGuard<T, F: FnMut(T)> { /* private fields */ }
Expand description

The DropGuard will remain to Send and Sync from T.

§Examples

The LinkedList<T> is Send. So the DropGuard will be too, but it will not be Sync:

use drop_guard::guard;
use std::collections::LinkedList;
use std::thread;

let list: LinkedList<u32> = LinkedList::new();

let a_list = guard(list, |_| {});

// Send the guarded list to another thread
thread::spawn(move || {
    assert_eq!(0, a_list.len());
}).join();

Implementations§

Source§

impl<T: Sized, F: FnMut(T)> DropGuard<T, F>

Source

pub fn new(data: T, func: F) -> DropGuard<T, F>

👎Deprecated: use drop_guard::guard that is shorter

Creates a new guard taking in your data and a function.

use drop_guard::guard;

let s = String::from("a commonString");
let mut s = guard(s, |final_string| println!("s became {} at last", final_string));

// much code and time passes by ...
*s = "a rainbow".to_string();

// by the end of this function the String will have become a rainbow

Trait Implementations§

Source§

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

Use the captured value.

use drop_guard::guard;

let val = guard(42usize, |_| {});
assert_eq!(42, *val);
Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

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

Modify the captured value.

use drop_guard::guard;

let mut val = guard(vec![2, 3, 4], |_| {});
assert_eq!(3, val.len());

val.push(5);
assert_eq!(4, val.len());
Source§

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

Mutably dereferences the value.
Source§

impl<T, F: FnMut(T)> Drop for DropGuard<T, F>

React to dropping the value. In this example we measure the time the value is alive.

use drop_guard::guard;
use std::time::Instant;

let start_time = Instant::now();
let val = guard(42usize, |_| {
    let time_alive = start_time.elapsed();
    println!("value lived for {}ns", time_alive.subsec_nanos())
});
assert_eq!(42, *val);
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, F> UnwindSafe for DropGuard<T, F>
where T: UnwindSafe, 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.