Add IsVariantAnd derive - #561
Open
ChrisJr404 wants to merge 2 commits into
Open
Conversation
Stable no longer prints the 'see issue #48214' help line, so the non_eq_field trybuild snapshot no longer matched.
Author
|
The failing stable check was the eq/non_eq_field compile_fail snapshot, not this derive. Newer stable rustc dropped the 'see issue #48214' help line, so I reblessed that .stderr to match. Passes locally now with --features full. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #365.
This adds an
IsVariantAndderive: for each enum variantfooit generates anis_foo_and(&self, f) -> boolmethod that returnstrueonly when the value is that variant and the given closure returnstruefor its contents. It's theOption::is_some_andpattern generalised to any enum, which is handy for matching on a variant and checking something about its payload in a single expression instead of amatches!or amatcharm.I modelled the API on the existing
IsVariantandUnwrapderives:&self(likeIsVariant), so it's non-consuming and works on borrowed enums.Unwrapreturns them. A single-field variant collapses to a plain reference, soJust(T)givesis_just_and(|x: &T| ...), exactly mirroringOption::is_some_and.()or empty record{}) take aFnOnce() -> boolclosure instead.#[is_variant_and(ignore)]skips a variant, consistent with#[is_variant(ignore)].It's behind a new
is_variant_andfeature flag (added tofull), with docs inimpl/doc/is_variant_and.md, a CHANGELOG entry, and integration tests intests/is_variant_and.rscovering tuple/struct/unit variants, references, generics with bounds,ignore, a single-variant enum, and thenightly/deprecatedcases theIsVarianttests exercise.One thing worth a look during review: I went with references-in-a-tuple for the closure argument rather than consuming
selflikeOption::is_some_anddoes, since a consuming derive felt surprising here andIsVariantalready established&self. Happy to change the shape if you'd prefer something else.