Skip to content

A Rust procedural macro auto-generating common methods on enums.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

makcandrov/enum-impl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

enum-impl

Warning

This crate is in the process of being deprecated in favor of quick-impl, a rework of this crate that provides more features, including handling structures.

enum-impl is a Rust procedural macro that simplifies working with enums by generating common methods and traits for each variant. This helps reduce boilerplate code and enhances the ergonomics of using enums in your Rust projects.

Features

  • [pub] as_ref [= "rename"] Generates a method that returns an immutable reference to the associated data of the enum variant.
  • [pub] as_ref_mut [= "rename"] Generates a method that returns a mutable reference to the associated data of the enum variant.
  • [pub] from [= "rename"] Generates a method that creates an instance of the enum variant from the associated data.
  • impl from Implements the From trait for the enum, creating an instance of the enum variant from the associated data.
  • [pub] into [= "rename"] Generates a method that converts the enum into the variant associated data.
  • [pub] is [= "rename"] Generates a method that returns a boolean indicating whether the enum instance matches the specified variant.

Usage

Add enum-impl to your Cargo.toml:

[dependencies]
enum-impl = "0.1"

In your Rust code:

use enum_impl::EnumImpl;

#[derive(EnumImpl)]
enum YourEnum {
    #[enum_impl(pub is)]
    Variant1,
    #[enum_impl(pub as_ref, as_ref_mut, impl from)]
    Variant2(i32),
    // ... add attributes to other variants as needed
}

fn main() {
    let instance = YourEnum::Variant1;

    // Use generated methods on enum instances
    assert!(instance.is_variant_1());

    let variant2_instance = YourEnum::from(42);
    assert_eq!(*variant2_instance.as_variant_2().unwrap(), 42);
}

More examples can be found in examples.

About

A Rust procedural macro auto-generating common methods on enums.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages