of_method

Macro of_method 

Source
macro_rules! of_method {
    (Self:: $method:ident) => { ... };
    ($ty:ident :: $method:ident) => { ... };
    ($ty:ident :: $method:ident ::<$($arg:ty),*>) => { ... };
    (<$ty:ty> :: $method:ident) => { ... };
    (<$ty:ty> :: $method:ident ::<$($arg:ty),*>) => { ... };
}
Expand description

Get the name of the given method like Type::method as a &'static str.

This macro resolves Self to the appropriate type when used inside an impl block.

By default, this macro expects a simple type identifier like Type::field. To use types with qualified path or generic parameters, wrap the type in angle brackets like <Type<T>>::field or <module::Type>::field.

If both the Type and method parts are single identifiers and the Type part is not Self, the macro expands to a string literal at compile time. For more complex types, the macro uses runtime type name retrieval with caching.

Due to implementation limitations, you cannot use the ::<..> placeholder to exclude generic parameters. Use explicit type arguments instead.

ยงExamples

struct MyStruct;
impl MyStruct {
    fn my_method(&self) {}
    fn my_generic_method<T>(&self) {}
}
struct MyGenericStruct<T>(std::marker::PhantomData<T>);
impl<T> MyGenericStruct<T> {
    fn my_method(&self) {}
    fn my_generic_method<U>(&self) {}
}
assert_eq!(pretty_name::of_method!(MyStruct::my_method), "MyStruct::my_method");
assert_eq!(pretty_name::of_method!(MyStruct::my_generic_method::<u32>), "MyStruct::my_generic_method::<u32>");
assert_eq!(pretty_name::of_method!(<MyGenericStruct<u32>>::my_method), "<MyGenericStruct<u32>>::my_method");
assert_eq!(pretty_name::of_method!(<MyGenericStruct<u32>>::my_generic_method::<String>), "<MyGenericStruct<u32>>::my_generic_method::<String>");