aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2018-01-23 22:54:12 +0100
committerGravatar Jorge Aparicio <jorge@japaric.io> 2018-01-23 22:54:12 +0100
commitd3940ec95cca6673c919f37c319170019374da88 (patch)
treea3a12774fa521d50e57c4df5a7f3c6dcb670a3b4 /src
parentd30209fa372831d819607e6a324b7512f8d1cfaa (diff)
downloadcortex-m-d3940ec95cca6673c919f37c319170019374da88.tar.gz
cortex-m-d3940ec95cca6673c919f37c319170019374da88.tar.zst
cortex-m-d3940ec95cca6673c919f37c319170019374da88.zip
singleton!: check that calls to unsafe functions require an unsafe block
Diffstat (limited to 'src')
-rw-r--r--src/macros.rs14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/macros.rs b/src/macros.rs
index c5799bf..45c511c 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -43,6 +43,9 @@ macro_rules! iprintln {
/// let y = alias();
/// // BAD this second call to `alias` will definitively `panic!`
/// let y_alias = alias();
+///
+/// # // check that the call to `uninitialized` requires unsafe
+/// # singleton!(: u8 = unsafe { std::mem::uninitialized() });
/// }
///
/// fn alias() -> &'static mut bool {
@@ -52,16 +55,17 @@ macro_rules! iprintln {
#[macro_export]
macro_rules! singleton {
(: $ty:ty = $expr:expr) => {
- $crate::interrupt::free(|_| unsafe {
+ $crate::interrupt::free(|_| {
static mut USED: bool = false;
static mut VAR: $crate::UntaggedOption<$ty> = $crate::UntaggedOption { none: () };
- if USED {
+ if unsafe { USED } {
None
} else {
- USED = true;
- VAR.some = $expr;
- let var: &'static mut _ = &mut VAR.some;
+ unsafe { USED = true }
+ let expr = $expr;
+ unsafe { VAR.some = expr }
+ let var: &'static mut _ = unsafe { &mut VAR.some };
Some(var)
}
})