pub struct TrashThreadStrategy();
Available on crate feature std only.
Expand description

Strategy which sends any to-be-dropped values to a dedicated ‘trash thread’

This thread must have been started first using TrashThreadStrategy::with_trash_thread.

This strategy is similar to the GlobalTrashThreadStrategy, but it makes sure that the trash thread is cleaned up properly (and any yet-to-be-dropped objects contained on it dropped) on exit.

Panics

If Backdrop objects using this strategy are dropped without the thread having been started, i.e. outside of the context of TrashThreadStrategy::with_trash_thread, a panic will happen.

Implementations§

source§

impl TrashThreadStrategy

source

pub fn with_trash_thread<R>(fun: impl FnOnce() -> R) -> R

Starts a new Trash Thread, calls the given function or closure while it is alive, and afterwards cleans up the Trash Thread again.

You probably want to e.g. surround the contents of your main or other ‘big work’ function with this.

This function is reentrant: Nesting it will start a second (third, etc.) trash thread which will be used for the duration of the nested call.

Examples found in repository?
examples/comparison.rs (lines 39-45)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
fn main() {
    let boxed = setup();
    let not_backdropped = boxed.clone();
    time("none", move || {
        assert_eq!(not_backdropped.len(), LEN);
        // Destructor runs here
    });

    let backdropped: TrivialBackdrop<_> = Backdrop::new(boxed.clone());
    time("fake backdrop", move || {
        assert_eq!(backdropped.len(), LEN);
        // Destructor runs here
    });

    let backdropped: thread::ThreadBackdrop<_> = Backdrop::new(boxed.clone());
    time("thread backdrop", move || {
        assert_eq!(backdropped.len(), LEN);
        // Destructor runs here
    });

    TrashThreadStrategy::with_trash_thread(||{
        let backdropped: thread::TrashThreadBackdrop<_> = Backdrop::new(boxed.clone());
        time("trash thread backdrop", move || {
            assert_eq!(backdropped.len(), LEN);
            // Destructor runs here
        });
    });

    TrashQueueStrategy::ensure_initialized();
    let backdropped = Backdrop::<_, TrashQueueStrategy>::new(boxed.clone());
    time("(single threaded) trash queue backdrop", move || {
        assert_eq!(backdropped.len(), LEN);
        // Destructor runs here
    });

    time("(single threaded) trash queue backdrop (actually cleaning up later)", move || {
        TrashQueueStrategy::cleanup_all();
    });

    #[cfg(miri)]
    {
        println!("Skipping Tokio examples when running on Miri, since it does not support Tokio yet");
    }
    #[cfg(not(miri))]
    {
    ::tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let backdropped: crate::tokio::TokioTaskBackdrop<_> = Backdrop::new(boxed.clone());
            time("tokio task (multithread runner)", move || {
                assert_eq!(backdropped.len(), LEN);
                // Destructor runs here
            });

            let backdropped: crate::tokio::TokioBlockingTaskBackdrop<_> = Backdrop::new(boxed.clone());
            time("tokio blocking task (multithread runner)", move || {
                assert_eq!(backdropped.len(), LEN);
                // Destructor runs here
            });
        });

    ::tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            let backdropped: crate::tokio::TokioTaskBackdrop<_> = Backdrop::new(setup());
            time("tokio task (current thread runner)", move || {
                assert_eq!(backdropped.len(), LEN);
                // Destructor runs here
            });

            let backdropped: crate::tokio::TokioBlockingTaskBackdrop<_> = Backdrop::new(setup());
            time("tokio blocking task (current thread runner)", move || {
                assert_eq!(backdropped.len(), LEN);
                // Destructor runs here
            });
        });
    }
}

Trait Implementations§

source§

impl<T: Send + 'static> BackdropStrategy<T> for TrashThreadStrategy

source§

fn execute(droppable: T)

Called whenever T should be dropped. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

source§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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.

§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.