#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

#2594 in Rust patterns

Download history 64/week @ 2024-01-01 72/week @ 2024-01-08 66/week @ 2024-01-15 71/week @ 2024-01-22 60/week @ 2024-01-29 50/week @ 2024-02-05 82/week @ 2024-02-12 114/week @ 2024-02-19 116/week @ 2024-02-26 82/week @ 2024-03-04 178/week @ 2024-03-11 199/week @ 2024-03-18 332/week @ 2024-03-25 310/week @ 2024-04-01 148/week @ 2024-04-08 206/week @ 2024-04-15

1,009 downloads per month
Used in 8 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