capturing_inner_function/
capturing_inner_function.rs

1use std::{
2    sync::{Arc, Mutex},
3    thread,
4    time::Duration,
5};
6
7fn main() {
8    // The data shared with the given callback has to be thread-safe.
9    // https://doc.rust-lang.org/nomicon/send-and-sync.html
10    let counter = Arc::new(Mutex::new(2));
11    let cancel_first_option: Arc<Mutex<Option<Box<dyn Fn() + Send + 'static>>>> =
12        Arc::new(Mutex::new(None));
13    let cancel_first_option_clone = cancel_first_option.clone();
14
15    let on_timeout = move || {
16        let mut c = counter.lock().unwrap();
17        *c -= 1;
18        println!("{}", c);
19        if *c == 0 {
20            cancel_first_option_clone.lock().unwrap().as_ref().unwrap()();
21            println!("The first instance of spawn_timeout has been succesfully stopped");
22        }
23    };
24
25    // Leaking this inner function to make its lifetime as 'static.
26    // https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak
27    let static_on_timeout = Box::leak(Box::new(on_timeout));
28
29    let cancel_first = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
30    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(2));
31    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(1));
32
33    *cancel_first_option.lock().unwrap() = Some(cancel_first);
34
35    // Sleeping for a long time for the sake of this example.
36    thread::sleep(Duration::from_millis(u64::MAX));
37}