initial commit: DBG! and function_name! macros
This commit is contained in:
commit
ec277110e5
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
/Cargo.lock
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rustics-dbg"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
40
src/lib.rs
Normal file
40
src/lib.rs
Normal file
@ -0,0 +1,40 @@
|
||||
/// Exact copy from the stdext crate v0.3.1
|
||||
/// <https://docs.rs/stdext/0.3.1/stdext/macro.function_name.html>
|
||||
#[macro_export]
|
||||
macro_rules! function_name {
|
||||
() => {{
|
||||
// Okay, this is ugly, I get it. However, this is the best we can get on a stable rust.
|
||||
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`.
|
||||
&name[..name.len() - 3]
|
||||
}};
|
||||
}
|
||||
|
||||
/// Like [`println!`] but also prints the current function name (including module path) and line.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use rustics_dbg::DBG;
|
||||
/// let x = 42;
|
||||
/// let y = "hello debug";
|
||||
/// DBG!("{} {}", x, y);
|
||||
/// ```
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// use rustics_dbg::DBG;
|
||||
/// let x = 42;
|
||||
/// let y = "hello debug";
|
||||
/// // Since internally the macro uses concat!, you cannot inline variables into the format string.
|
||||
/// DBG!("{x} {y}"); // this won't compile
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! DBG {
|
||||
($fmt:literal $(,$arg:expr)*) => {
|
||||
println!(concat!("{}():{} ", $fmt), $crate::function_name!(), line!() $(,$arg)*);
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user