Crates.io | same_enum |
lib.rs | same_enum |
version | 0.1.2 |
source | src |
created_at | 2024-07-19 00:04:44.345475 |
updated_at | 2024-07-19 00:21:19.008451 |
description | Generates `From` trait implementations for enums with the same unit variants. |
homepage | |
repository | |
max_upload_size | |
id | 1308154 |
size | 9,312 |
Generates From
trait implementations for enums with the same unit variants.
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.
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.
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);
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,
}