Macro hotdrink_rs::component_type [−][src]
macro_rules! component_type { ( $(#[$meta:meta])* $vis:vis enum $type_name:ident { $( $constr:ident ),* $(,)? } ) => { ... }; }
A macro for automatically generating an enum that has a variant for each type to use in a constraint system.
It will also implement From
and TryInto
implementations for each of these variants.
Examples
#[derive(Debug, PartialEq)] struct Foo; // Generate the struct and impls hotdrink_rs::component_type! { #[derive(Debug, PartialEq)] enum MyType { i32, f64, Foo } } // Create instance of MyType let mt: MyType = MyType::from(Foo); assert_eq!(mt, MyType::Foo(Foo)); // Try to convert it to f32 let x: Result<f64, ()> = mt.try_into(); assert_eq!(x, Err(())); // Try to convert a MyType to i32 let y: Result<i32, ()> = MyType::from(23).try_into(); assert_eq!(y, Ok(23));