Skip to content

Instantly share code, notes, and snippets.

@Emilgardis
Created December 29, 2022 00:33
Show Gist options
  • Save Emilgardis/4f360f035a4d869669f17b5af9c7500a to your computer and use it in GitHub Desktop.
Save Emilgardis/4f360f035a4d869669f17b5af9c7500a to your computer and use it in GitHub Desktop.
defer
fn defer<F: FnOnce()>(f: F) -> impl Drop {
struct D<F: FnOnce()>(Option<F>);
impl<F: FnOnce()> Drop for D<F> {
fn drop(&mut self) {
if let Some(f) = self.0.take() {
f()
}
}
}
D(Some(f))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment