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.
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
Enforce constraints for unnamed enums #3884
Enforce constraints for unnamed enums #3884
Changes from all commits
651538a
6deae2f
bfdf606
e410ad8
029a6e3
042fe5c
8226826
2ceceb7
879f350
2d96bc5
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, I wonder why we have this class at all i.e. why we don't test directly against
InfallibleEnumType
. It feels wrong to copy over the implementations from the "real" classes to this class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you on this. I'll see if it can be easily fixed in this PR, otherwise will raise another one for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InfallibleEnumType
allowsUnknownVariants
, whereasTestEnumType
panics when an unknown variant is encountered. I tried composingTestEnumType
to useInfallibleEnumType
internally, butInfallibleEnumType
is defined incodegen-client
, whileTestEnumType
is incodegen-core
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is first converting to a heap-allocated
String
and only then matching on the enum values. So invalid enum values would unnecessarily be heap-allocated. We should makeTryFrom<String>
andTryFrom<&str>
instead delegate toFromStr
, which should only heap-allocate when the enum value is valid.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike named enums, which can use
&str
for comparison and directly return an enum variant, unnamed enums need to store an owned, heap-allocatedString
. TheTryFrom<String>
implementation already receives an ownedString
as a parameter, so delegating toFromStr
would result in an unnecessary heap allocation. Additionally,ConstraintViolation
takes ownership of a heap-allocatedString
. Therefore, both code paths (valid and invalid enum values) require a heap-allocatedString
. Callingto_owned
withinTryFrom<&str>
only shifts the allocation slightly earlier without adding any additional allocation.