Skip to content
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

Add Fields::iter and IntoIterator for &Fields #324

Merged
merged 3 commits into from
Jan 16, 2018
Merged

Conversation

sgrif
Copy link
Contributor

@sgrif sgrif commented Jan 15, 2018

These methods were removed in syn 0.12. They seem like an odd removal,
since they're pretty widely used in the wild. This adds these methods
back. Though Fields is the closest structurally to VariantData, I've
opted to put it on DataStruct instead, as it's in the location most
people will look for it. fields.fields() also reads really poorly.

Fixes #316

These methods were removed in syn 0.12. They seem like an odd removal,
since they're pretty widely used in the wild. This adds these methods
back. Though `Fields` is the closest structurally to `VariantData`, I've
opted to put it on `DataStruct` instead, as it's in the location most
people will look for it. `fields.fields()` also reads really poorly.

Fixes dtolnay#316
@dtolnay
Copy link
Owner

dtolnay commented Jan 15, 2018

Thanks!

@sgrif
Copy link
Contributor Author

sgrif commented Jan 15, 2018

Do you think it would be possible to do this the way proposed in #316 with a static empty Punctuated?

Not on stable. An empty Punctuated would require const fn, and allowing Drop types in statics (or did that get stabilized?). We could use lazy_static! for this, but that felt like overkill vs just adding a small iterator.

I think the methods should be on Fields and not DataStruct. A couple other syntax tree nodes also contain Fields: Variant and ItemStruct.

How would you feel about putting that method on Fields, but then adding a delegator for it in DataStruct (as well as Variant and ItemStruct if you'd like)

@sgrif
Copy link
Contributor Author

sgrif commented Jan 15, 2018

To avoid the fields.fields() naming how about AsRef for Fields and AsMut for Fields?

This can't be done unless we have a static mut EMPTY_FIELDS: Punctuated<Field, Token![,]>, which as I mentioned above we can't do on stable (even if we could, is it worth introducing unsafe code?)

@dtolnay
Copy link
Owner

dtolnay commented Jan 16, 2018

Is there a use case for fields_mut? I see that you added it in #26 but I see nothing in diesel's history with git log -S fields_mut and I can't find a single caller in github search.

@sgrif
Copy link
Contributor Author

sgrif commented Jan 16, 2018

I definitely had one at the time I wrote that PR (or I wouldn't have written it), but clearly I didn't go with that implementation. I don't have one off the top of my head. I personally think it's worth having for parity with iter_mut but I'm fine with removing if you'd like

@dtolnay
Copy link
Owner

dtolnay commented Jan 16, 2018

I would drop the mut one for now if nobody will use it.

What do you think of adding IntoIterator for &Fields instead of a fields() method? I think for field in &data.fields is clear enough, works equally well for fields in a Variant or ItemStruct, avoids having a fields field and fields() method on the same types that do different things, and avoids fields.fields() stutter.

@sgrif
Copy link
Contributor Author

sgrif commented Jan 16, 2018

I can do that. I do think it's worth continuing to have the explicit iter method on fields in that case though, for the same reason that virtually every type which implements IntoIterator for & does.

@dtolnay
Copy link
Owner

dtolnay commented Jan 16, 2018

Seems reasonable. Let's do Fields::iter and IntoIterator for &Fields.

@sgrif
Copy link
Contributor Author

sgrif commented Jan 16, 2018

There you go. :)

@sgrif
Copy link
Contributor Author

sgrif commented Jan 16, 2018

I just figured out how to get rid of MaybeEmpty, too. I'll push that up shortly

While we can't easily have an empty `Punctuated` with static storage,
since `punctuated::Iter` is just `slice::Iter`, we can absolutely create
an empty one of those.
@sgrif
Copy link
Contributor Author

sgrif commented Jan 16, 2018

This should be good to go if CI agrees

Copy link
Collaborator

@mystor mystor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sgrif Thanks for getting rid of MaybeEmpty :-). Definitely a cleaner API now.

@mystor mystor merged commit 03a0920 into dtolnay:master Jan 16, 2018
@dtolnay dtolnay changed the title Bring back VariantData::fields and VariantData::fields_mut Add Fields::iter and IntoIterator for &Fields Jan 17, 2018
@sgrif sgrif deleted the sg-fields branch January 31, 2018 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a Fields::fields() method which returns a &Punctuated<Field, Token![,]>
3 participants