Crates.io | enum_variant_macros |
lib.rs | enum_variant_macros |
version | 0.3.0 |
source | src |
created_at | 2022-02-23 02:24:33.722334 |
updated_at | 2023-04-08 19:57:39.264376 |
description | Macros to generate From & TryFrom for enums |
homepage | |
repository | https://github.com/MisterEggnog/enum-variant-macros |
max_upload_size | |
id | 537580 |
size | 21,687 |
Macro library for use in generating From & TryFrom implementations for enums composed of unnamed single members. In other terms, of the following format.
pub enum Variants {
Integer(i32),
Float(f32),
Bool(bool),
Byte(u8),
}
This library has 2 macros, TryFromVariants & FromVariants.
TryFromVariants implements TryFrom for each of the variant types, permitting code such as,
use enum_variant_macros::*;
use std::error::Error;
use strum_macros::IntoStaticStr;
#[derive(IntoStaticStr, TryFromVariants)]
enum Variants {
Integer(i32),
Float(f32),
}
fn main() -> Result<(), Box<dyn Error>> {
let variant = Variants::Float(12.0);
let float: f32 = TryFrom::try_from(variant)?;
Ok(())
}
Note: Derivation of this type also requires that impl From<Variant> for &'static str
is implemented.
FromVariants is relatively simple, it just generates From for each wrapped type.
use enum_variant_macros::*;
#[derive(Debug, PartialEq, FromVariants)]
enum Variants {
Integer(i32),
Float(f32),
}
let variant = Variants::from(12);
assert_eq!(Variants::Integer(12), variant);
If this library is not to your preference, may I recomend enum dispatch, a more expansive library that also provides from & try_into for enums.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.