#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

#2140 in Rust patterns

Download history 180/week @ 2024-07-22 362/week @ 2024-07-29 289/week @ 2024-08-05 276/week @ 2024-08-12 166/week @ 2024-08-19 316/week @ 2024-08-26 297/week @ 2024-09-02 203/week @ 2024-09-09 333/week @ 2024-09-16 182/week @ 2024-09-23 246/week @ 2024-09-30 354/week @ 2024-10-07 319/week @ 2024-10-14 242/week @ 2024-10-21 161/week @ 2024-10-28 174/week @ 2024-11-04

909 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