penum is a procedural macro that is used to make an enum conform to a given pattern, which can include generics with trait bounds. Pattern matching, but for enums!
Installation
This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:
[]
= "0.1.11"
Or run this command in your cargo project:
Overview
- A
penumexpression consists of apatternand an optionalwhere clause. - A
patterncan consists of multiplepattern fragmentsthat comes in threegroup-flavors,Named {...},Unnamed (...)andUnit. They are then used for pattern matching against variants. - A
group's inner parts can consist of:genericparameters and are used asnamed polymorphic placeholdersthat CAN have trait bounds, but CAN ONLY be declared with capital letters.placeholderparameters and are used asunnamed polymorphic placeholdersthat CANNOT have trait bounds, and are declared with undercore_.variadicparameters which arepolymorphic rest placeholdersand CAN ONLY be declared as the last argument, and only once (for now).
where clauseis used to bind generic parameters to traits which puts constraints on the concrete types that follows.
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, but where T, U and F are replaced with i32, u32 and f32.
Supported
- Shapes
(...) | {...} - Generics
(T, U) | {num: T} - Placeholders
(_, _) | {num: _} - Bounds
(T, U) where T: Copy, U: Clone - Variadic
(T, U, ..) | {num: T, ..}
Under development
Static dispatch- auto implementcore/std/customtraits (read more).
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 _:
Other times we only care about the first varaint field implementing a trait:
Demo
use shape;
// `(T, FOO, BAR)` are valid generic parameters, but `(t, Foo, BaR)` are not,
// they are considered as **concrete** types.
// 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
Unsupported
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..