fn_name

Crates.iofn_name
lib.rsfn_name
version0.1.0
sourcesrc
created_at2022-05-11 17:44:15.552064
updated_at2022-05-11 17:44:15.552064
descriptionMacros that produce the name of the function they're invoked within.
homepage
repositoryhttps://github.com/jswrenn/fn_name
max_upload_size
id584817
size17,834
Jack Wrenn (jswrenn)

documentation

README

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.

Commit count: 2

cargo fmt