diff options
| author | Oleksandr Babak <alexanderbabak@proton.me> | 2025-05-15 14:03:21 +0200 |
|---|---|---|
| committer | Henrik Tjäder <henrik@tjaders.com> | 2025-06-15 09:03:30 +0000 |
| commit | ff3b011cef0ca6e5f77cb1112940337e913ca957 (patch) | |
| tree | fe2fcb59673738f00b9d7d1c3998d2326b15c26a /rtic-common | |
| parent | aa4f4ddac84fc56ca517abc18906c95a2f49f395 (diff) | |
feat: `OnDropWith` helper
Diffstat (limited to 'rtic-common')
| -rw-r--r-- | rtic-common/src/dropper.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/rtic-common/src/dropper.rs b/rtic-common/src/dropper.rs index a4b4d15..37c289d 100644 --- a/rtic-common/src/dropper.rs +++ b/rtic-common/src/dropper.rs @@ -1,5 +1,9 @@ //! A drop implementation runner. +use core::ops::{Deref, DerefMut}; + +pub(crate) struct OnDropWith<T, F: FnMut(&mut T)>(T, F); + /// Runs a closure on drop. pub struct OnDrop<F: FnOnce()> { f: core::mem::MaybeUninit<F>, @@ -24,3 +28,33 @@ impl<F: FnOnce()> Drop for OnDrop<F> { unsafe { self.f.as_ptr().read()() } } } + +impl<T, F: FnMut(&mut T)> OnDropWith<T, F> { + pub(crate) fn new(value: T, f: F) -> Self { + Self(value, f) + } + + pub(crate) fn execute(&mut self) { + (self.1)(&mut self.0); + } +} + +impl<T, F: FnMut(&mut T)> Deref for OnDropWith<T, F> { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<T, F: FnMut(&mut T)> DerefMut for OnDropWith<T, F> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl<T, F: FnMut(&mut T)> Drop for OnDropWith<T, F> { + fn drop(&mut self) { + self.execute(); + } +} |
