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