dark_std/defer/
mod.rs

1pub struct Guard<F: FnMut()>(pub Option<F>);
2
3impl<F: FnMut()> Drop for Guard<F> {
4    fn drop(&mut self) {
5        if let Some(mut f) = (self.0).take() {
6            f()
7        }
8    }
9}
10
11/// Defers evaluation of a block of code until the end of the scope.
12/// Sort of LIFO(last-in, first-out queue)
13/// If you encounter references to resources that cannot mut more than 2 times, try nesting RefCell and then use.borrow() and.borrow_mut().
14///
15/// for example:
16/// ```
17///  use dark_std::defer;
18///  //LIFO, so it will print: guard: 3  guard: 2   guard: 1
19///  fn main(){
20///     defer!({
21///        println!("guard: 1");
22///        });
23///     defer!(||{
24///        println!("guard: 2");
25///        });
26///     defer!{
27///        println!("guard: 3");
28///     }
29/// }
30/// ```
31///
32///
33#[macro_export]
34macro_rules! defer {
35    ($func:block) => {
36       let _guard = $crate::defer::Guard(Some( ||$func));
37    };
38    ($func:expr) => {
39        let _guard = $crate::defer::Guard(Some($func));
40    };
41    { $($func:expr$(;)?)+ } => {
42       let _guard = $crate::defer::Guard(Some( ||{$($func;)+}));
43    }
44}
45
46#[cfg(test)]
47mod test {
48    #[test]
49    fn test_defer() {
50        defer!(|| {
51            println!("defer");
52        });
53    }
54}