Add a repr option for enums instead of relying on a feature#1539
Open
zackyancey wants to merge 1 commit into
Open
Add a repr option for enums instead of relying on a feature#1539zackyancey wants to merge 1 commit into
repr option for enums instead of relying on a feature#1539zackyancey wants to merge 1 commit into
Conversation
The `#[repr()]` attribute is a language-level attribute that affects the
internal representation of the enum. It doesn't on its own say anything about
how the type is serialized, so we need some option to tell us whether to use
the `#[repr()]`
The current way of doing that, using the `repr` crate feature to control this
functionality, has a problem. If I have an enum definition like this:
```rust
// Note serde::Serialize, not serde_repr::Serialize, so I need strings, not
// ints, in the schema
#[derive(serde::Serialize, ToSchema)]
#[repr(u8)] // repr is specified, but nothing to do with serialization
enum StringEnum {
A = 0,
B = 1
}
```
What I need to do is not enable the `repr` feature. However, because features
are addative and unified across all crates, if *any other* dependency of mine
enables the repr feature, then the schema for `StringEnum` will be wrong.
This is something that needs to be specified per-schema, eg
```rust
#[derive(serde::Serialize, ToSchema)]
#[schema(repr)]
#[repr(u8)]
enum IntEnum {
A = 0,
B = 1
}
// To avoid the ambiguity caused by the feature, you can specify repr = false
// to ignore the `repr` feature if it's enabled.
#[derive(serde::Serialize, ToSchema)]
#[schema(repr = false)]
#[repr(u8)]
enum StringEnum {
A = 0,
B = 1
}
```
This change leaves the `repr` crate feature unchanged if no `#[schema(repr)]`
is specified, but if it is then that value will be used regardless of the state
of the `repr` feature. This lets new code use the unambiguous syntax without
breaking anything that depends on the feature. I think the `repr` crate feature
should probably be removed in the next major release.
c1de050 to
bfd7d34
Compare
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.
This is to address this issue:
The
#[repr()]attribute is a language-level attribute that affects the internal representation of the enum. It doesn't on its own say anything about how the type is serialized, so we need some option to tell us whether to use the#[repr()]The current way of doing that, using the
reprcrate feature to control this functionality, has a problem. If I have an enum definition like this:What I need to do is not enable the
reprfeature. However, because features are addative and unified across all crates, if any other dependency of mine enables the repr feature, then the schema forStringEnumwill be wrong.This is something that needs to be specified per-schema, eg
This change leaves the
reprcrate feature unchanged if no#[schema(repr)]is specified, but if it is then that value will be used regardless of the state of thereprfeature. This lets new code use the unambiguous syntax without breaking anything that depends on the feature. I think thereprcrate feature should probably be removed in the next major release.