Skip to content

Commit

Permalink
Improve discoverability with expect! as alias for unwrap!
Browse files Browse the repository at this point in the history
See related issue: knurling-rs#867
Its easy to assume unwrap! can not have a message like std's expect.

Adding an expect macro will help both autocomplete guide programmers
looking for one and those who discount the unwrap entry in the defmt
api docs based on their knowledge that std's unwrap can not have a
message.

The documentation of the expect alias highlights that unwrap works with
a message to and points to the unwrap macro docs for details.
  • Loading branch information
dvdsk committed Oct 9, 2024
1 parent b7a89f5 commit 1e1f284
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions defmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ pub use defmt_macros::panic_ as panic;
/// [the manual]: https://defmt.ferrous-systems.com/macros.html
pub use defmt_macros::unwrap;

/// This is an alias for defmt's [`unwrap`] macro which supports messages like std's except.
/// ```
/// use defmt::expect;
///
/// # let arg = ();
/// let x = value.expect(&format!("text {:?}", arg));
/// let x = expect!(value, "text {:?}", arg); // arg must be implement `Format`
/// ```
///
/// For the complete documentation see that of defmt's *unwrap* macro.
// note: Linking to unwrap is broken as of 2024-10-09, it links back to expect
pub use defmt_macros::unwrap as expect;

/// Overrides the panicking behavior of `defmt::panic!`
///
/// By default, `defmt::panic!` calls `core::panic!` after logging the panic message using `defmt`.
Expand Down

0 comments on commit 1e1f284

Please sign in to comment.