pub struct TrashThreadStrategy();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
impl TrashThreadStrategy
Sourcepub fn with_trash_thread<R>(fun: impl FnOnce() -> R) -> R
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?
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}