1use backdrop::*;
2
3fn time(name: &'static str, f: impl FnOnce()) {
4 let start = std::time::Instant::now();
5 f();
6 let end = std::time::Instant::now();
7 println!("{name}, took {:?}", end.duration_since(start));
8}
9
10const LEN: usize = 5_000_000;
11
12fn setup() -> Box<[Box<str>]> {
13 (0..LEN)
14 .map(|x| x.to_string().into_boxed_str())
15 .collect::<Vec<_>>()
16 .into_boxed_slice()
17}
18
19fn main() {
20 let boxed = setup();
21 let not_backdropped = boxed.clone();
22 time("none", move || {
23 assert_eq!(not_backdropped.len(), LEN);
24 });
26
27 let backdropped: TrivialBackdrop<_> = Backdrop::new(boxed.clone());
28 time("fake backdrop", move || {
29 assert_eq!(backdropped.len(), LEN);
30 });
32
33 let backdropped: thread::ThreadBackdrop<_> = Backdrop::new(boxed.clone());
34 time("thread backdrop", move || {
35 assert_eq!(backdropped.len(), LEN);
36 });
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 });
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 });
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 });
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 });
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 });
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 });
98 });
99 }
100}
101
102