penum is a procedural macro that is used to make an enum follow a given pattern, which can include generics with trait bounds.
Installation
This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:
[]
= "0.1.10"
Or run this command in your cargo project:
Overview
A pattern consists of one or more shapes and an optional where clause, which would auto bind all the concrete types that matches your pattern--with the trait bounds you've specified.
shapecan either beNamed,UnnamedorUnit, and are used to validate variants.whereclause is used to bind generic parameters to traits.genericparameter CAN ONLY be declared with capital letters or underscore. e.g(T, FOO, BAR)are valid generic parameters, but(t, Foo, BaR)are not, they are considered as concrete types.
Supported
- Shapes
(_, _) | {num: _} | unit - Generics
(T, U) | {num: T} - Bounds
(T, U) where T: Copy, U: Clone - Variadic
(T, U, ..) | {num: T, ..}
Unsupported
Static dispatch- auto implement commonstdtraits.RangeLit- variadic fields by range(T, U, ..4) | {num: T, ..3}VariadicLit- variadic fields with bounds(T, U, ..Copy) | {num: T, ..Copy}Discriminants- support for#ident(T) = func(#ident), or something..
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 make the enum conform to a specific pattern, like this:
// This forces all current and future variants to
// contain one field which must implement `Copy`.
..which would expand to the first example above.
Examples
It's also possible to make an enum conform to multiple shapes by seperating a shape with | symbol, for example:
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 _:
Demo
use shape;
// Note that this shape has a name (`tuple`). Right now
// it doesn't do anything,but there is an idea of using
// regexp to be able to validate on Variant names too.
// Also, there is thoughts about using these Idents to
// specify other rules, like if penum should auto implement
// a static dispatch for a certain pattern. But this could
// also be done by other rules.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`the