Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Solutions to the Space Age exercise.
Mentoring
First iteration
The solution in b380536 manually implements the trait, copy-paste style.
I never requested mentoring on this since it was so straightforward and there were hints to use a macro to simplify.
Second iteration
I thought: "the usual way to auto-implement traits is to use a derive-directive", and so I did in 1674489
This, more or less, requires that you use additional crates--in particular,
syn
--to parse the token stream into a structured syntax tree.I realized afterward that they were looking to use a declarative macro to both define the struct and implement the trait.
However, a derive directive has its benefits. For example, you can slap a
Planet
implementation onto any struct you want.Or, maybe we could rename the trait more generally as
SunOrbiter
so Pluto can join as well =)I consciously added the
seconds
field to the Duration struct because I find it less ambiguous compared to a tuple struct. It's helpful to be specific about the intended unit. But this also made me think that an enum might be more suited: then one could choose if the duration is in seconds, days, or years.The mentor thought I did good:
They added a suggestion that I could use a
parse_meta
method to improve:Third iteration
I thought I'd take the suggestion and see how it went.
However, the suggestion is only valid for
syn
1.0, but the package is currently in 2.0 where this behavior has changed.The replacement module
syn::meta
and the related methodAttribute::parse_nested_meta
only allowMeta::List
as attributes. I'd have to redefine my helper to be#[orbit_period(years = 0.123)]
.I find this strange since, to me, it seems that a
MetaItem
in an attribute according to the Rust reference allowsSimplePath = Expression
.In any case, it might be nice to also have the unit of the orbital period in the macro, so I submitted a modified version.
The mentor did not offer any additional constructive comments.