Trait FromStructural

Source
pub trait FromStructural<T>: TryFromStructural<T> {
    // Required method
    fn from_structural(from: T) -> Self;
}
Expand description

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 Examples

§Derivation

This example demonstrates how this trait can be derived 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)]
#[struc(from_structural)]
struct Entity{
    #[struc(public)]
    x: u32,

    #[struc(public)]
    y: u32,

    #[struc(init_with_lit = 100)]
    health: u32,
}

§Semi-manual impl

You can implement the FromStructural trait manually by using the z_impl_from_structural macro.


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});
}

#[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 Examples

§Derivation

This example demonstrates how this trait can be derived for an enum with one private field.

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

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

assert_eq!(OptionLike::Some(3).into_struc::<UberOption<_>>(), UberOption::Some(3, 0));
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, 0)
);
assert_eq!(
    UberOption::None.into_struc::<UberOption<u32>>(),
    UberOption::None,
);

#[derive(Debug,Structural,PartialEq)]
#[struc(from_structural(bound = "T: Default"))]
enum UberOption<T>{
    Some(
        T,
        #[struc(init_with_default)]
        T,
    ),
    None,
}

§Semi-manual

This example demonstrates how this trait can be semi-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§

Source

fn from_structural(from: T) -> Self

Performs the conversion

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

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

Source§

fn from_structural(this: F) -> Self

Source§

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>,

Source§

fn from_structural(this: F) -> Self

Source§

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

Source§

fn from_structural(this: F) -> Self

Source§

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>,

Source§

fn from_structural(this: F) -> Self

Source§

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

Source§

fn from_structural(this: F) -> Self

Source§

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

Source§

fn from_structural(this: F) -> Self

Source§

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

Source§

fn from_structural(this: F) -> Self

Source§

impl<P, __From> FromStructural<__From> for Pin<P>
where P: Deref + FromStructural<__From>, P::Target: Sized + Unpin,

Source§

fn from_structural(from: __From) -> Self

Source§

impl<T> FromStructural<T> for ()

Source§

fn from_structural(_from: T) -> Self

Source§

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

Source§

fn from_structural(value: T) -> Self

Source§

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

Source§

fn from_structural(value: T) -> Self

Source§

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

Source§

fn from_structural(value: T) -> Self

Source§

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

Source§

fn from_structural(value: T) -> Self

Source§

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

Source§

fn from_structural(value: T) -> Self

Source§

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>,

Source§

fn from_structural(value: T) -> Self

Source§

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>,

Source§

fn from_structural(value: T) -> Self

Source§

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>,

Source§

fn from_structural(value: T) -> Self

Source§

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>,

Source§

fn from_structural(value: T) -> Self

Source§

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>,

Source§

fn from_structural(value: T) -> Self

Source§

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>,

Source§

fn from_structural(value: T) -> Self

Source§

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>,

Source§

fn from_structural(value: T) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

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

Source§

fn from_structural(from: F) -> Self

Source§

impl<T, __From> FromStructural<__From> for Box<T>
where T: ?Sized + FromStructural<__From>,

Source§

fn from_structural(from: __From) -> Self

Source§

impl<T, __From> FromStructural<__From> for Rc<T>
where T: ?Sized + FromStructural<__From>,

Source§

fn from_structural(from: __From) -> Self

Source§

impl<T, __From> FromStructural<__From> for Arc<T>
where T: ?Sized + FromStructural<__From>,

Source§

fn from_structural(from: __From) -> Self

Source§

impl<T, __From> FromStructural<__From> for ManuallyDrop<T>
where T: FromStructural<__From>,

Source§

fn from_structural(from: __From) -> Self

Implementors§

Source§

impl<T, __From> FromStructural<__From> for FieldCloner<T>
where T: FromStructural<__From>,

Source§

impl<T, __From> FromStructural<__From> for StrucWrapper<T>
where T: FromStructural<__From>,