Struct TrashThreadStrategy

Source
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)
19fn main() {
20    let boxed = setup();
21    let not_backdropped = boxed.clone();
22    time("none", move || {
23        assert_eq!(not_backdropped.len(), LEN);
24        // Destructor runs here
25    });
26
27    let backdropped: TrivialBackdrop<_> = Backdrop::new(boxed.clone());
28    time("fake backdrop", move || {
29        assert_eq!(backdropped.len(), LEN);
30        // Destructor runs here
31    });
32
33    let backdropped: thread::ThreadBackdrop<_> = Backdrop::new(boxed.clone());
34    time("thread backdrop", move || {
35        assert_eq!(backdropped.len(), LEN);
36        // Destructor runs here
37    });
38
39    TrashThreadStrategy::with_trash_thread(||{
40        let backdropped: thread::TrashThreadBackdrop<_> = Backdrop::new(boxed.clone());
41        time("trash thread backdrop", move || {
42            assert_eq!(backdropped.len(), LEN);
43            // Destructor runs here
44        });
45    });
46
47    TrashQueueStrategy::ensure_initialized();
48    let backdropped = Backdrop::<_, TrashQueueStrategy>::new(boxed.clone());
49    time("(single threaded) trash queue backdrop", move || {
50        assert_eq!(backdropped.len(), LEN);
51        // Destructor runs here
52    });
53
54    time("(single threaded) trash queue backdrop (actually cleaning up later)", move || {
55        TrashQueueStrategy::cleanup_all();
56    });
57
58    #[cfg(miri)]
59    {
60        println!("Skipping Tokio examples when running on Miri, since it does not support Tokio yet");
61    }
62    #[cfg(not(miri))]
63    {
64    ::tokio::runtime::Builder::new_multi_thread()
65        .enable_all()
66        .build()
67        .unwrap()
68        .block_on(async {
69            let backdropped: crate::tokio::TokioTaskBackdrop<_> = Backdrop::new(boxed.clone());
70            time("tokio task (multithread runner)", move || {
71                assert_eq!(backdropped.len(), LEN);
72                // Destructor runs here
73            });
74
75            let backdropped: crate::tokio::TokioBlockingTaskBackdrop<_> = Backdrop::new(boxed.clone());
76            time("tokio blocking task (multithread runner)", move || {
77                assert_eq!(backdropped.len(), LEN);
78                // Destructor runs here
79            });
80        });
81
82    ::tokio::runtime::Builder::new_current_thread()
83        .enable_all()
84        .build()
85        .unwrap()
86        .block_on(async {
87            let backdropped: crate::tokio::TokioTaskBackdrop<_> = Backdrop::new(setup());
88            time("tokio task (current thread runner)", move || {
89                assert_eq!(backdropped.len(), LEN);
90                // Destructor runs here
91            });
92
93            let backdropped: crate::tokio::TokioBlockingTaskBackdrop<_> = Backdrop::new(setup());
94            time("tokio blocking task (current thread runner)", move || {
95                assert_eq!(backdropped.len(), LEN);
96                // Destructor runs here
97            });
98        });
99    }
100}

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

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 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<F, W, T, D> Deserialize<With<T, W>, D> for F
where 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

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<T> Pointee for T

Source§

type Metadata = ()

The type for metadata in pointers and references to Self.
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.