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 Dec 24, 2021
1 parent d893417 commit e00ff35
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,34 @@ pub use cstr_core as cstr;

pub mod error;

pub const BOARD: &'static str = match {
match cstr::CStr::from_bytes_with_nul(riot_sys::RIOT_BOARD) {
Ok(s) => s,
_ => panic!("Board names are null-terminated C strings"),
}

// Preferred would be
// .expect("Board names are null-terminated C strings")
}
.to_str()
{
Ok(x) => x,
// Preferred would be
// .expect("Board names should be ASCII");
// but feature(const_result) seems not to cover that yet
_ => panic!("Board names should be ASCII"),
};

/// 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).
#[deprecated(note = "Access BOARD instead")]
pub fn board() -> &'static str {
cstr::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")
BOARD
}

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

0 comments on commit e00ff35

Please sign in to comment.