SpireEnum
A self-proclaimed enum-macro suite for Rust, providing several macros that aim to make enums great again. (they never stopped being great, but I needed a punchline)
-
#[delegated_enum]: Placed on enums, generates a declarative macro that allows you to delegate impls for your enum in a single line. , and/or allows extracting variant types. -
#[delegated_impl]: Placed on impl blocks, works in conjunction with#[delegated_enum]to generate your enum's delegated impls. -
#[variant_type_table]: Place on enums, generates a table type that holds exactly one of each of the enums's variants, as well as several useful implementations for that type. -
#[variant_generic_table]: Place on enums, works similarly to#[variant_type_table], except each value on the table is of a generic parameter instead of the variant's type. -
#[discriminant_generic_table]: Place on enums, works similarly to#[variant_generic_table], except this is meant for enums with unit variants, accessing the values is used by indexing with the enum variant itself(instead of the variant's type). -
For more info on the table macros, see each macro's documentation.
-
For more info on
#[delegated_enum]and#[delegated_impl], keep reading this file.
Table of Contents
- Showcase: What is a delegate enum?
- Key Features
- Overview
- Alternatives
- Usage
- Example: Basic Usage
- Example: State Machine
- Troubleshooting
- Performance
- Contributing
Showcase: What is a delegate enum?
A "delegate enum" is a common pattern where an enum is created to represent possible types that implement a certain trait.
Example
You're implementing a state machine for a turret in your video game:
// Trait that defines what every state should do.
// Some state types
// Their impls
// Now you need some type to store which state a given Turret currently is on.
// The first thing that may come to mind is to simply put it in a `Box<dyn IState>`, but that introduces some problems:
//
// - Serializing cannot be easily done by just deriving `serde::Serialize`, you need to resort to something like [typetag](https://crates.io/crates/typetag).
// - You lose *some* performance: `dyn` uses dynamic dispatch, and you now need to box your states when storing them.
//
// In this case, you know all possible variants of a state at compile time, so you could just store those in an enum:
// Serialization is very straightforward
// All good so far, but then, in your main function:
You can imagine how verbose this gets, you need to match on all the variants, every-single-time, even if you want to call a method that every variant has.
spire_enum can help this case in multiple ways.
First, we can generate those big match statements for you, which you replace by calling the generated delegate_state! macro:
use delegated_enum;
// Your main function turns into:
// The declarative macro `delegate_state!` generates the same code mentioned in the initial example, except it doesn't pollute your view anymore :3
spire_enum can take it a step further though: instead of having you invoke that macro everytime, why not just let it implement the trait IState for your enum?
use delegate_impl;
// Now you can just call those methods directly in the enum:
But that's not where spire_enum stops, in this case, it can also help if you specify the setting extract_variants in your enum:
That setting will make the macro also generate and extract the types for each variant, so you don't need to declare the types anymore
This is how your entire file would look like with the usage of spire_enum
use ;
// Trait
// Enum
// Variant impls
// Enum impl
You thought we were done? Not yet.
When implementing the trait for each variant, there's a good chance you're often converting variants from/into the enum.
That's the least of our worries, but it doesn't mean we can't do better.
Let's have spire_enum generate conversions implementations too (From<Variant> for Enum, TryFrom<Enum> for Variant),
which can be done with the setting impl_conversions:
So now we can just use .into():
There's more this crate can do, but this showcase is already long enough, check the Usage section if you want to know more.
Key Features:
- Delegation: Generates inherent/trait implementations for your enums, by delegating to their inner types.
- Conversions: Generate
From<>/TryFrom<>implementations between your enums and their variants. - Not limited to just plain/simple types: It properly handles generic parameters, lifetimes (bounds included), where clauses, etc.
- Hygiene (IDE friendly!):
- Proc macros desugar into declarative macros.
- Token spans are preserved.
- Macro only uses inputs feed into it, no reflection is performed, no files are read (no IO operations).
- No state is preserved between macro invocations, each invocation is completely isolated.
Overview
SpireEnum provides three macros that work together:
#[delegated_enum]- An attribute macro for defining enums with delegation capabilities.#[delegate_impl]- An attribute macro for implementing traits or methods for the enum.- (Generated on the fly)
delegate_[enum_name]- A declarative macro generated bydelegated_enum, one for each annotated enum.
Macros 1. and 2. work together by:
- When you apply
#[delegated_enum]to an enum, the macro parses the enum definition, its settings and variants. - Based on the analysis, the macro generates:
- A declarative macro named
delegate_[enum_name]that handles the delegation logic. [Optional]A new type for each variant.[Optional]Conversion (From<>,TryFrom<>) implementations between the enum and its variants.
- A declarative macro named
- The
#[delegate_impl]macro can be applied on trait or inherent implementations of the enum, it uses the generateddelegate_[enum_name]macro to implement the method bodies that need delegation.
Alternatives
- enum_delegate - The initial inspiration for this crate, I aimed to provide better features while avoiding that crate's drawbacks (the main ones being lack of hygiene, preserving state between macro invocations).
- delegation - A fork of
enum_delegate, which solves some of the old one's issues - though it takes a different approach compared tospire_enum. - enum_variant_type - Provides similar functionality to this crate's
extract_variantssetting.
Usage
1. #[delegated_enum] (Enum attribute macro)
This attribute is applied to an enum definition to enable delegation capabilities:
use delegated_enum;
The delegated_enum attribute supports several optional settings that control how the enum behaves:
1.1 Basic Usage
When used without any settings, delegated_enum generates:
- A declarative macro named
delegate_[enum_name]that can be used to implement delegated traits for the enum.
Which generates the declared enum, and this macro:
pub use delegate_media_content;
The macro may seem a bit cryptic, but it allows you to manually delegate impls in a very simple way:
// Let's Imagine that all variants of the enum `MediaContent` implement this trait:
// If you were to manually write that implementation for the enum, you would have to:
// That can obviously get very tedious if you're frequently using this pattern,
// that's where the generated macro (`delegate_media_content`) comes in:
Although it can be used manually, the generated declarative macro delegate_media_content is used by the #[delegate_impl] attribute macro in the
exact same way.
1.2 Conversion Settings
These are specified inside #[delegated_enum( **here** )], separated by commas.
1.2.1 impl_conversions
Enable automatic generation of conversion methods between the enum and its variants.
For each Variant:
TryFrom<Enum>forVariantTryFrom<&Enum>for&VariantTryFrom<&mut Enum>for&mut VariantFrom<Variant>forEnum
Which additionally generates:
// ... same for each other variant
These implementations facilitate conversions between the enums and its possible variants, especially when each variant has a unique type.
The setting also generates a set of custom trait implementations provided by spire_enum:
FromEnum/FromEnumRef/FromEnumMutforVariant
These traits allow spire_enum::prelude::EnumExtensions to be implemented for your enum,
which provides even more utilities for enum-variant conversion:
use EnumExtensions;
let media = Text;
assert!;
assert!;
assert_eq!;
assert_eq!;
For more information, read the trait EnumExtensions's documentation.
This setting is configurable in a per-variant basis, you may skip generating the implementations for certain variants by using the attribute # [dont_impl_conversions]:
1.2.2 impl_enum_try_into_variants
Similar to impl_conversions, except it only generates TryFrom<Enum>, TryFrom<&Enum> and TryFrom<&mut Enum> for each variant.
1.2.3 impl_variants_into_enum
Similar to impl_conversions, except it only generates From<Variant> for the enum.
1.3 Variant Types Generation
These are specified inside #[delegated_enum( **here** )], separated by commas.
1.3.1 extract_variants
Generates a new type for each variant:
Which additionally generates:
;
;
;
// And replaces the original enum's variant types with the generated ones:
Note that the declarative macro delegate_[enum_name] is also generated differently to handle the new variant types.
1.3.2 extract_variants( attrs(attribute_list) )
Applies every attribute in (attribute_list) to each generated variant type.
Which will generate:
;
;
;
1.3.3 extract_variants( derive(trait_list) )
Shorthand for attrs(derive(trait_list))
Which will generate:
;
;
;
2. #[delegate_impl] (Inherent/Trait impl attribute macro)
This attribute should be applied to the enum's implementation blocks:
use delegate_impl;
Which generates:
Note that it uses the macro delegate_api_response, which would be generated by the enum annotated with #[delegated_enum].
2.1 Associated Types, Constants and Static Functions
Delegating these items is impossible since there's no enum value to match on, you must write that particular item manually if a trait requires these.
Example:
use ;
// Suppose you have this trait:
// And you wish to apply it to the enum:
// The only item that can be delegated is `fn apply(&self)`, other items must be manually written:
Note that you may still manually write the implementation of fn apply(&self), which will "override" what would be generated by the macro.
3. Variant Attributes
Attributes that can be applied on a per-variant basis.
3.1 #[dont_impl_conversions] / #[dont_extract] (Variant attributes)
3.2 #[delegate_via(|var| var.foo())] (Variant attribute)
When delegating methods, instead of calling the method directly on the variant, call the delegated method on the result of the closure inside
delegate_via.
Example:
This attribute affects how the macro delegate_[enum_name] will be generated, in this, changing it:
// From
// To
3.3 #[delegator] (Variant field attribute)
Use this to delegate method calls to a field of the variant instead of the variant itself.
Example:
This will change the Legacy case in the delegate_[enum_name] macro:
// From
Config::Legacy { version: $ arg, ..} => { $ ( $ Rest) * }
// To
Config::Legacy { config: $ arg, ..} => { $ ( $ Rest) * }
Example: Basic Usage
use ;
let some_variant: Value =..;
println!;
Example: State Machine
SpireEnum is particularly useful for implementing state machines:
// Each state already has its own variant type declared outside the macro, no need to use `extract_variants`.
// Common interface for all states
// Implementation delegated to each state.
Troubleshooting
The macros provided by this crate carefully parse the inputs provided to it, I aimed to provide helpful error messages as reasonably as I could. If you encounter a cryptic error message, please open an issue, I'll do what I can to fix it in a timely manner.
1. "Cannot find macro delegate_[enum_name]"
You're likely using the macro #[delegate_impl] outside of the module that contains your enum.
The macro delegate_[enum_name] is generated alongside the enum annotated with #[delegated_enum],
you need to import it on other modules where #[delegate_impl] is used:
use ;
Performance
The delegation macros generate code that is equivalent to what you would write manually with match statements. There is no runtime overhead compared to manually written code.
Contributing
The best way to contribute is by using the crate and providing feedback. Please open an issue if you'd like to request a feature or report a bug.