Skip to content

Commit 93f3170

Browse files
committed
Add the alternative suggested by @oli-obk
1 parent e7fd9c4 commit 93f3170

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

text/0000-arbitrary_enum_discriminant.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,12 @@ This introduces one more knob to the representation of enumerations.
231231

232232
Reusing the current syntax and semantics for explicit discriminants of
233233
field-less enumerations means that the changes to the grammar and semantics of
234-
the language are minimal. The alternative is to put the specified discriminants
235-
in variant attributes, but this would be at odds with the syntax for field-less
236-
enumerations.
234+
the language are minimal. There are a few possible alternatives nonetheless.
235+
236+
## Discriminant values in attributes
237+
238+
We could specify the discriminant values in variant attributes, but this would
239+
be at odds with the syntax for field-less enumerations.
237240

238241
```rust
239242
enum ParisianSandwichIngredient {
@@ -246,6 +249,30 @@ enum ParisianSandwichIngredient {
246249
}
247250
```
248251

252+
## Use discriminants of a separate field-less enumeration
253+
254+
We could tell rustc to tie the discriminants of the enumeration to the
255+
variants of a separate field-less enumeration.
256+
257+
```rust
258+
#[discriminant(IngredientKind)]
259+
enum ParisianSandwichIngredient {
260+
Bread(BreadKind),
261+
Ham(HamKind),
262+
Butter(ButterKind),
263+
}
264+
265+
enum IngredientKind {
266+
Bread,
267+
Ham,
268+
Butter,
269+
}
270+
```
271+
272+
This isn't applicable if such a separate field-less enumeration doesn't exist,
273+
and this can easily be done as a procedural macro using the feature described
274+
in this RFC. It also looks way more like spooky action at a distance.
275+
249276
# Prior art
250277
[prior-art]: #prior-art
251278

0 commit comments

Comments
 (0)