diff --git a/cortex-m/CHANGELOG.md b/cortex-m/CHANGELOG.md index fc043db9..85efc02f 100644 --- a/cortex-m/CHANGELOG.md +++ b/cortex-m/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added the ability to name the statics generated by `singleton!()` for better debuggability (#364, #380). - Added `critical-section-single-core` feature which provides an implementation for the `critical_section` crate for single-core systems, based on disabling all interrupts. (#447) - Added support for `embedded-hal` version 1 delay traits, requiring rust 1.60. +- `singleton!()` now forwards attributes (#522). ### Fixed - Fixed `singleton!()` statics sometimes ending up in `.data` instead of `.bss` (#364, #380). diff --git a/cortex-m/src/macros.rs b/cortex-m/src/macros.rs index 2cf4f89a..ba780481 100644 --- a/cortex-m/src/macros.rs +++ b/cortex-m/src/macros.rs @@ -64,11 +64,12 @@ macro_rules! iprintln { /// ``` #[macro_export] macro_rules! singleton { - ($name:ident: $ty:ty = $expr:expr) => { + ($(#[$meta:meta])* $name:ident: $ty:ty = $expr:expr) => { $crate::_export::critical_section::with(|_| { // this is a tuple of a MaybeUninit and a bool because using an Option here is // problematic: Due to niche-optimization, an Option could end up producing a non-zero // initializer value which would move the entire static from `.bss` into `.data`... + $(#[$meta])* static mut $name: (::core::mem::MaybeUninit<$ty>, bool) = (::core::mem::MaybeUninit::uninit(), false); @@ -82,14 +83,13 @@ macro_rules! singleton { #[allow(unsafe_code)] unsafe { $name.1 = true; - $name.0 = ::core::mem::MaybeUninit::new(expr); - Some(&mut *$name.0.as_mut_ptr()) + Some($name.0.write(expr)) } } }) }; - (: $ty:ty = $expr:expr) => { - $crate::singleton!(VAR: $ty = $expr) + ($(#[$meta:meta])* : $ty:ty = $expr:expr) => { + $crate::singleton!($(#[$meta])* VAR: $ty = $expr) }; } @@ -115,3 +115,15 @@ const CFAIL: () = (); /// ``` #[allow(dead_code)] const CPASS: () = (); + +/// ``` +/// use cortex_m::singleton; +/// +/// fn foo() { +/// // check that attributes are forwarded +/// singleton!(#[link_section = ".bss"] FOO: u8 = 0); +/// singleton!(#[link_section = ".bss"]: u8 = 1); +/// } +/// ``` +#[allow(dead_code)] +const CPASS_ATTR: () = ();