[][src]Trait structural::convert::FromStructural

pub trait FromStructural<T>: TryFromStructural<T> {
    fn from_structural(from: T) -> Self;
}

For conversions between structural types.

For enums,usually the from enum has to have same variants and at least the same fields as the into enum.
For structs,usually the from struct has to have at least the same fields as the into struct.

Implementations

Structs

This trait is usually implemented for structs by:

  • Bounding the trait type parameter with the *_SI structural alias generated for the struct by the Structural derive.

  • Calling into_fields on the parameter to convert it into the fields in common with the Self struct.

Enums

This trait is usually implemented for enums by:

  • Bounding the trait type parameter with the *_ESI structural alias generated for the enum by the Structural derive.

  • Matching on all the variants of the enum with the switch macro, returning the same variant.

Struct Example

This example demonstrates how this trait can be manually implemented for structs.

use structural::{convert::FromStructural, StructuralExt, Structural, fp, make_struct};

{
    let this = Point{x: 33, y:45};
    assert_eq!(this.into_struc::<Entity>(), Entity{x: 33, y: 45, health: 100});
}
{
    let this = make_struct!{x: 100, y: 200, foo: "hello"};
    assert_eq!(this.into_struc::<Entity>(), Entity{x: 100, y: 200, health: 100});
}

{
    // The `Point_SI` trait was generated by the Structural derive on `Point`,
    // aliasing it's accessor trait impls.
    let this: Box<dyn Point_SI<u32>> = Box::new(Point{x: 2016, y: 2080});
    assert_eq!(this.into_struc::<Entity>(), Entity{x: 2016, y: 2080, health: 100});
}


#[derive(Structural)]
struct Point<T>{
    pub x: T,
    pub y: u32,
}

#[derive(Debug,PartialEq,Structural)]
struct Entity{
    #[struc(public)]
    x: u32,

    #[struc(public)]
    y: u32,

    health: u32,
}

// This macro implements TryFromStructural for Entity by
// delegating to the passed-in implementation of `FromStructural`.
//
// `TryFromStructural` cannot have a blanket impl because it would collide
// with user implementations.
structural::z_impl_from_structural!{
    impl[F] FromStructural<F> for Entity
    where[ F: Entity_SI ]
    {
        fn from_structural(from){
            let (x, y) = from.into_fields(fp!(x, y));
            Entity{ x, y, health: 100 }
        }    
    }
}

Enum Example

This example demonstrates how this trait can be manually implemented for an enum with one private field (as in a field that doesn't have accessor impls to get it).

use structural::{
    convert::FromStructural,
    for_examples::OptionLike,
    structural_aliases::OptionMove_ESI,
    StructuralExt, Structural, switch,
};

assert_eq!(Some(100).into_struc::<UberOption<_>>(), UberOption::Some(100, 100));
assert_eq!(None.into_struc::<UberOption<u32>>(), UberOption::None);

assert_eq!(OptionLike::Some(3).into_struc::<UberOption<_>>(), UberOption::Some(3, 3));
assert_eq!(OptionLike::None.into_struc::<UberOption<u32>>(), UberOption::None);

// Converting an UberOption back to itself with `FromStructural` drops the
// `#[struct(not_public)]` field,because it has no accessor impls.
assert_eq!(
    UberOption::Some(5, 200).into_struc::<UberOption<_>>(),
    UberOption::Some(5, 5)
);
assert_eq!(
    UberOption::None.into_struc::<UberOption<u32>>(),
    UberOption::None,
);

#[derive(Debug,Structural,PartialEq)]
enum UberOption<T>{
    Some(
        T,
        #[struc(not_public)]
        T,
    ),
    None,
}

// This macro implements TryFromStructural for Entity by
// delegating to the passed-in implementation of `FromStructural`.
//
// `TryFromStructural` cannot have a blanket impl because it would collide
// with user implementations.
structural::z_impl_from_structural!{
    impl[F, T] FromStructural<F> for UberOption<T>
    where[
        F: OptionMove_ESI<T>,
        T: Clone,
    ]{
        fn from_structural(from){
            switch!{from;
                Some(x) => Self::Some(x.clone(), x),
                None => Self::None,
            }
        }
    }
}

Required methods

fn from_structural(from: T) -> Self

Performs the conversion

Loading content...

Implementations on Foreign Types

impl<T, F> FromStructural<F> for [T; 32] where
    F: ArrayMove32<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 31] where
    F: ArrayMove31<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 30] where
    F: ArrayMove30<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 29] where
    F: ArrayMove29<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 28] where
    F: ArrayMove28<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 27] where
    F: ArrayMove27<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 26] where
    F: ArrayMove26<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 25] where
    F: ArrayMove25<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 24] where
    F: ArrayMove24<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 23] where
    F: ArrayMove23<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 22] where
    F: ArrayMove22<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 21] where
    F: ArrayMove21<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 20] where
    F: ArrayMove20<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 19] where
    F: ArrayMove19<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 18] where
    F: ArrayMove18<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 17] where
    F: ArrayMove17<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 16] where
    F: ArrayMove16<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 15] where
    F: ArrayMove15<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 14] where
    F: ArrayMove14<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 13] where
    F: ArrayMove13<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 12] where
    F: ArrayMove12<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 11] where
    F: ArrayMove11<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 10] where
    F: ArrayMove10<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 9] where
    F: ArrayMove9<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 8] where
    F: ArrayMove8<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 7] where
    F: ArrayMove7<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 6] where
    F: ArrayMove6<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 5] where
    F: ArrayMove5<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 4] where
    F: ArrayMove4<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 3] where
    F: ArrayMove3<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 2] where
    F: ArrayMove2<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 1] where
    F: ArrayMove1<T>, 
[src]

impl<T, F> FromStructural<F> for [T; 0] where
    F: ArrayMove0<T>, 
[src]

impl<F, T> FromStructural<F> for Option<T> where
    F: OptionMove_ESI<T>, 
[src]

impl<F, T, E> FromStructural<F> for Result<T, E> where
    F: ResultMove_ESI<T, E>, 
[src]

impl<F, T> FromStructural<F> for Range<T> where
    F: IntoField<TStr<__TS<(__s, __t, __a, __r, __t)>>, Ty = T> + IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>, 
[src]

impl<F, T> FromStructural<F> for RangeFrom<T> where
    F: IntoField<TStr<__TS<(__s, __t, __a, __r, __t)>>, Ty = T>, 
[src]

impl<F, T> FromStructural<F> for RangeTo<T> where
    F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>, 
[src]

impl<F, T> FromStructural<F> for RangeToInclusive<T> where
    F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>, 
[src]

impl<F, T> FromStructural<F> for RangeInclusive<T> where
    F: IntoField<TStr<__TS<(__s, __t, __a, __r, __t)>>, Ty = T> + IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>, 
[src]

impl<T, __From> FromStructural<__From> for ManuallyDrop<T> where
    T: FromStructural<__From>, 
[src]

impl<P, __From> FromStructural<__From> for Pin<P> where
    P: Deref,
    P::Target: Sized,
    P::Target: Unpin,
    P: FromStructural<__From>, 
[src]

impl<T, __From> FromStructural<__From> for Arc<T> where
    T: ?Sized,
    T: FromStructural<__From>, 
[src]

impl<T, __From> FromStructural<__From> for Rc<T> where
    T: ?Sized,
    T: FromStructural<__From>, 
[src]

impl<T, __From> FromStructural<__From> for Box<T> where
    T: ?Sized,
    T: FromStructural<__From>, 
[src]

impl<T, C0> FromStructural<T> for (C0,) where
    T: TupleMove1<C0>, 
[src]

impl<T, C0, C1> FromStructural<T> for (C0, C1) where
    T: TupleMove2<C0, C1>, 
[src]

impl<T, C0, C1, C2> FromStructural<T> for (C0, C1, C2) where
    T: TupleMove3<C0, C1, C2>, 
[src]

impl<T, C0, C1, C2, C3> FromStructural<T> for (C0, C1, C2, C3) where
    T: TupleMove4<C0, C1, C2, C3>, 
[src]

impl<T, C0, C1, C2, C3, C4> FromStructural<T> for (C0, C1, C2, C3, C4) where
    T: TupleMove5<C0, C1, C2, C3, C4>, 
[src]

impl<T, C0, C1, C2, C3, C4, C5> FromStructural<T> for (C0, C1, C2, C3, C4, C5) where
    T: TupleMove6<C0, C1, C2, C3, C4, C5>, 
[src]

impl<T, C0, C1, C2, C3, C4, C5, C6> FromStructural<T> for (C0, C1, C2, C3, C4, C5, C6) where
    T: TupleMove7<C0, C1, C2, C3, C4, C5, C6>, 
[src]

impl<T, C0, C1, C2, C3, C4, C5, C6, C7> FromStructural<T> for (C0, C1, C2, C3, C4, C5, C6, C7) where
    T: TupleMove8<C0, C1, C2, C3, C4, C5, C6, C7>, 
[src]

impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8> FromStructural<T> for (C0, C1, C2, C3, C4, C5, C6, C7, C8) where
    T: TupleMove9<C0, C1, C2, C3, C4, C5, C6, C7, C8>, 
[src]

impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9> FromStructural<T> for (C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) where
    T: TupleMove10<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>, 
[src]

impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10> FromStructural<T> for (C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10) where
    T: TupleMove11<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10>, 
[src]

impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11> FromStructural<T> for (C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11) where
    T: TupleMove12<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11>, 
[src]

impl<T> FromStructural<T> for ()[src]

Loading content...

Implementors

impl<T, __From> FromStructural<__From> for FieldCloner<T> where
    T: FromStructural<__From>, 
[src]

impl<T, __From> FromStructural<__From> for StrucWrapper<T> where
    T: FromStructural<__From>, 
[src]

Loading content...