1use crate::prelude::*;
8
9#[must_use = "This deferred function runs immediately. Assign it to a guard to run it at the end of the block: `let _guard = defer(…);`"]
11pub struct Deferred<F>(ManuallyDrop<F>)
12where
13 F: FnOnce();
14
15pub fn defer<F>(func: F) -> Deferred<F>
17where
18 F: FnOnce(),
19{
20 Deferred(ManuallyDrop::new(func))
21}
22
23impl<F> Drop for Deferred<F>
24where
25 F: FnOnce(),
26{
27 fn drop(&mut self) {
28 let func = unsafe { ManuallyDrop::take(&mut self.0) };
29
30 func();
31 }
32}