Skip to content

Commit 7bfd43e

Browse files
committed
singleton: forward attributes and visibility
This only works in the long form with a named variable because of constraints on where the `vis` metavariable can be used. close #521
1 parent d85d9c8 commit 7bfd43e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

cortex-m/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2424
- Added the ability to name the statics generated by `singleton!()` for better debuggability (#364, #380).
2525
- 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)
2626
- Added support for `embedded-hal` version 1 delay traits, requiring rust 1.60.
27+
- `singleton!()` now forwards attributes and visibility (#521).
2728

2829
### Fixed
2930
- Fixed `singleton!()` statics sometimes ending up in `.data` instead of `.bss` (#364, #380).

cortex-m/src/macros.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ macro_rules! iprintln {
6464
/// ```
6565
#[macro_export]
6666
macro_rules! singleton {
67-
($name:ident: $ty:ty = $expr:expr) => {
67+
($(#[$meta:meta])* $vis:vis $name:ident: $ty:ty = $expr:expr) => {
6868
$crate::_export::critical_section::with(|_| {
6969
// this is a tuple of a MaybeUninit and a bool because using an Option here is
7070
// problematic: Due to niche-optimization, an Option could end up producing a non-zero
7171
// initializer value which would move the entire static from `.bss` into `.data`...
72-
static mut $name: (::core::mem::MaybeUninit<$ty>, bool) =
72+
$(#[$meta])*
73+
$vis static mut $name: (::core::mem::MaybeUninit<$ty>, bool) =
7374
(::core::mem::MaybeUninit::uninit(), false);
7475

7576
#[allow(unsafe_code)]
@@ -115,3 +116,15 @@ const CFAIL: () = ();
115116
/// ```
116117
#[allow(dead_code)]
117118
const CPASS: () = ();
119+
120+
121+
/// ```
122+
/// use cortex_m::singleton;
123+
///
124+
/// fn foo() {
125+
/// // check that attributes and visibility are forwarded
126+
/// singleton!(#[link_section = ".bss"] pub(crate) FOO: u8 = 0);
127+
/// }
128+
/// ```
129+
#[allow(dead_code)]
130+
const CPASS_ATTR: () = ();

0 commit comments

Comments
 (0)