I want to create a macro that generate fns that use in both impl Item... and trait TraitName. In trait, pub must be omitted, but in impl SomeStruct I need pub, so the macro need a $vis:vis like this:
macro_rules! create_method {
($vis:vis $name:ident) => {
$vis fn $name(&self) {
println!("{}", stringify!($name));
}
}
}
trait T1 {
// This not works
create_method!(method_of_t1);
}
trait T2 {
fn method_of_t2(&self);
}
struct Struct;
impl T2 for Struct {
// This works
create_method!(method_of_t2);
}
impl Struct {
// I want this method to be `pub`, so the macro need a `:vis`
// for receiving the visibility keyword like this:
create_method!(pub another_method);
}
Expected result: create_method!(method_of_t1); in trait T1 should works.
(Edit: a better comment about the :vis in impl Struct)
I want to create a macro that generate
fns that use in bothimpl Item...andtrait TraitName. Intrait,pubmust be omitted, but inimpl SomeStructI needpub, so the macro need a$vis:vislike this:Expected result:
create_method!(method_of_t1);intrait T1should works.(Edit: a better comment about the
:visinimpl Struct)