[−][src]Trait structural::convert::TryFromStructural
For fallible conversions between structural types.
Usually conversions between enums require the from enum to have at least the fields and variants of the into enum.
All the examples in this crate are for enums, since converting from an enum to another with a subset of the variants in the first is the motivating usecase for defining this trait.
Implementations
Enums
This trait is usually implemented for enums by:
-
Bounding the trait's type parameter with the
*_SI
structural alias generated for the enum by theStructural
derive. -
Matching on all the variants of the enum with the switch macro, returning
Ok
with a variant if the parameter matches that enum variant. If the parameter doesn't match any of the enum variants,an error is returned.
Example
This example demonstrates how this trait can be implemented with the
z_impl_try_from_structural_for_enum
macro.
use structural::{ convert::{EmptyTryFromError, TryFromError}, for_examples::{Enum3, Enum4}, Structural, StructuralExt, switch, }; use std::cmp::Ordering; assert_eq!( Enum3::Foo(3, 5).try_into_struc::<Variants>(), Ok(Variants::Foo(3)), ); assert_eq!( Enum3::Bar(Ordering::Less, None).try_into_struc::<Variants>(), Ok(Variants::Bar), ); assert_eq!( Enum3::Baz{foom: "hi"}.try_into_struc::<Variants>(), Ok(Variants::Baz{foom: "hi"}), ); let qux=Enum4::Qux { uh: [0; 4], what: (false, false) }; assert_eq!( qux.try_into_struc::<Variants>(), Err(TryFromError::with_empty_error(qux)), ); #[derive(Structural, Copy, Clone, Debug, PartialEq)] enum Variants { Foo(u8), Bar, Baz { foom: &'static str }, } // This macro implements FromStructural in terms of TryFromStructural, // // 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. structural::z_impl_try_from_structural_for_enum!{ impl[F] TryFromStructural<F> for Variants // `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`. where[ 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` was generated by the `Structural` derive for `Variants` // aliasing its accessor trait impls, // and requires `F` to only have the `Foo`,`Bar`,and `Baz` variants. FromStructural where[ F: Variants_ESI, ] }
Example: Manual implementation
use structural::{ convert::{EmptyTryFromError, FromStructural, TryFromError, TryFromStructural}, for_examples::{Enum3, Enum4}, Structural, StructuralExt, switch, }; use std::cmp::Ordering; assert_eq!( Enum3::Foo(3, 5).try_into_struc::<Variants>(), Ok(Variants::Foo(3)), ); assert_eq!( Enum3::Bar(Ordering::Less, None).try_into_struc::<Variants>(), Ok(Variants::Bar), ); assert_eq!( Enum3::Baz{foom: "hi"}.try_into_struc::<Variants>(), Ok(Variants::Baz{foom: "hi"}), ); let qux=Enum4::Qux { uh: [0; 4], what: (false, false) }; assert_eq!( qux.try_into_struc::<Variants>(), Err(TryFromError::with_empty_error(qux)), ); #[derive(Structural, Copy, Clone, Debug, PartialEq)] enum Variants { Foo(u8), Bar, Baz { foom: &'static str }, } impl<F> FromStructural<F> for Variants where // `Variants_ESI` was generated by the `Structural` derive for `Variants` // aliasing its accessor trait impls, // and requires `F` to only have the `Foo`,`Bar`,and `Baz` variants. F: Variants_ESI, { fn from_structural(this: F) -> Self { switch! {this; Foo(x) => Self::Foo(x), Bar => Self::Bar, Baz{foom} => Self::Baz{foom}, } } } 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: F) -> Result<Self, TryFromError<F, Self::Error>> { switch! {this; Foo(x) => Ok(Self::Foo(x)), Bar => Ok(Self::Bar), Baz{foom} => Ok(Self::Baz{foom}), _ => Err(TryFromError::with_empty_error(this)), } } }
Associated Types
type Error
The error parameter of TryFromError
,
returned from try_into_structural
on conversion error.
Required methods
fn try_from_structural(from: T) -> Result<Self, TryFromError<T, Self::Error>>
Performs the conversion
Implementations on Foreign Types
impl<T, F> TryFromStructural<F> for [T; 32] where
F: ArrayMove32<T>,
[src]
F: ArrayMove32<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 31] where
F: ArrayMove31<T>,
[src]
F: ArrayMove31<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 30] where
F: ArrayMove30<T>,
[src]
F: ArrayMove30<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 29] where
F: ArrayMove29<T>,
[src]
F: ArrayMove29<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 28] where
F: ArrayMove28<T>,
[src]
F: ArrayMove28<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 27] where
F: ArrayMove27<T>,
[src]
F: ArrayMove27<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 26] where
F: ArrayMove26<T>,
[src]
F: ArrayMove26<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 25] where
F: ArrayMove25<T>,
[src]
F: ArrayMove25<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 24] where
F: ArrayMove24<T>,
[src]
F: ArrayMove24<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 23] where
F: ArrayMove23<T>,
[src]
F: ArrayMove23<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 22] where
F: ArrayMove22<T>,
[src]
F: ArrayMove22<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 21] where
F: ArrayMove21<T>,
[src]
F: ArrayMove21<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 20] where
F: ArrayMove20<T>,
[src]
F: ArrayMove20<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 19] where
F: ArrayMove19<T>,
[src]
F: ArrayMove19<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 18] where
F: ArrayMove18<T>,
[src]
F: ArrayMove18<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 17] where
F: ArrayMove17<T>,
[src]
F: ArrayMove17<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 16] where
F: ArrayMove16<T>,
[src]
F: ArrayMove16<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 15] where
F: ArrayMove15<T>,
[src]
F: ArrayMove15<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 14] where
F: ArrayMove14<T>,
[src]
F: ArrayMove14<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 13] where
F: ArrayMove13<T>,
[src]
F: ArrayMove13<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 12] where
F: ArrayMove12<T>,
[src]
F: ArrayMove12<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 11] where
F: ArrayMove11<T>,
[src]
F: ArrayMove11<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 10] where
F: ArrayMove10<T>,
[src]
F: ArrayMove10<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 9] where
F: ArrayMove9<T>,
[src]
F: ArrayMove9<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 8] where
F: ArrayMove8<T>,
[src]
F: ArrayMove8<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 7] where
F: ArrayMove7<T>,
[src]
F: ArrayMove7<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 6] where
F: ArrayMove6<T>,
[src]
F: ArrayMove6<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 5] where
F: ArrayMove5<T>,
[src]
F: ArrayMove5<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 4] where
F: ArrayMove4<T>,
[src]
F: ArrayMove4<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 3] where
F: ArrayMove3<T>,
[src]
F: ArrayMove3<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 2] where
F: ArrayMove2<T>,
[src]
F: ArrayMove2<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 1] where
F: ArrayMove1<T>,
[src]
F: ArrayMove1<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, F> TryFromStructural<F> for [T; 0] where
F: ArrayMove0<T>,
[src]
F: ArrayMove0<T>,
type Error = Infallible
fn try_from_structural(from: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<F, T> TryFromStructural<F> for Option<T> where
F: OptionMove_SI<T>,
[src]
F: OptionMove_SI<T>,
type Error = EmptyTryFromError
fn try_from_structural(this: F) -> Result<Self, TryFromError<F, Self::Error>>
[src]
impl<F, T, E> TryFromStructural<F> for Result<T, E> where
F: ResultMove_SI<T, E>,
[src]
F: ResultMove_SI<T, E>,
type Error = EmptyTryFromError
fn try_from_structural(this: F) -> Result<Self, TryFromError<F, Self::Error>>
[src]
impl<F, T> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(this: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<F, T> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(this: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<F, T> TryFromStructural<F> for RangeTo<T> where
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
[src]
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
type Error = Infallible
fn try_from_structural(this: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<F, T> TryFromStructural<F> for RangeToInclusive<T> where
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
[src]
F: IntoField<TStr<__TS<(__e, __n, __d)>>, Ty = T>,
type Error = Infallible
fn try_from_structural(this: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<F, T> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(this: F) -> Result<Self, TryFromError<F, Infallible>>
[src]
impl<T, __From, __Err> TryFromStructural<__From> for ManuallyDrop<T> where
T: TryFromStructural<__From, Error = __Err>,
[src]
T: TryFromStructural<__From, Error = __Err>,
type Error = __Err
fn try_from_structural(
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
[src]
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
impl<P, __From, __Err> TryFromStructural<__From> for Pin<P> where
P: Deref,
P::Target: Sized,
P::Target: Unpin,
P: TryFromStructural<__From, Error = __Err>,
[src]
P: Deref,
P::Target: Sized,
P::Target: Unpin,
P: TryFromStructural<__From, Error = __Err>,
type Error = __Err
fn try_from_structural(
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
[src]
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
impl<T, __From, __Err> TryFromStructural<__From> for Arc<T> where
T: ?Sized,
T: TryFromStructural<__From, Error = __Err>,
[src]
T: ?Sized,
T: TryFromStructural<__From, Error = __Err>,
type Error = __Err
fn try_from_structural(
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
[src]
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
impl<T, __From, __Err> TryFromStructural<__From> for Rc<T> where
T: ?Sized,
T: TryFromStructural<__From, Error = __Err>,
[src]
T: ?Sized,
T: TryFromStructural<__From, Error = __Err>,
type Error = __Err
fn try_from_structural(
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
[src]
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
impl<T, __From, __Err> TryFromStructural<__From> for Box<T> where
T: ?Sized,
T: TryFromStructural<__From, Error = __Err>,
[src]
T: ?Sized,
T: TryFromStructural<__From, Error = __Err>,
type Error = __Err
fn try_from_structural(
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
[src]
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
impl<T, C0> TryFromStructural<T> for (C0,) where
T: TupleMove1<C0>,
[src]
T: TupleMove1<C0>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1> TryFromStructural<T> for (C0, C1) where
T: TupleMove2<C0, C1>,
[src]
T: TupleMove2<C0, C1>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2> TryFromStructural<T> for (C0, C1, C2) where
T: TupleMove3<C0, C1, C2>,
[src]
T: TupleMove3<C0, C1, C2>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3> TryFromStructural<T> for (C0, C1, C2, C3) where
T: TupleMove4<C0, C1, C2, C3>,
[src]
T: TupleMove4<C0, C1, C2, C3>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4> TryFromStructural<T> for (C0, C1, C2, C3, C4) where
T: TupleMove5<C0, C1, C2, C3, C4>,
[src]
T: TupleMove5<C0, C1, C2, C3, C4>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4, C5> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4, C5, C6> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4, C5, C6, C7> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11> TryFromStructural<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>,
type Error = Infallible
fn try_from_structural(value: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
impl<T> TryFromStructural<T> for ()
[src]
type Error = Infallible
fn try_from_structural(_from: T) -> Result<Self, TryFromError<T, Infallible>>
[src]
Implementors
impl<T, __From, __Err> TryFromStructural<__From> for FieldCloner<T> where
T: TryFromStructural<__From, Error = __Err>,
[src]
T: TryFromStructural<__From, Error = __Err>,
type Error = __Err
fn try_from_structural(
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
[src]
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
impl<T, __From, __Err> TryFromStructural<__From> for StrucWrapper<T> where
T: TryFromStructural<__From, Error = __Err>,
[src]
T: TryFromStructural<__From, Error = __Err>,
type Error = __Err
fn try_from_structural(
from: __From
) -> Result<Self, TryFromError<__From, __Err>>
[src]
from: __From
) -> Result<Self, TryFromError<__From, __Err>>