Created
December 29, 2022 00:33
-
-
Save Emilgardis/4f360f035a4d869669f17b5af9c7500a to your computer and use it in GitHub Desktop.
defer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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