#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

#2653 in Rust patterns

Download history 180/week @ 2024-04-03 180/week @ 2024-04-10 221/week @ 2024-04-17 263/week @ 2024-04-24 427/week @ 2024-05-01 146/week @ 2024-05-08 219/week @ 2024-05-15 149/week @ 2024-05-22 187/week @ 2024-05-29 290/week @ 2024-06-05 177/week @ 2024-06-12 205/week @ 2024-06-19 161/week @ 2024-06-26 125/week @ 2024-07-03 136/week @ 2024-07-10 236/week @ 2024-07-17

691 downloads per month
Used in 9 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