Skip to content

Commit 2ed23a5

Browse files
committed
Merge pull request #522 from quartiq/singleton-meta
singleton: forward attributes
1 parent 7e75be4 commit 2ed23a5

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

cortex-m/src/macros.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ macro_rules! iprintln {
6161
/// ```
6262
#[macro_export]
6363
macro_rules! singleton {
64-
($name:ident: $ty:ty = $expr:expr) => {
64+
($(#[$meta:meta])* $name:ident: $ty:ty = $expr:expr) => {
6565
$crate::interrupt::free(|_| {
6666
// this is a tuple of a MaybeUninit and a bool because using an Option here is
6767
// problematic: Due to niche-optimization, an Option could end up producing a non-zero
6868
// initializer value which would move the entire static from `.bss` into `.data`...
69+
$(#[$meta])*
6970
static mut $name: (::core::mem::MaybeUninit<$ty>, bool) =
7071
(::core::mem::MaybeUninit::uninit(), false);
7172

@@ -79,14 +80,13 @@ macro_rules! singleton {
7980
#[allow(unsafe_code)]
8081
unsafe {
8182
$name.1 = true;
82-
$name.0 = ::core::mem::MaybeUninit::new(expr);
83-
Some(&mut *$name.0.as_mut_ptr())
83+
Some($name.0.write(expr))
8484
}
8585
}
8686
})
8787
};
88-
(: $ty:ty = $expr:expr) => {
89-
$crate::singleton!(VAR: $ty = $expr)
88+
($(#[$meta:meta])* : $ty:ty = $expr:expr) => {
89+
$crate::singleton!($(#[$meta])* VAR: $ty = $expr)
9090
};
9191
}
9292

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

0 commit comments

Comments
 (0)