Skip to content

Commit 4395bae

Browse files
authored
Merge pull request rust-embedded#522 from quartiq/singleton-meta
singleton: forward attributes
2 parents 53073e1 + f7474e6 commit 4395bae

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

cortex-m/CHANGELOG.md

+1
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 (#522).
2728

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

cortex-m/src/macros.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ macro_rules! iprintln {
6464
/// ```
6565
#[macro_export]
6666
macro_rules! singleton {
67-
($name:ident: $ty:ty = $expr:expr) => {
67+
($(#[$meta:meta])* $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+
$(#[$meta])*
7273
static mut $name: (::core::mem::MaybeUninit<$ty>, bool) =
7374
(::core::mem::MaybeUninit::uninit(), false);
7475

@@ -82,14 +83,13 @@ macro_rules! singleton {
8283
#[allow(unsafe_code)]
8384
unsafe {
8485
$name.1 = true;
85-
$name.0 = ::core::mem::MaybeUninit::new(expr);
86-
Some(&mut *$name.0.as_mut_ptr())
86+
Some($name.0.write(expr))
8787
}
8888
}
8989
})
9090
};
91-
(: $ty:ty = $expr:expr) => {
92-
$crate::singleton!(VAR: $ty = $expr)
91+
($(#[$meta:meta])* : $ty:ty = $expr:expr) => {
92+
$crate::singleton!($(#[$meta])* VAR: $ty = $expr)
9393
};
9494
}
9595

@@ -115,3 +115,15 @@ const CFAIL: () = ();
115115
/// ```
116116
#[allow(dead_code)]
117117
const CPASS: () = ();
118+
119+
/// ```
120+
/// use cortex_m::singleton;
121+
///
122+
/// fn foo() {
123+
/// // check that attributes are forwarded
124+
/// singleton!(#[link_section = ".bss"] FOO: u8 = 0);
125+
/// singleton!(#[link_section = ".bss"]: u8 = 1);
126+
/// }
127+
/// ```
128+
#[allow(dead_code)]
129+
const CPASS_ATTR: () = ();

0 commit comments

Comments
 (0)