Github
Github Download Tests crates.io docs.rs

penum is a procedural macro that is used to make an enum conform to a given pattern that can include generics with trait bounds, which then allows for static dispatching. It's a tool for asserting how enums should look and behave through simple expressive rust grammar.

Allowing developers to assert how an enum should look and behave.

Installation

This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml: toml [dependencies] penum = "0.1.16" Or run this command in your cargo project: sh $ cargo add penum

Overview

A Penum expression can look like this: ```text

[penum( (T) where T: Trait )]

     ^^^       ^^^^^^^^
     |         |
     |         Predicate bound
     |
     Pattern fragment.

``` note that there can be multiple patterns fragments and predicate bounds.

Here's how it would look like if we wanted to dispatch a trait. ```text

[penum( (T) where T: ^Trait )]

                  |
                  Dispatch symbol

`` Penumis smart enough to infer certain return types for non-matching variants. e.gOption,&Option,String,&str. It can even handle&String, referenced non-const types. The goal is to support any type that implementedDefault`.

Note, when dispatching traits with associated types, it's important to declare them. e.g Add<i32, Output = i32>.

Trivial example:

Here we have an enum with one unary and one binary tuple variant where the field type Storage and Something implements the trait Trait. The goal is to be able to call the trait method through Foo. This can be accomplished automatically marking the trait with a dispatch symbol ^. ```rust

[penum{ unit | (T) | (_, T) where T: ^Trait }]

enum Foo { V1(Storage), V2(i32, Something), V3 } ```

rust struct Storage; struct Something; trait Trait { fn method(&self, text: &str) -> &Option<&str>; } impl Trait for Storage {} impl Trait for Something {}

Examples

Used penum to force every variant to be a tuple with one field that must implement Copy.

```rust

[penum( (T) where T: Copy )]

enum Guard { Bar(String), ^^^^^^ // ERROR: String doesn't implement Copy

Bor(Option<&str>), 
    ^^^^^^^^^^^^
// ERROR: `Option<&str>` doesn't implement `Copy`

Bur(Vec<i32>), 
    ^^^^^^^^
// ERROR: `Vec<i32>` doesn't implement `Copy`

Bir(i32, i32), 
   ^^^^^^^^^^
// ERROR: `(i32, i32)` doesn't match pattern `(T)`

Byr(), 
^^^^^
// ERROR: `Byr()` doesn't match pattern `(T)`

Bxr { name: usize }, 
    ^^^^^^^^^^^^^^^
// ERROR: `{ nname: usize }` doesn't match pattern `(T)`

Brr,
^^^
// ERROR: `Brr` doesn't match pattern `(T)`

Beer(i32) // Works!

} ```

Under development

| Traits | Supported | | ---------- | ------------- | |Any| supported | |Borrow| supported | |BorrowMut| supported | |Eq| supported | |AsMut| supported | |AsRef| supported | |From| supported | |Into| supported | |TryFrom| supported | |TryInto| supported | |Default| supported | |Binary| supported | |Debug| supported | |Display| supported | |LowerExp| supported | |LowerHex| supported | |Octal| supported | |Pointer| supported | |UpperExp| supported | |UpperHex| supported | |Future| supported | |IntoFuture| supported | |FromIterator| supported | |FusedIterator| supported | |IntoIterator| supported | |Product| supported | |Sum| supported | |Copy| supported | |Sized| supported | |ToSocketAddrs| supported | |Add| supported | |AddAssign| supported | |BitAnd| supported | |BitAndAssign| supported | |BitOr| supported | |BitOrAssign| supported | |BitXor| supported | |BitXorAssign| supported | |Deref| supported | |DerefMut| supported | |Div| supported | |DivAssign| supported | |Drop| supported | |Fn| supported | |FnMut| supported | |FnOnce| supported | |Index| supported | |IndexMut| supported | |Mul| supported | |MulAssign| supported | |MultiMethod| supported | |Neg| supported | |Not| supported | |Rem| supported | |RemAssign| supported | |Shl| supported | |ShlAssign| supported | |Shr| supported | |ShrAssign| supported | |Sub| supported | |SubAssign| supported | |Termination| supported | |SliceIndex| supported | |FromStr| supported | |ToString| supported |