commit ec277110e581bbd20567827ce94d49757349ebc8 Author: ddidderr Date: Sun Jan 29 12:05:36 2023 +0100 initial commit: DBG! and function_name! macros diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fa36803 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rustics-dbg" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..676b3e7 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,40 @@ +/// Exact copy from the stdext crate v0.3.1 +/// +#[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) -> &'static str { + std::any::type_name::() + } + 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)*); + }; +}