initial commit: DBG! and function_name! macros

This commit is contained in:
ddidderr 2023-01-29 12:05:36 +01:00
commit ec277110e5
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14
3 changed files with 48 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/Cargo.lock

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "rustics-dbg"
version = "1.0.0"
edition = "2021"
[dependencies]

40
src/lib.rs Normal file
View 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)*);
};
}