#enums #light #macro-derive #value #values #derive #macro

macro bin light_enum

provide a derive keyword to generate a light enum

4 releases

0.2.2 Nov 15, 2023
0.2.1 Nov 12, 2023
0.2.0 Nov 11, 2023
0.1.0 Oct 31, 2023

#1963 in Rust patterns

Download history 40/week @ 2024-07-21 33/week @ 2024-07-28 12/week @ 2024-08-04 10/week @ 2024-08-11 18/week @ 2024-08-18 9/week @ 2024-08-25 12/week @ 2024-09-01 7/week @ 2024-09-08 10/week @ 2024-09-15 31/week @ 2024-09-22 19/week @ 2024-09-29 10/week @ 2024-10-06 28/week @ 2024-10-13 17/week @ 2024-10-20 26/week @ 2024-10-27 27/week @ 2024-11-03

100 downloads per month

MIT license

9KB
113 lines

light_enum

crates.io docs.rs license ci_status

A crate providing a derive keyword to generate a light enum.

Usage

cargo add light_enum

This crate provide two derive keywords:

  • LightEnum will generate a new enum without the content of each field
  • Values will generate a vector containing each field of the enum

See examples bellow to understand what they do.

LightEnum

use light_enum::LightEnum;

#[derive(LightEnum)]
enum MyEnum {
    A(i32, i32),
    B(i32),
    C,
}

let heavy = MyEnum::A(0, 0);
let light = heavy.to_light();
assert!(light == MyEnumLight::A);

MyEnumLight will be generated:

enum MyEnumLight {
    A,
    B,
    C,
}

Values

use light_enum::Values;

#[derive(Values, PartialEq, Eq)]
enum Vals {
    A,
    B,
    C,
}

let values = Vals::VALUES;
assert!(values.len() == 3);
assert!(values.contains(&Vals::A));
assert!(values.contains(&Vals::B));
assert!(values.contains(&Vals::C));

Generated code:

impl Vals {
    const VALUES: [Vals; 3usize] = [Vals::A, Vals::B, Vals::C];
}

Limitations

Values

Having a field with content will cause an error.

// this code will not compile
#[derive(Values)]
enum MyEnum {
    A(i32),
}

Dev

The cargo expand utility is useful to see what is generated. It can be installed with cargo install cargo-expand.

Dependencies

~240–690KB
~16K SLoC