Crate enum_variant_macros
source ·Expand description
This macro library is currently incomplete, it works for enums in a very specific format but it is has some rough spots.
To use the TryFromVariants macro, the type needs to provide a From<YourEnum>
to &'static str
for for this derivation to
succeed.
I recommend using strum_macros::IntoStaticStr.
Warning
Note that this only works for enums composed solely of 1 member unnamed variant.
Example
use enum_variant_macros::*;
use strum_macros::IntoStaticStr;
#[derive(PartialEq, Debug, IntoStaticStr, FromVariants, TryFromVariants)]
enum Wrap {
Float(f32),
Int(i32),
}
assert_eq!(Wrap::Int(4), Wrap::from(4_i32));
assert_eq!(Ok(4_i32), i32::try_from(Wrap::Int(4)));
assert!(f32::try_from(Wrap::Int(4)).is_err());
Structs
- Different variant than expected during TryFrom
Derive Macros
- Generates From for each variant of enum.
- Generates TryFrom for each variant of enum.