Skip to content

Commit

Permalink
Add debug_name macro
Browse files Browse the repository at this point in the history
This macro helps developers to have good debug information to show in code and help them
find it via a modern IDE

Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Jan 30, 2024
1 parent a791533 commit 5fd827f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ chance that it will suit your needs.
- New handy macros (mostly for development purposes):

```rust
use stdext::{compile_warning, function_name};
use stdext::{compile_warning, debug_name, function_name};

fn sample_function() {
println!("This function is called {}", function_name!());
println!("You can also found it here:", debug_name!());

compile_warning!("This function must do something else...");
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@
//!
//! - [`compile_warning`] for spawning a user-defined compilation warnings.
//! - [`function_name`] for getting an enclosing function name.
//! - [`debug_name`] for getting a helpful string for debug.
//!
//! [`compile_warning`]: ./macro.compile_warning.html
//! [`function_name`]: ./macro.function_name.html
//! [`debug_name`]: ./macro.debug_name.html
//!
//! ## Highlights
//!
Expand Down Expand Up @@ -115,10 +117,11 @@
//! - New handy macros (mostly for development purposes):
//!
//! ```rust
//! use stdext::{compile_warning, function_name};
//! use stdext::{compile_warning, debug_name, function_name};
//!
//! fn sample_function() {
//! println!("This function is called {}", function_name!());
//! println!("You can also found it here: {}", debug_name!());
//!
//! compile_warning!("This function must do something else...");
//! }
Expand Down
35 changes: 35 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,41 @@ macro_rules! function_name {
}};
}

/// This macro returns the name of the enclosing function, line number and also filename.
/// As the internal implementation is based on the [`std::any::type_name`], this macro derives
/// all the limitations of this function.
///
/// ## Examples
///
/// ```rust
/// mod bar {
/// pub fn sample_function() {
/// use stdext::debug_name;
/// assert!(debug_name!().starts_with("src/macros.rs:8"));
/// assert!(debug_name!().ends_with("bar::sample_function"));
/// }
/// }
///
/// bar::sample_function();
/// ```
///
/// [`std::any::type_name`]: https://doc.rust-lang.org/std/any/fn.type_name.html
#[macro_export]
macro_rules! debug_name {
() => {{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let name = type_name_of(f);
// `3` is the length of the `::f`.
let trimmed_name = &name[..name.len() - 3];
let file = file!();
let line = line!();
format!("{file}:{line} at {trimmed_name}")
}};
}

/// Attempts to get variant from the enum variable.
///
/// ## Examples
Expand Down

0 comments on commit 5fd827f

Please sign in to comment.