Skip to content

Commit

Permalink
Merge pull request #522 from quartiq/singleton-meta
Browse files Browse the repository at this point in the history
singleton: forward attributes
  • Loading branch information
adamgreig committed Apr 17, 2024
2 parents 53073e1 + f7474e6 commit 4395bae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions cortex-m/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
22 changes: 17 additions & 5 deletions cortex-m/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
};
}

Expand All @@ -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: () = ();

0 comments on commit 4395bae

Please sign in to comment.