nativeshell/shell/
handle.rs

1// Opaque handle for keeping a resource alive while handle exists
2pub struct Handle {
3    on_cancel: Option<Box<dyn FnOnce()>>,
4}
5
6impl Handle {
7    pub fn new<F>(on_cancel: F) -> Self
8    where
9        F: FnOnce() + 'static,
10    {
11        Self {
12            on_cancel: Some(Box::new(on_cancel)),
13        }
14    }
15
16    pub fn cancel(&mut self) {
17        if let Some(on_cancel) = self.on_cancel.take() {
18            on_cancel();
19        }
20    }
21
22    pub fn detach(&mut self) {
23        self.on_cancel.take();
24    }
25}
26
27impl Drop for Handle {
28    fn drop(&mut self) {
29        self.cancel();
30    }
31}