From ead987245d083b7e6be7158ea3fb63c8a47bf304 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 11 Sep 2021 01:53:53 +0200 Subject: embassy: Refactor module structure to remove kitchen-sink `util`. --- embassy-hal-common/src/drop.rs | 51 ++++++++++++++++++++++++++++++++ embassy-hal-common/src/lib.rs | 1 + embassy-hal-common/src/usb/usb_serial.rs | 2 +- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 embassy-hal-common/src/drop.rs (limited to 'embassy-hal-common/src') 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 @@ +use core::mem; +use core::mem::MaybeUninit; + +pub struct OnDrop { + f: MaybeUninit, +} + +impl OnDrop { + pub fn new(f: F) -> Self { + Self { + f: MaybeUninit::new(f), + } + } + + pub fn defuse(self) { + mem::forget(self) + } +} + +impl Drop for OnDrop { + fn drop(&mut self) { + unsafe { self.f.as_ptr().read()() } + } +} + +/// 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. +pub struct DropBomb { + _private: (), +} + +impl DropBomb { + pub fn new() -> Self { + Self { _private: () } + } + + /// Diffuses the bomb, rendering it safe to drop. + pub fn defuse(self) { + mem::forget(self) + } +} + +impl Drop for DropBomb { + fn drop(&mut self) { + panic!("boom") + } +} 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 @@ // This mod MUST go first, so that the others see its macros. pub(crate) mod fmt; +pub mod drop; pub mod interrupt; mod macros; 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; use core::task::{Context, Poll}; use embassy::io::{self, AsyncBufRead, AsyncWrite}; -use embassy::util::WakerRegistration; +use embassy::waitqueue::WakerRegistration; use usb_device::bus::UsbBus; use usb_device::class_prelude::*; use usb_device::UsbError; -- cgit