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