1use std::fmt;
2
3#[derive(Default)]
4pub struct Exit {
5 exited: bool,
6 exits: Vec<Box<dyn FnOnce() + Send + Sync>>,
7}
8
9impl Exit {
10 pub fn register_exit(&mut self, exit: Box<dyn FnOnce() + Send + Sync>) {
11 if self.exited {
12 exit();
13 } else {
14 self.exits.push(exit);
15 }
16 }
17
18 pub fn exit(&mut self) {
19 self.exited = true;
20 for exit in self.exits.drain(..) {
21 exit();
22 }
23 }
24}
25
26impl fmt::Debug for Exit {
27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28 write!(f, "{} exits", self.exits.len())
29 }
30}