1 unstable release

0.1.0 Feb 23, 2022

#24 in #companion

Download history 3/week @ 2023-12-19 8/week @ 2023-12-26 12/week @ 2024-01-09 39/week @ 2024-01-16 46/week @ 2024-01-23 36/week @ 2024-01-30 44/week @ 2024-02-06 132/week @ 2024-02-13 38/week @ 2024-02-20 44/week @ 2024-02-27 34/week @ 2024-03-05 18/week @ 2024-03-12 14/week @ 2024-03-19 24/week @ 2024-03-26 41/week @ 2024-04-02

101 downloads per month
Used in 2 crates

MIT license

5KB
86 lines

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

Dependencies

~1.5MB
~34K SLoC