#unit #enums #traits #variant #generate #partial-eq #debugging

macro same_enum

Generates From trait implementations for enums with the same unit variants

3 releases

0.1.2 Jul 19, 2024
0.1.1 Jul 19, 2024
0.1.0 Jul 19, 2024

#833 in Procedural macros

Download history 291/week @ 2024-07-14 22/week @ 2024-07-21 45/week @ 2024-07-28 2/week @ 2024-08-04 33/week @ 2024-09-15 27/week @ 2024-09-22 38/week @ 2024-09-29 2/week @ 2024-10-06

100 downloads per month

MIT license

8KB
76 lines

same_enum

Generates From trait implementations for enums with the same unit variants.

Get Started

Place a #[from(path::to::TargetEnum)] or #[to(path::to::TargetEnum)] attribute above the source enum definition to generate From trait implementations.

#[from(path::to::TargetEnum)]

Generates an implementation of the From trait to convert from a target enum to the annotated enum.

Example

use same_enum::from;
#[from(TargetEnum)]
#[derive(PartialEq, Debug)]
enum SourceEnum {
    One,
    Two,
}
#[derive(PartialEq, Debug)]
enum TargetEnum {
    One,
    Two,
}

let target_one = TargetEnum::One;
let source_one: SourceEnum = target_one.into(); // from implementation gives us into() as well

assert_eq!(source_one, SourceEnum::One);

#[to(path::to::TargetEnum)]

Generates an implementation of the From trait to convert from the annotated enum to a target enum.

Example

use same_enum::to;
#[to(TargetEnum)]
#[derive(PartialEq, Debug)]
enum SourceEnum {
    One,
    Two,
}
#[derive(PartialEq, Debug)]
enum TargetEnum {
    One,
    Two,
}

let source_one = SourceEnum::One;
let target_one = TargetEnum::from(source_one);

assert_eq!(target_one, TargetEnum::One);

Panics

same_enum panics if the annotated_item is not an enum with Unit variants.

An example of an enum with Unit variants would be:

enum BinaryNumbers {
    Zero,
    One,
}

Dependencies

~1.5MB
~36K SLoC