diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-09-11 01:53:53 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-09-11 02:35:35 +0200 |
| commit | ead987245d083b7e6be7158ea3fb63c8a47bf304 (patch) | |
| tree | 2d51b80e3d4fc2670b64cb05af22cc54202a6104 /embassy-hal-common | |
| parent | 200f881048a8193c490ff5906ebf320ac98a8331 (diff) | |
embassy: Refactor module structure to remove kitchen-sink `util`.
Diffstat (limited to 'embassy-hal-common')
| -rw-r--r-- | embassy-hal-common/src/drop.rs | 51 | ||||
| -rw-r--r-- | embassy-hal-common/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-hal-common/src/usb/usb_serial.rs | 2 |
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 @@ | |||
| 1 | use core::mem; | ||
| 2 | use core::mem::MaybeUninit; | ||
| 3 | |||
| 4 | pub struct OnDrop<F: FnOnce()> { | ||
| 5 | f: MaybeUninit<F>, | ||
| 6 | } | ||
| 7 | |||
| 8 | impl<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 | |||
| 20 | impl<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. | ||
| 32 | pub struct DropBomb { | ||
| 33 | _private: (), | ||
| 34 | } | ||
| 35 | |||
| 36 | impl 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 | |||
| 47 | impl 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. |
| 4 | pub(crate) mod fmt; | 4 | pub(crate) mod fmt; |
| 5 | 5 | ||
| 6 | pub mod drop; | ||
| 6 | pub mod interrupt; | 7 | pub mod interrupt; |
| 7 | mod macros; | 8 | mod macros; |
| 8 | pub mod peripheral; | 9 | pub 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; | |||
| 4 | use core::task::{Context, Poll}; | 4 | use core::task::{Context, Poll}; |
| 5 | 5 | ||
| 6 | use embassy::io::{self, AsyncBufRead, AsyncWrite}; | 6 | use embassy::io::{self, AsyncBufRead, AsyncWrite}; |
| 7 | use embassy::util::WakerRegistration; | 7 | use embassy::waitqueue::WakerRegistration; |
| 8 | use usb_device::bus::UsbBus; | 8 | use usb_device::bus::UsbBus; |
| 9 | use usb_device::class_prelude::*; | 9 | use usb_device::class_prelude::*; |
| 10 | use usb_device::UsbError; | 10 | use usb_device::UsbError; |
