diff options
author | 2022-01-03 11:32:26 +0100 | |
---|---|---|
committer | 2022-01-03 11:39:55 +0100 | |
commit | 19125ac052fcba1d67de39be3292e34bd6c6d0f4 (patch) | |
tree | cc8fc44e3e8ddb6f457d9722a4bdff288aabb35b /src/macros.rs | |
parent | 50c7fd34901eb9a2a9aec79c4c5a5574140046ba (diff) | |
download | cortex-m-19125ac052fcba1d67de39be3292e34bd6c6d0f4.tar.gz cortex-m-19125ac052fcba1d67de39be3292e34bd6c6d0f4.tar.zst cortex-m-19125ac052fcba1d67de39be3292e34bd6c6d0f4.zip |
macros: Allow setting a name for singleton!() statics
Right now, the `singleton!()` macro always creates a static named `VAR`.
This is annoying when digging through objdump output or digging into
memory with a debugger because it is hard to see what singleton you're
looking at when they are all called `<...>::{{closure}}::VAR`.
To make it a bit nicer, allow setting the name of the generated static
to some meaningful identifier which can then be cross-referenced in
debugger output, for example with
singleton!(FOO_BUFFER: [u8; 1024] = [0u8; 1024]);
There is no other side-effects to this change - the identifier is never
visible to other code because it is still contained in the closure of
the macro.
Diffstat (limited to 'src/macros.rs')
-rw-r--r-- | src/macros.rs | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/macros.rs b/src/macros.rs index 66b75b1..a1ce322 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -30,9 +30,12 @@ macro_rules! iprintln { /// `None` variant the caller must ensure that the macro is called from a function that's executed /// at most once in the whole lifetime of the program. /// -/// # Note +/// # Notes /// This macro is unsound on multi core systems. /// +/// For debuggability, you can set an explicit name for a singleton. This name only shows up the +/// the debugger and is not referencable from other code. See example below. +/// /// # Example /// /// ``` no_run @@ -50,15 +53,20 @@ macro_rules! iprintln { /// fn alias() -> &'static mut bool { /// singleton!(: bool = false).unwrap() /// } +/// +/// fn singleton_with_name() { +/// // A name only for debugging purposes +/// singleton!(FOO_BUFFER: [u8; 1024] = [0u8; 1024]); +/// } /// ``` #[macro_export] macro_rules! singleton { - (: $ty:ty = $expr:expr) => { + ($name:ident: $ty:ty = $expr:expr) => { $crate::interrupt::free(|_| { - static mut VAR: Option<$ty> = None; + static mut $name: Option<$ty> = None; #[allow(unsafe_code)] - let used = unsafe { VAR.is_some() }; + let used = unsafe { $name.is_some() }; if used { None } else { @@ -66,16 +74,19 @@ macro_rules! singleton { #[allow(unsafe_code)] unsafe { - VAR = Some(expr) + $name = Some(expr) } #[allow(unsafe_code)] unsafe { - VAR.as_mut() + $name.as_mut() } } }) }; + (: $ty:ty = $expr:expr) => { + $crate::singleton!(VAR: $ty = $expr) + }; } /// ``` compile_fail |