diff options
| author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-23 19:37:29 +0000 |
|---|---|---|
| committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-23 19:37:29 +0000 |
| commit | 6b61cd2e3ff26d96615a7bfc386077ccf6505c28 (patch) | |
| tree | 2a8b089d42aad24286cba11c4561e714fc84b85d /src | |
| parent | 43c2ffbdcfcf96382dbe5e5a64b2af90a447636d (diff) | |
| parent | c6f9b2c0aa62fa3ceed6ac58928af15d9e3a58a2 (diff) | |
Merge #153
153: add "nightly" feature; replace hint::unreachable_unchecked with a panic r=korken89 a=japaric
this implements the action plan described in #149
to give you a sense of the overhead of this change: it has increased the binary
size of some of our examples by up to 10% but this is mainly from pulling in a
panic handler that does formatting
r? @korken89
Co-authored-by: Jorge Aparicio <jorge@japaric.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/export.rs | 73 | ||||
| -rw-r--r-- | src/lib.rs | 6 |
2 files changed, 60 insertions, 19 deletions
diff --git a/src/export.rs b/src/export.rs index 6eae65f..495468e 100644 --- a/src/export.rs +++ b/src/export.rs @@ -1,8 +1,8 @@ //! IMPLEMENTATION DETAILS. DO NOT USE ANYTHING IN THIS MODULE -#[cfg(not(debug_assertions))] -use core::hint; -use core::{cell::Cell, ptr, u8}; +#[cfg(not(feature = "nightly"))] +use core::ptr; +use core::{cell::Cell, u8}; #[cfg(armv7m)] use cortex_m::register::basepri; @@ -64,28 +64,69 @@ impl Priority { } } -// TODO(MaybeUninit) Until core::mem::MaybeUninit is stabilized we use our own (inefficient) -// implementation +#[cfg(feature = "nightly")] +pub struct MaybeUninit<T> { + // we newtype so the end-user doesn't need `#![feature(maybe_uninit)]` in their code + inner: core::mem::MaybeUninit<T>, +} + +#[cfg(feature = "nightly")] +impl<T> MaybeUninit<T> { + pub const fn uninitialized() -> Self { + MaybeUninit { + inner: core::mem::MaybeUninit::uninitialized(), + } + } + + pub fn as_ptr(&self) -> *const T { + self.inner.as_ptr() + } + + pub fn as_mut_ptr(&mut self) -> *mut T { + self.inner.as_mut_ptr() + } + + pub fn set(&mut self, value: T) -> &mut T { + self.inner.set(value) + } +} + +#[cfg(not(feature = "nightly"))] pub struct MaybeUninit<T> { value: Option<T>, } +#[cfg(not(feature = "nightly"))] +const MSG: &str = + "you have hit a bug (UB) in RTFM implementation; try enabling this crate 'nightly' feature"; + +#[cfg(not(feature = "nightly"))] impl<T> MaybeUninit<T> { pub const fn uninitialized() -> Self { MaybeUninit { value: None } } + pub fn as_ptr(&self) -> *const T { + if let Some(x) = self.value.as_ref() { + x + } else { + unreachable!(MSG) + } + } + + pub fn as_mut_ptr(&mut self) -> *mut T { + if let Some(x) = self.value.as_mut() { + x + } else { + unreachable!(MSG) + } + } + pub unsafe fn get_ref(&self) -> &T { if let Some(x) = self.value.as_ref() { x } else { - match () { - // Try to catch UB when compiling in release with debug assertions enabled - #[cfg(debug_assertions)] - () => unreachable!(), - #[cfg(not(debug_assertions))] - () => hint::unreachable_unchecked(), - } + unreachable!(MSG) } } @@ -93,13 +134,7 @@ impl<T> MaybeUninit<T> { if let Some(x) = self.value.as_mut() { x } else { - match () { - // Try to catch UB when compiling in release with debug assertions enabled - #[cfg(debug_assertions)] - () => unreachable!(), - #[cfg(not(debug_assertions))] - () => hint::unreachable_unchecked(), - } + unreachable!(MSG) } } @@ -35,7 +35,13 @@ //! //! [`Instant`]: struct.Instant.html //! [`Duration`]: struct.Duration.html +//! +//! - `nightly`. Enabling this opt-in feature makes RTFM internally use the unstable +//! `core::mem::MaybeUninit` API and unstable `const_fn` language feature to reduce static memory +//! usage, runtime overhead and initialization overhead. This feature requires a nightly compiler +//! and may stop working at any time! +#![cfg_attr(feature = "nightly", feature(maybe_uninit))] #![deny(missing_docs)] #![deny(warnings)] #![no_std] |
