penum
penum is a procedural macro that is used to make an enum follow a given pattern, which can include generics with trait bounds.
A pattern consists of one or more shapes and an optional where clause, which will autobind the concrete types specified for you.
shapecan either beNamed,UnnamedorUnit, and are used to validate variants.where clauseare used to bind the generic parameters to traits.
Use case
Normally, using a generic in an enum means that it gets applied to the whole enum, and not per variant.
For example, if I want to specify that all variants should be a tuple(T) where T must implement Copy,
I'd have to specify a generic for all variants:
This seems kind of tedious, because all we want to do is to be able to make the enum conform to a specific pattern, like this:
]
..which would expand to the first example above.
Examples
There are much more one could do with this, for example, one could specify that an enum should follow a pattern with multiple different shapes:
]
Also, If an enum should break a pattern, like if a variant doesn't implement the correct Trait,
an error would occur:
]
..or if a variant doesn't match the specified shape:
]
Sometime we don't care about specifying a where clause and just want our enum to follow a specific shape.
This is done by specifing _:
]
Future support
- Discriminants
- Static dispatch (i.e auto impl for
stdtraits) - Spread operator
Demo
use shape;
]
]
]
]
]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`the