Skip to content

Commit

Permalink
add debug_assert, debug_assert_eq and debug_assert_ne
Browse files Browse the repository at this point in the history
  • Loading branch information
japaric committed Nov 19, 2020
1 parent 698d283 commit db68648
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,37 @@ right: `{{:?}}`",
.into()
}

// NOTE these `debug_*` macros can be written using `macro_rules!` (that'd be simpler) but that
// results in an incorrect source code location being reported: the location of the `macro_rules!`
// statement is reported. Using a proc-macro results in the call site being reported, which is what
// we want
#[proc_macro]
pub fn debug_assert_(ts: TokenStream) -> TokenStream {
let assert = TokenStream2::from(assert_(ts));
quote!(if cfg!(debug_assertions) {
#assert
})
.into()
}

#[proc_macro]
pub fn debug_assert_eq_(ts: TokenStream) -> TokenStream {
let assert = TokenStream2::from(assert_eq_(ts));
quote!(if cfg!(debug_assertions) {
#assert
})
.into()
}

#[proc_macro]
pub fn debug_assert_ne_(ts: TokenStream) -> TokenStream {
let assert = TokenStream2::from(assert_ne_(ts));
quote!(if cfg!(debug_assertions) {
#assert
})
.into()
}

// TODO share more code with `log`
#[proc_macro]
pub fn winfo(ts: TokenStream) -> TokenStream {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub use defmt_macros::assert_ as assert;
pub use defmt_macros::assert_eq_ as assert_eq;
pub use defmt_macros::assert_ne_ as assert_ne;

pub use defmt_macros::debug_assert_ as debug_assert;
pub use defmt_macros::debug_assert_eq_ as debug_assert_eq;
pub use defmt_macros::debug_assert_ne_ as debug_assert_ne;

pub use defmt_macros::unreachable_ as unreachable;

pub use defmt_macros::todo_ as todo;
Expand Down

0 comments on commit db68648

Please sign in to comment.