Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug_name macro #17

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading