-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Closed
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-parserArea: The lexing & parsing of Rust source code to an ASTArea: The lexing & parsing of Rust source code to an ASTA-visibilityArea: Visibility / privacyArea: Visibility / privacyC-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-langRelevant to the language teamRelevant to the language team
Description
I want to create a macro that generate fn
s 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
)
Metadata
Metadata
Assignees
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-parserArea: The lexing & parsing of Rust source code to an ASTArea: The lexing & parsing of Rust source code to an ASTA-visibilityArea: Visibility / privacyArea: Visibility / privacyC-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-langRelevant to the language teamRelevant to the language team