#enums #variant #macro-derive #companion #generate #fields #specifying

enum-tags

A Derive-Macro library that generates a companion tag-enum for any enum so that variants can be referred to without specifying fields

1 unstable release

0.1.0 Feb 23, 2022

#2308 in Rust patterns

Download history 17/week @ 2024-03-11 8/week @ 2024-03-18 17/week @ 2024-03-25 39/week @ 2024-04-01 7/week @ 2024-04-08 59/week @ 2024-04-15 70/week @ 2024-04-22 44/week @ 2024-04-29 15/week @ 2024-05-06 33/week @ 2024-05-13 25/week @ 2024-05-20 38/week @ 2024-05-27 53/week @ 2024-06-03 47/week @ 2024-06-10 49/week @ 2024-06-17 22/week @ 2024-06-24

172 downloads per month
Used in 3 crates (2 directly)

MIT license

5KB

enum-tags

A Derive-Macro library that generates a companion tag-enum for any enum so that variants can be referred to without specifying fields.

Usage

Add this to your Cargo.toml:

[dependencies]
enum-tags = "0.1.0"

Then derive [Tag] for any enum you want to generate a companion tag-enum for.

#[derive(Tag)]
enum MyEnum {
	A,
	B = 1024,
	C(char),
	D { x: i32, y: i32 },
}

The generated enum will look like this:

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MyEnumTag {
	A,
	B = 1024,
	C,
	D,
}

An impl for the TaggedEnum trait will also be generated to allow conversion from your enum type to the tag-enum. The generated impl will look like this:

impl ::enum_tags_traits::TaggedEnum for MyEnum {
	type Tag = MyEnumTag;
	fn tag(&self) -> Self::Tag {
		match *self {
			Self::A => Self::Tag::A,
			Self::B => Self::Tag::B,
			Self::C(_) => Self::Tag::C,
			Self::D { x: _, y: _ } => Self::Tag::D,
		}
	}
}

lib.rs:

Struct Test

use enum_tags::Tag;
#[derive(Tag)]
struct Bad {}

Union Test

use enum_tags::Tag;
#[derive(Tag)]
union Bad {}

Dependencies

~1.5MB
~35K SLoC