1 unstable release

0.1.0 Feb 23, 2022
Download history 23/week @ 2023-10-19 17/week @ 2023-10-26 18/week @ 2023-11-02 13/week @ 2023-11-09 19/week @ 2023-11-16 20/week @ 2023-11-23 15/week @ 2023-11-30 14/week @ 2023-12-07 24/week @ 2023-12-14 36/week @ 2023-12-21 13/week @ 2023-12-28 26/week @ 2024-01-04 66/week @ 2024-01-11 44/week @ 2024-01-18 94/week @ 2024-01-25 49/week @ 2024-02-01

258 downloads per month
Used in 2 crates

MIT license

2KB

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,
		}
	}
}

No runtime deps