[][src]Struct defer_drop::DeferDrop

#[repr(transparent)]pub struct DeferDrop<T: Send + 'static> { /* fields omitted */ }

Wrapper type that, when dropped, sends the inner value to a global background thread to be dropped. Useful in cases where a value takes a long time to drop (for instance, a windows file that might block on close, or a large data structure that has to extensively recursively trawl itself).

DeferDrop implements Deref and DerefMut, meaning it can be dereferenced and freely used like a container around its inner type.

Notes:

Carefully consider whether this pattern is necessary for your use case. Like all worker-thread abstractions, sending the value to a separate thread comes with its own costs, so it should only be done if performance profiling indicates that it's a performance gain.

There is only one global worker thread. Dropped values are enqueued in an unbounded channel to be consumed by this thread; if you produce more garbage than the thread can handle, this will cause unbounded memory consumption. There is currently no way for the thread to signal or block if it is overwhelmed.

All of the standard non-determinism threading caveats apply here. The objects are guaranteed to be destructed in the order received through a channel, which means that objects sent from a single thread will be destructed in order. However, there is no guarantee about the ordering of interleaved values from different threads. Additionally, there are no guarantees about how long the values will be queued before being dropped, or even that they will be dropped at all. If your main thread terminates before all drops could be completed, they will be silently lost (as though via a mem::forget).This behavior is entirely up to your OS's thread scheduler. There is no way to receive a signal indicating when a particular object was dropped.

Example

use defer_drop::DeferDrop;
use std::time::{Instant, Duration};

let massive_vec: Vec<Vec<i32>> = (0..1000000)
    .map(|_| vec![1, 2, 3])
    .collect();

let deferred = DeferDrop::new(massive_vec.clone());

fn timer(f: impl FnOnce()) -> Duration {
    let start = Instant::now();
    f();
    Instant::now() - start
}

let drop1 = timer(move || drop(massive_vec));
let drop2 = timer(move || drop(deferred));

assert!(drop2 < drop1);

Implementations

impl<T: Send + 'static> DeferDrop<T>[src]

pub fn new(value: T) -> Self[src]

Create a new DeferDrop value.

pub fn into_inner(this: Self) -> T[src]

Unwrap the DeferDrop, returning the inner value. This has the effect of cancelling the deferred drop behavior; ownership of the inner value is transferred to the caller.

Trait Implementations

impl<T: Send + 'static> AsMut<T> for DeferDrop<T>[src]

impl<T: Send + 'static> AsRef<T> for DeferDrop<T>[src]

impl<T: Clone + Send + 'static> Clone for DeferDrop<T>[src]

impl<T: Debug + Send + 'static> Debug for DeferDrop<T>[src]

impl<T: Default + Send + 'static> Default for DeferDrop<T>[src]

impl<T: Send + 'static> Deref for DeferDrop<T>[src]

type Target = T

The resulting type after dereferencing.

impl<T: Send + 'static> DerefMut for DeferDrop<T>[src]

impl<T: Send + 'static> Drop for DeferDrop<T>[src]

impl<T: Eq + Send + 'static> Eq for DeferDrop<T>[src]

impl<T: Send + 'static> From<T> for DeferDrop<T>[src]

impl<T: Hash + Send + 'static> Hash for DeferDrop<T>[src]

impl<T: Ord + Send + 'static> Ord for DeferDrop<T>[src]

impl<T: PartialEq + Send + 'static> PartialEq<DeferDrop<T>> for DeferDrop<T>[src]

impl<T: PartialOrd + Send + 'static> PartialOrd<DeferDrop<T>> for DeferDrop<T>[src]

impl<T: Send + 'static> StructuralEq for DeferDrop<T>[src]

impl<T: Send + 'static> StructuralPartialEq for DeferDrop<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for DeferDrop<T> where
    T: RefUnwindSafe

impl<T> Send for DeferDrop<T>

impl<T> Sync for DeferDrop<T> where
    T: Sync

impl<T> Unpin for DeferDrop<T> where
    T: Unpin

impl<T> UnwindSafe for DeferDrop<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.