method_name 0.3.2

macro that expands to the name of the annotated function
Documentation

::method_name

This crate is a fork of https://github.com/danielhenrymantilla/rust-function_name

Function attribute #[named] that generates a method_name! macro in the scope of the function's body.

The generated method_name!() is a macro that expands to the name of the annotated function, as a string literal.

Repository Latest version Documentation unsafe forbidden no_std compatible License CI

Examples

use ::method_name::named;

#[named]
fn my_super_duper_function ()
{
    assert_eq!(
        method_name!(),
        "my_super_duper_function",
    );
}

For methods, put #[named] on the impl block to include the enclosing type in method_name!():

use ::method_name::named;

struct Session;

#[named]
impl Session {
    fn new() {
        assert_eq!(method_name!(), "Session::new");
    }
}

When #[named] is placed directly on a method, method_name!() continues to return only the method name.

Since the generated method_name! expands to a string literal, it can be used with other macros such as concat!:

#[macro_use] extern crate method_name;

macro_rules! function_path {() => (concat!(
    module_path!(), "::", method_name!()
))}

pub mod foo {
    pub mod bar {
        #[named]
        pub fn baz ()
        {
            assert_eq!(
                function_path!(),
                [
                    env!("CARGO_PKG_NAME"),
                    "foo", "bar",
                    "baz",
                ].join("::"),
            );
        }
    }
}

Unstable API

The method_name_unstable!() macro is available when the unstable feature is enabled. This API may change in future releases.

Enable the feature in your dependency declaration:

method_name = { version = "0.3", features = ["unstable"] }

Then import and call the macro from your crate:

use method_name::method_name_unstable;

fn foo() {
    assert_eq!(method_name_unstable!(), "foo");
}