1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! The contents of this module are only available when the `tokio` feature is enabled.
use crate::{Backdrop, BackdropStrategy};

/// Strategy which spawns a new tokio task which drops the contained value.
///
/// This only works within the context of a Tokio runtime.
/// (Dropping objects constructed with this strategy while no Tokio runtime is available
/// will result in a panic!)
///
/// Since the overhead of creating new Tokio tasks is very small, this is really fast
/// (at least from the perspective of the current task.)
///
/// Note that if dropping your value takes a very long time, you might be better off
/// using [`TokioBlockingTaskStrategy`] instead. Benchmark!
pub struct TokioTaskStrategy();
impl<T: Send + 'static> BackdropStrategy<T> for TokioTaskStrategy {
    #[inline]
    fn execute(droppable: T) {
        tokio::task::spawn(async move {
            core::mem::drop(droppable);
        });
    }
}

/// Convenient alias for a [`Backdrop`] that uses the [`TokioTaskStrategy`]
pub type TokioTaskBackdrop<T> = Backdrop<T, TokioTaskStrategy>;

/// Strategy which spawns a new 'blocking' tokio task which drops the contained value.
///
/// This only works within the context of a Tokio runtime.
/// (Dropping objects constructed with this strategy while no Tokio runtime is available
/// will result in a panic!)
///
/// This strategy is similar to [`TokioTaskStrategy`] but uses [`tokio::task::spawn_blocking`]
/// instead. This makes sure that the 'fast async tasks' thread pool can continue its normal work,
/// because the drop work is passed to the 'ok to block here' thread pool.
///
/// Benchmark to find out which approach suits your scenario better!
pub struct TokioBlockingTaskStrategy();
impl<T: Send + 'static> BackdropStrategy<T> for TokioBlockingTaskStrategy {
    #[inline]
    fn execute(droppable: T) {
        tokio::task::spawn_blocking(move || core::mem::drop(droppable));
    }
}

/// Convenient alias for a [`Backdrop`] that uses the [`TokioBlockingTaskStrategy`]
pub type TokioBlockingTaskBackdrop<T> = Backdrop<T, TokioBlockingTaskStrategy>;