[][src]Macro structural::z_impl_try_from_structural_for_enum

macro_rules! z_impl_try_from_structural_for_enum {
    (
        impl[ $($impl_params:tt)* ] TryFromStructural<$from:ident> for $self:ty
        where [ $($where_preds:tt)* ]
        {
            type Error= $err:ty;
            fn try_from_structural($from_var:ident){
                $($code:tt)*
            }
        }

        FromStructural
        where [ $($from_where_preds:tt)* ]
    ) => { ... };
}

For implementing TryFromStructural, and delegating the implementation of FromStructural to it.

The implementation of FromStructural inherits all the constraints of the TryFromStructural impl.

In order to implement FromStructural, this macro assumes that the TryFromStructural implementation written by users:

  • Matches on all the variants of the enum

  • Returns Ok for all the variants of the enum that were matches by name.

Example

use structural::{
    convert::{EmptyTryFromError, FromStructural, TryFromError, TryFromStructural},
    Structural, StructuralExt, switch,
};

use std::cmp::Ordering;


assert_eq!(
    EnumAAAA::Foo([9,8,7,6,5,4,3,2,1,0]).try_into_struc::<Variants>(),
    Ok(Variants::Foo(9)),
);

assert_eq!(
    EnumAAAA::Bar{heh: true}.try_into_struc::<Variants>(),
    Ok(Variants::Bar),
);

assert_eq!(
    EnumAAAA::Baz{foom: "hi", uh: Ordering::Less}.try_into_struc::<Variants>(),
    Ok(Variants::Baz{foom: "hi"}),
);

assert_eq!(
    EnumAAAA::Qux.try_into_struc::<Variants>(),
    Err(TryFromError::with_empty_error(EnumAAAA::Qux)),
);


#[derive(Structural, Copy, Clone, Debug, PartialEq)]
#[struc(no_trait)]
enum EnumAAAA {
    // This delegates the `*VariantField` accessor traits to the array,
    // meaning that `.field_(fp!(::Foo.0))` would access the 0th element,
    // `.field_(fp!(::Foo.4))` would access the 4th element of the array,
    // etcetera.
    //
    // If this enum had a `EnumAAAA_SI` trait alias (generated by the `Structural` derive),
    // this variant would only have the `IsVariant<TS!(Foo)>` bound in the trait alias.
    #[struc(newtype)]
    Foo([u8;10]),
    Bar{ heh: bool },
    Baz {
        foom: &'static str,
        uh: Ordering,
    },
    Qux,
}
  
#[derive(Structural, Copy, Clone, Debug, PartialEq)]
enum Variants {
    Foo(u8),
    Bar,
    Baz { foom: &'static str },
}
  

structural::z_impl_try_from_structural_for_enum!{
    impl[F] TryFromStructural<F> for Variants
    where[
        // `Variants_SI` was generated by the `Structural` derive for `Variants`
        // aliasing its accessor trait impls,
        // and allows `F` to have more variants than `Foo`,`Bar`,and `Baz`.
        F: Variants_SI,
    ]{
        type Error = EmptyTryFromError;

        fn try_from_structural(this) {
            switch! {this;
                Foo(x) => Ok(Self::Foo(x)),
                Bar => Ok(Self::Bar),
                Baz{foom} => Ok(Self::Baz{foom}),
                _ => Err(TryFromError::with_empty_error(this)),
            }
        }
    }

    // `Variants_ESI` is like `Variants_SI` with the additional requirement that `F`
    // only has the `Foo`,`Bar`,and `Baz` variants.
    FromStructural
    where[ F: Variants_ESI, ]
}