mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-29 15:04:32 +01:00
27 lines
531 B
Rust
27 lines
531 B
Rust
|
//! 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()() }
|
||
|
}
|
||
|
}
|