#[macro_export]
macro_rules! func_path {
() => {{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let type_name = type_name_of(f);
let name_path = &type_name[..type_name.len() - 3];
let stripped = name_path.replace("::{{closure}}", "");
stripped
}};
}
#[macro_export]
macro_rules! func_name {
() => {{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let type_name = type_name_of(f);
let name_path = &type_name[..type_name.len() - 3];
let stripped = name_path.replace("::{{closure}}", "");
stripped.split("::").last().unwrap().to_string()
}};
}
#[cfg(test)]
mod tests {
#[tokio::test]
async fn test_func_path() {
async fn foo() -> String {
func_path!()
}
let p = foo().await;
assert_eq!("anyerror::macros::tests::test_func_path::foo", p);
}
#[tokio::test]
async fn test_func_name() {
async fn bar() -> String {
func_name!()
}
let p = bar().await;
assert_eq!("bar", p);
}
}