sim_lib_control/
unwind.rs1#[derive(Clone, Debug, PartialEq, Eq)]
3pub enum Unwind<R, B, C, E> {
4 Return(R),
6 Break(B),
8 Continue(C),
10 Exception(E),
12 Cancelled,
14 Closed,
16}
17
18type Cleanup<U> = Box<dyn FnOnce(&U)>;
20
21pub struct CleanupStack<U> {
23 cleanups: Vec<Cleanup<U>>,
24}
25
26impl<U> Default for CleanupStack<U> {
27 fn default() -> Self {
28 Self {
29 cleanups: Vec::new(),
30 }
31 }
32}
33
34impl<U> CleanupStack<U> {
35 pub fn new() -> Self {
37 Self::default()
38 }
39
40 pub fn push(&mut self, cleanup: impl FnOnce(&U) + 'static) {
42 self.cleanups.push(Box::new(cleanup));
43 }
44
45 pub fn unwind(mut self, reason: U) -> U {
47 while let Some(cleanup) = self.cleanups.pop() {
48 cleanup(&reason);
49 }
50 reason
51 }
52}