#name #macro #function #methods

no-std fn_name

Macros that produce the name of the function they're invoked within

1 unstable release

0.1.0 May 11, 2022

#2598 in Rust patterns

Download history 188/week @ 2024-11-17 219/week @ 2024-11-24 186/week @ 2024-12-01 186/week @ 2024-12-08 167/week @ 2024-12-15 86/week @ 2024-12-22 259/week @ 2024-12-29 189/week @ 2025-01-05 166/week @ 2025-01-12 214/week @ 2025-01-19 164/week @ 2025-01-26 208/week @ 2025-02-02 227/week @ 2025-02-09 488/week @ 2025-02-16 324/week @ 2025-02-23 398/week @ 2025-03-02

1,458 downloads per month
Used in 11 crates (via veilid-tools)

MIT/Apache

7KB

fn_name

Macros that produce the name of the function they're invoked within.

Uninstantiated Names

The uninstantiated! macro produces the name of the surrounding function or method, without generics instantiated; e.g.:

struct GenericType<A>(A);

impl<A> GenericType<A> {
    fn generic_method<B>(self, _: B) -> &'static str {
        fn_name::uninstantiated!()
    }
}

fn main() {
    assert_eq!(
        GenericType(42u8).generic_method(false),
        "GenericType<_>::generic_method"
    );
}

Instantiated Names

The instantiated! macro produces the name of the surrounding function or method, including instantiated generics (if any); e.g.:

struct GenericType<A>(A);

impl<A> GenericType<A> {
    fn generic_method<B>(self, _: B) -> &'static str {
        fn_name::instantiated!()
    }
}

fn main() {
    assert_eq!(
        GenericType(42u8).generic_method(false),
        "GenericType<u8>::generic_method<bool>"
    );
}

Limitations

The expansion of these macros is not (yet) const evaluable; their implementations rely on core::any::type_name, which is not a const fn.

No runtime deps