[−][src]Trait structural::convert::FromStructural
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 theStructural
derive. -
Calling
into_fields
on the parameter to convert it into the fields in common with theSelf
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 theStructural
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
fn from_structural(from: T) -> Self
Performs the conversion
Implementations on Foreign Types
impl<T, F> FromStructural<F> for [T; 32] where
F: ArrayMove32<T>,
[src]
F: ArrayMove32<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 31] where
F: ArrayMove31<T>,
[src]
F: ArrayMove31<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 30] where
F: ArrayMove30<T>,
[src]
F: ArrayMove30<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 29] where
F: ArrayMove29<T>,
[src]
F: ArrayMove29<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 28] where
F: ArrayMove28<T>,
[src]
F: ArrayMove28<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 27] where
F: ArrayMove27<T>,
[src]
F: ArrayMove27<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 26] where
F: ArrayMove26<T>,
[src]
F: ArrayMove26<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 25] where
F: ArrayMove25<T>,
[src]
F: ArrayMove25<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 24] where
F: ArrayMove24<T>,
[src]
F: ArrayMove24<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 23] where
F: ArrayMove23<T>,
[src]
F: ArrayMove23<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 22] where
F: ArrayMove22<T>,
[src]
F: ArrayMove22<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 21] where
F: ArrayMove21<T>,
[src]
F: ArrayMove21<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 20] where
F: ArrayMove20<T>,
[src]
F: ArrayMove20<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 19] where
F: ArrayMove19<T>,
[src]
F: ArrayMove19<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 18] where
F: ArrayMove18<T>,
[src]
F: ArrayMove18<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 17] where
F: ArrayMove17<T>,
[src]
F: ArrayMove17<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 16] where
F: ArrayMove16<T>,
[src]
F: ArrayMove16<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 15] where
F: ArrayMove15<T>,
[src]
F: ArrayMove15<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 14] where
F: ArrayMove14<T>,
[src]
F: ArrayMove14<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 13] where
F: ArrayMove13<T>,
[src]
F: ArrayMove13<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 12] where
F: ArrayMove12<T>,
[src]
F: ArrayMove12<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 11] where
F: ArrayMove11<T>,
[src]
F: ArrayMove11<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 10] where
F: ArrayMove10<T>,
[src]
F: ArrayMove10<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 9] where
F: ArrayMove9<T>,
[src]
F: ArrayMove9<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 8] where
F: ArrayMove8<T>,
[src]
F: ArrayMove8<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 7] where
F: ArrayMove7<T>,
[src]
F: ArrayMove7<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 6] where
F: ArrayMove6<T>,
[src]
F: ArrayMove6<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 5] where
F: ArrayMove5<T>,
[src]
F: ArrayMove5<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 4] where
F: ArrayMove4<T>,
[src]
F: ArrayMove4<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 3] where
F: ArrayMove3<T>,
[src]
F: ArrayMove3<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 2] where
F: ArrayMove2<T>,
[src]
F: ArrayMove2<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 1] where
F: ArrayMove1<T>,
[src]
F: ArrayMove1<T>,
fn from_structural(from: F) -> Self
[src]
impl<T, F> FromStructural<F> for [T; 0] where
F: ArrayMove0<T>,
[src]
F: ArrayMove0<T>,
fn from_structural(from: F) -> Self
[src]
impl<F, T> FromStructural<F> for Option<T> where
F: OptionMove_ESI<T>,
[src]
F: OptionMove_ESI<T>,
fn from_structural(this: F) -> Self
[src]
impl<F, T, E> FromStructural<F> for Result<T, E> where
F: ResultMove_ESI<T, E>,
[src]
F: ResultMove_ESI<T, E>,
fn from_structural(this: F) -> Self
[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]
F: IntoField<TStr<__TS<(__s, __t, __a, __r, __t)>>, Ty = T> + IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
fn from_structural(this: F) -> Self
[src]
impl<F, T> FromStructural<F> for RangeFrom<T> where
F: IntoField<TStr<__TS<(__s, __t, __a, __r, __t)>>, Ty = T>,
[src]
F: IntoField<TStr<__TS<(__s, __t, __a, __r, __t)>>, Ty = T>,
fn from_structural(this: F) -> Self
[src]
impl<F, T> FromStructural<F> for RangeTo<T> where
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
[src]
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
fn from_structural(this: F) -> Self
[src]
impl<F, T> FromStructural<F> for RangeToInclusive<T> where
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
[src]
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
fn from_structural(this: F) -> Self
[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]
F: IntoField<TStr<__TS<(__s, __t, __a, __r, __t)>>, Ty = T> + IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
fn from_structural(this: F) -> Self
[src]
impl<T, __From> FromStructural<__From> for ManuallyDrop<T> where
T: FromStructural<__From>,
[src]
T: FromStructural<__From>,
fn from_structural(from: __From) -> Self
[src]
impl<P, __From> FromStructural<__From> for Pin<P> where
P: Deref,
P::Target: Sized,
P::Target: Unpin,
P: FromStructural<__From>,
[src]
P: Deref,
P::Target: Sized,
P::Target: Unpin,
P: FromStructural<__From>,
fn from_structural(from: __From) -> Self
[src]
impl<T, __From> FromStructural<__From> for Arc<T> where
T: ?Sized,
T: FromStructural<__From>,
[src]
T: ?Sized,
T: FromStructural<__From>,
fn from_structural(from: __From) -> Self
[src]
impl<T, __From> FromStructural<__From> for Rc<T> where
T: ?Sized,
T: FromStructural<__From>,
[src]
T: ?Sized,
T: FromStructural<__From>,
fn from_structural(from: __From) -> Self
[src]
impl<T, __From> FromStructural<__From> for Box<T> where
T: ?Sized,
T: FromStructural<__From>,
[src]
T: ?Sized,
T: FromStructural<__From>,
fn from_structural(from: __From) -> Self
[src]
impl<T, C0> FromStructural<T> for (C0,) where
T: TupleMove1<C0>,
[src]
T: TupleMove1<C0>,
fn from_structural(value: T) -> Self
[src]
impl<T, C0, C1> FromStructural<T> for (C0, C1) where
T: TupleMove2<C0, C1>,
[src]
T: TupleMove2<C0, C1>,
fn from_structural(value: T) -> Self
[src]
impl<T, C0, C1, C2> FromStructural<T> for (C0, C1, C2) where
T: TupleMove3<C0, C1, C2>,
[src]
T: TupleMove3<C0, C1, C2>,
fn from_structural(value: T) -> Self
[src]
impl<T, C0, C1, C2, C3> FromStructural<T> for (C0, C1, C2, C3) where
T: TupleMove4<C0, C1, C2, C3>,
[src]
T: TupleMove4<C0, C1, C2, C3>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove5<C0, C1, C2, C3, C4>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove6<C0, C1, C2, C3, C4, C5>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove7<C0, C1, C2, C3, C4, C5, C6>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove8<C0, C1, C2, C3, C4, C5, C6, C7>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove9<C0, C1, C2, C3, C4, C5, C6, C7, C8>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove10<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove11<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10>,
fn from_structural(value: T) -> Self
[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]
T: TupleMove12<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11>,
fn from_structural(value: T) -> Self
[src]
impl<T> FromStructural<T> for ()
[src]
fn from_structural(_from: T) -> Self
[src]
Implementors
impl<T, __From> FromStructural<__From> for FieldCloner<T> where
T: FromStructural<__From>,
[src]
T: FromStructural<__From>,
fn from_structural(from: __From) -> Self
[src]
impl<T, __From> FromStructural<__From> for StrucWrapper<T> where
T: FromStructural<__From>,
[src]
T: FromStructural<__From>,