-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b643d4a
commit 877edbc
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Meta variant structs and enums | ||
|
||
Meta variants are an optional feature, useful for scenarios where you'd want nested | ||
enums at the top-level. structs will be created for all combinations of `meta_variants` | ||
and `variants`, names in the format `{BaseName}{MetaVariantName}{VariantName}`. | ||
Additionally, enums will be created for each `meta_variant` named `{BaseName}{MetaVariantName}`. | ||
|
||
For example: | ||
|
||
```rust,no_run,no_playground | ||
#[superstruct(meta_variants(Baz, Qux), variants(Foo, Bar))] | ||
struct MyStruct { | ||
name: String, | ||
#[superstruct(only(Foo))] | ||
location: u16, | ||
#[superstruct(meta_only(Baz))] | ||
score: u64, | ||
#[superstruct(only(Bar), meta_only(Qux))] | ||
id: usize, | ||
} | ||
``` | ||
|
||
Here the `BaseName` is `MyStruct` and there are two variants in the meta-enum called | ||
`Baz` and `Qux`. | ||
|
||
The generated enums are: | ||
|
||
```rust,no_run,no_playground | ||
enum MyStruct{ | ||
Baz(MyStructBaz), | ||
Qux(MyStructQux), | ||
} | ||
enum MyStructBaz{ | ||
Foo(MyStructBazFoo), | ||
Bar(MyStructBazBar), | ||
} | ||
enum MyStructQux{ | ||
Foo(MyStructQuxFoo), | ||
Bar(MyStructQuxBar), | ||
} | ||
``` | ||
|
||
The generated variant structs are: | ||
|
||
```rust,no_run,no_playground | ||
struct MyStructBazFoo { | ||
name: String, | ||
location: u16, | ||
score: u64, | ||
} | ||
struct MyStructBazBar { | ||
name: String, | ||
score: u64, | ||
} | ||
struct MyStructQuxFoo { | ||
name: String, | ||
location: u16, | ||
} | ||
struct MyStructQuxBar { | ||
name: String, | ||
id: usize, | ||
} | ||
``` | ||
|
||
Note how the `only` attribute still applies, and a new `meta_only` attribute can be used to | ||
control the presence of fields in each meta variant. | ||
|
||
For more information see [Struct attributes](../config/struct.md). |
This file contains 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