-
-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should syn have convenience functions for (derive) macros? #1037
Comments
One that I had to use recently was on pub trait ToSpan {
fn to_span(self) -> Span;
}
impl ToSpan for Data {
fn to_span(self) -> Span {
match self {
Data::Struct(d) => d.struct_token.span(),
Data::Enum(d) => d.enum_token.span(),
Data::Union(d) => d.union_token.span(),
}
}
} Now I realize that implementing |
I found myself reaching for a number of conveniences that were outside |
They need to go in a separate crate. The suggestion of adding various conveniences would make this crate effectively unbounded in scope which is not appealing to me or the users who rely on it to compile fast. |
There's a few things I often need while writing a derive macro, which I keep writing small helper functions for. I'm wondering if these should be part of
syn
, or if they should go in a separate crate.An example:
I often want to iterate over the
Member
s of a list ofFields
, such thatUnit
results in an empty iterator,Tuple
results inUnnamed(0)
,Unnamed(1)
, etc., andStruct
results inNamed("field1")
,Named("field2")
etc. This makes it easy to use e.g.quote!{ #structname { #( #member: Trait::thing(), )* } }
and have it work for all three kinds of structs.(Concrete use case example: It'd reduce this
match
to just a single case.)This doesn't seem to exactly fit with the rest of
syn
, because the tuple indexes (0
,1
, ..) don't occur in the source code, but are generated for the convenience of proc macros.A second example:
An
.expect_struct()
onData
that would return aResult<DataStruct>
, containing a nice error in case of an enum or union (with the span set to the enum or union token). (Or a.expect_unit()
onFields
, etc.)I have a few more examples like this. But I first wanted to ask the general question: Would
syn
be the right place for functions like this, or shouldsyn
only focus on parsing and keep other functionality to a minimum?The text was updated successfully, but these errors were encountered: