of_method

Macro of_method 

Source
macro_rules! of_method {
    ($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 as &'static str.

This macro returns &'static str. It caches the result and subsequent calls have zero runtime overhead.

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.

This macro checks that the method exists on the given type. If either the type or method is renamed via refactoring tools, the macro call will be updated accordingly.

Due to implementation limitations, you cannot use ::<..> syntax 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>");