ch32_hal/internal/
drop.rs1#![allow(unused)]
2use core::mem;
4use core::mem::MaybeUninit;
5
6#[must_use = "to delay the drop handler invocation to the end of the scope"]
8pub struct OnDrop<F: FnOnce()> {
9 f: MaybeUninit<F>,
10}
11
12impl<F: FnOnce()> OnDrop<F> {
13 pub fn new(f: F) -> Self {
15 Self { f: MaybeUninit::new(f) }
16 }
17
18 pub fn defuse(self) {
20 mem::forget(self)
21 }
22}
23
24impl<F: FnOnce()> Drop for OnDrop<F> {
25 fn drop(&mut self) {
26 unsafe { self.f.as_ptr().read()() }
27 }
28}
29
30#[must_use = "to delay the drop bomb invokation to the end of the scope"]
37pub struct DropBomb {
38 _private: (),
39}
40
41impl DropBomb {
42 pub fn new() -> Self {
44 Self { _private: () }
45 }
46
47 pub fn defuse(self) {
49 mem::forget(self)
50 }
51}
52
53impl Drop for DropBomb {
54 fn drop(&mut self) {
55 panic!("boom")
56 }
57}