Skip to content

Commit

Permalink
Add example showing a macro wrapping bunt macros
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasKalbertodt committed Oct 3, 2020
1 parent 3167d11 commit a992ca8
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Sometimes, you might want to wrap bunt's macros into your own
//! project-specific macro. Often, you want to print a prefix or something like
//! that.


macro_rules! log {
($fmt:literal $(, $arg:expr)* $(,)?) => {
bunt::println!(
// Bunt macros allow to pass an "array" of format strings which are
// then concatenated by bunt.
[
// Our prefix
"[{[magenta] log_module_path}] ",
// What the user passed
$fmt,
// Our postfix
" {$cyan}({log_file}:{log_line}){/$}",
],
$($arg ,)*
log_module_path = std::module_path!(),
log_file = std::file!(),
log_line = std::line!(),
)

// This solution is not optimal though. For one, it would be nice to
// `stringify!(std::module_path!())` instead of passing it as runtime
// string argument. That's not possible because macro expansions are
// lazy.
//
// Futhermore, we pass the arguments with names like `log_module_path`.
// In theory, the user could also use a named argument with that name.
// This `log!` macro is very much an internal helper macro and not
// something you would want to put into your public API.
};
}


fn main() {
log!("Hello {}", "peter");
banana::do_something();
}

mod banana {
pub fn do_something() {
log!("I'm doing something with {[yellow]:?}", vec![1, 2, 4]);
}
}

0 comments on commit a992ca8

Please sign in to comment.