Skip to content

Commit

Permalink
Introduce BOARD const, deprecating board() function
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Sep 14, 2023
1 parent 234ec3f commit e1cbf96
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,25 @@ mod helpers;
mod never;
use never::Never;

/// The identifier of the RIOT board the program is being built for (`RIOT_BOARD` in C).
#[doc(alias = "RIOT_BOARD")]
pub const BOARD: &'static str = {
let b = riot_sys::RIOT_BOARD;
let Ok(b) = core::ffi::CStr::from_bytes_with_nul(b) else {
// Could be `.expect()`, but that's not const yet
// Workaround-For: https://github.com/rust-lang/rust/issues/67441
panic!("Board names are null-terminated C strings");
};
let Ok(b) = b.to_str() else {
panic!("Board names should be ASCII")
};
b
};

/// Name of the RIOT board that is being used
///
/// Development:
///
/// Once this can be const, it'll be deprecated in favor of a pub const &'static str. That'll also
/// force the compiler to remove all the exceptions at build time (currently it does not, even with
/// aggressive optimization).
pub fn board() -> &'static str {
core::ffi::CStr::from_bytes_with_nul(riot_sys::RIOT_BOARD)
.expect("Board names are null-terminated C strings")
.to_str()
.expect("Board names should be ASCII")
#[deprecated(note = "Access BOARD instead")]
pub const fn board() -> &'static str {
BOARD
}

/// Cast pointers around before passing them in to functions; this is sometimes needed when a
Expand Down

0 comments on commit e1cbf96

Please sign in to comment.