Support infallible EnumString for enums with a default variant #432
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.
Any enum with a
#[strum(default)]variant has infallible parsing in practice, but the compiler does not know this because the error type is stillstrum::ParseError.This PR proposes a non-breaking change to address the issue: Define a new type-level attribute
parse_infallible, which instructsEnumStringto usestd::convert::InfallibleforFromStr::Err, and to deriveFrom<&str>(always) instead ofTryFrom<&str>(1.34+). This allows e.g.and (in 1.82+)
Existing
EnumStringderivations remain unchanged unless the user adds theparse_infallibleattribute. As a future breaking change, we could start inferringparse_infallibleautomatically even for enums that do not carry the annotation. If a breaking change is immediately acceptable, we don't need to define the new attribute at all, and this PR becomes simpler.NOTE:
TryFromis still available (for 1.34+) withparse_infallible, because theFrom<&str>derived byEnumStringactivates a blanket implementation of reverseInto, which in turn activates a blanket implementation ofTryFrom.Also update docs and add new unit tests that exercise infallible parsing for both normal and PHF enums.
Fixes #307