[−][src]Struct defer_drop::DeferDrop
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]
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]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T: Ord + Send + 'static> Ord for DeferDrop<T>
[src]
fn cmp(&self, other: &DeferDrop<T>) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[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]
fn partial_cmp(&self, other: &DeferDrop<T>) -> Option<Ordering>
[src]
fn lt(&self, other: &DeferDrop<T>) -> bool
[src]
fn le(&self, other: &DeferDrop<T>) -> bool
[src]
fn gt(&self, other: &DeferDrop<T>) -> bool
[src]
fn ge(&self, other: &DeferDrop<T>) -> bool
[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,
T: RefUnwindSafe,
impl<T> Send for DeferDrop<T>
impl<T> Sync for DeferDrop<T> where
T: Sync,
T: Sync,
impl<T> Unpin for DeferDrop<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for DeferDrop<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[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]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,