Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 and visibility (#521).

### Fixed
- Fixed `singleton!()` statics sometimes ending up in `.data` instead of `.bss` (#364, #380).
Expand Down
18 changes: 15 additions & 3 deletions cortex-m/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ macro_rules! iprintln {
/// ```
#[macro_export]
macro_rules! singleton {
($name:ident: $ty:ty = $expr:expr) => {
($(#[$meta:meta])* $vis:vis $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`...
static mut $name: (::core::mem::MaybeUninit<$ty>, bool) =
$(#[$meta])*
$vis static mut $name: (::core::mem::MaybeUninit<$ty>, bool) =
(::core::mem::MaybeUninit::uninit(), false);

#[allow(unsafe_code)]
Expand All @@ -83,7 +84,7 @@ macro_rules! singleton {
unsafe {
$name.1 = true;
$name.0 = ::core::mem::MaybeUninit::new(expr);
Some(&mut *$name.0.as_mut_ptr())
Some($name.0.assume_init_mut())
}
}
})
Expand Down Expand Up @@ -115,3 +116,14 @@ const CFAIL: () = ();
/// ```
#[allow(dead_code)]
const CPASS: () = ();

/// ```
/// use cortex_m::singleton;
///
/// fn foo() {
/// // check that attributes and visibility are forwarded
/// singleton!(#[link_section = ".bss"] pub(crate) FOO: u8 = 0);
/// }
/// ```
#[allow(dead_code)]
const CPASS_ATTR: () = ();