aboutsummaryrefslogtreecommitdiff
path: root/rtic-common/src/dropper.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rtic-common/src/dropper.rs')
-rw-r--r--rtic-common/src/dropper.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/rtic-common/src/dropper.rs b/rtic-common/src/dropper.rs
new file mode 100644
index 0000000..a4b4d15
--- /dev/null
+++ b/rtic-common/src/dropper.rs
@@ -0,0 +1,26 @@
+//! A drop implementation runner.
+
+/// Runs a closure on drop.
+pub struct OnDrop<F: FnOnce()> {
+ f: core::mem::MaybeUninit<F>,
+}
+
+impl<F: FnOnce()> OnDrop<F> {
+ /// Make a new droppper given a closure.
+ pub fn new(f: F) -> Self {
+ Self {
+ f: core::mem::MaybeUninit::new(f),
+ }
+ }
+
+ /// Make it not run drop.
+ pub fn defuse(self) {
+ core::mem::forget(self)
+ }
+}
+
+impl<F: FnOnce()> Drop for OnDrop<F> {
+ fn drop(&mut self) {
+ unsafe { self.f.as_ptr().read()() }
+ }
+}