Struct backdrop::thread::TrashThreadStrategy
source · 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?
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
});
});
}
}