aboutsummaryrefslogtreecommitdiff
path: root/embassy-hal-common
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-hal-common')
-rw-r--r--embassy-hal-common/src/drop.rs51
-rw-r--r--embassy-hal-common/src/lib.rs1
-rw-r--r--embassy-hal-common/src/usb/usb_serial.rs2
3 files changed, 53 insertions, 1 deletions
diff --git a/embassy-hal-common/src/drop.rs b/embassy-hal-common/src/drop.rs
new file mode 100644
index 000000000..74a484de7
--- /dev/null
+++ b/embassy-hal-common/src/drop.rs
@@ -0,0 +1,51 @@
1use core::mem;
2use core::mem::MaybeUninit;
3
4pub struct OnDrop<F: FnOnce()> {
5 f: MaybeUninit<F>,
6}
7
8impl<F: FnOnce()> OnDrop<F> {
9 pub fn new(f: F) -> Self {
10 Self {
11 f: MaybeUninit::new(f),
12 }
13 }
14
15 pub fn defuse(self) {
16 mem::forget(self)
17 }
18}
19
20impl<F: FnOnce()> Drop for OnDrop<F> {
21 fn drop(&mut self) {
22 unsafe { self.f.as_ptr().read()() }
23 }
24}
25
26/// An explosive ordinance that panics if it is improperly disposed of.
27///
28/// This is to forbid dropping futures, when there is absolutely no other choice.
29///
30/// To correctly dispose of this device, call the [defuse](struct.DropBomb.html#method.defuse)
31/// method before this object is dropped.
32pub struct DropBomb {
33 _private: (),
34}
35
36impl DropBomb {
37 pub fn new() -> Self {
38 Self { _private: () }
39 }
40
41 /// Diffuses the bomb, rendering it safe to drop.
42 pub fn defuse(self) {
43 mem::forget(self)
44 }
45}
46
47impl Drop for DropBomb {
48 fn drop(&mut self) {
49 panic!("boom")
50 }
51}
diff --git a/embassy-hal-common/src/lib.rs b/embassy-hal-common/src/lib.rs
index ea20747eb..01e2d3aee 100644
--- a/embassy-hal-common/src/lib.rs
+++ b/embassy-hal-common/src/lib.rs
@@ -3,6 +3,7 @@
3// This mod MUST go first, so that the others see its macros. 3// This mod MUST go first, so that the others see its macros.
4pub(crate) mod fmt; 4pub(crate) mod fmt;
5 5
6pub mod drop;
6pub mod interrupt; 7pub mod interrupt;
7mod macros; 8mod macros;
8pub mod peripheral; 9pub mod peripheral;
diff --git a/embassy-hal-common/src/usb/usb_serial.rs b/embassy-hal-common/src/usb/usb_serial.rs
index 8b27152b5..ca43a4d73 100644
--- a/embassy-hal-common/src/usb/usb_serial.rs
+++ b/embassy-hal-common/src/usb/usb_serial.rs
@@ -4,7 +4,7 @@ use core::pin::Pin;
4use core::task::{Context, Poll}; 4use core::task::{Context, Poll};
5 5
6use embassy::io::{self, AsyncBufRead, AsyncWrite}; 6use embassy::io::{self, AsyncBufRead, AsyncWrite};
7use embassy::util::WakerRegistration; 7use embassy::waitqueue::WakerRegistration;
8use usb_device::bus::UsbBus; 8use usb_device::bus::UsbBus;
9use usb_device::class_prelude::*; 9use usb_device::class_prelude::*;
10use usb_device::UsbError; 10use usb_device::UsbError;