2020-09-22 18:03:43 +02:00
|
|
|
use core::mem;
|
2021-09-11 01:53:53 +02:00
|
|
|
use core::mem::MaybeUninit;
|
|
|
|
|
|
|
|
pub struct OnDrop<F: FnOnce()> {
|
|
|
|
f: MaybeUninit<F>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: FnOnce()> OnDrop<F> {
|
|
|
|
pub fn new(f: F) -> Self {
|
2022-06-12 22:15:44 +02:00
|
|
|
Self { f: MaybeUninit::new(f) }
|
2021-09-11 01:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn defuse(self) {
|
|
|
|
mem::forget(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: FnOnce()> Drop for OnDrop<F> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { self.f.as_ptr().read()() }
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2021-03-24 20:36:02 +01:00
|
|
|
/// An explosive ordinance that panics if it is improperly disposed of.
|
|
|
|
///
|
|
|
|
/// This is to forbid dropping futures, when there is absolutely no other choice.
|
|
|
|
///
|
|
|
|
/// To correctly dispose of this device, call the [defuse](struct.DropBomb.html#method.defuse)
|
|
|
|
/// method before this object is dropped.
|
2020-09-22 18:03:43 +02:00
|
|
|
pub struct DropBomb {
|
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DropBomb {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { _private: () }
|
|
|
|
}
|
2021-03-24 21:21:32 +01:00
|
|
|
|
2022-01-14 12:48:38 +01:00
|
|
|
/// Defuses the bomb, rendering it safe to drop.
|
2020-09-22 18:03:43 +02:00
|
|
|
pub fn defuse(self) {
|
|
|
|
mem::forget(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for DropBomb {
|
|
|
|
fn drop(&mut self) {
|
2020-11-01 17:17:24 +01:00
|
|
|
panic!("boom")
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
|
|
|
}
|