#name #methods #function #macro

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

#1961 in Rust patterns

Download history 180/week @ 2024-12-13 101/week @ 2024-12-20 248/week @ 2024-12-27 173/week @ 2025-01-03 175/week @ 2025-01-10 204/week @ 2025-01-17 171/week @ 2025-01-24 199/week @ 2025-01-31 231/week @ 2025-02-07 318/week @ 2025-02-14 459/week @ 2025-02-21 366/week @ 2025-02-28 461/week @ 2025-03-07 417/week @ 2025-03-14 652/week @ 2025-03-21 389/week @ 2025-03-28

1,997 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