[][src]Trait structural::convert::TryIntoStructural

pub trait TryIntoStructural<T>: Sized {
    type Error;
    fn try_into_structural(self) -> Result<T, TryFromError<Self, Self::Error>>;
}

For fallible conversions between structural types.

This trait has a blanket implementations for all types that implement TryFromStructural.

Example

This example demonstrates how you can use TryIntoStructural as a bound.

use structural::{
    convert::TryIntoStructural,
    for_examples::{Enum2, Enum3}
};

use std::cmp::{Ordering::{Less, Equal, Greater}, PartialEq};

compare_eq(Enum3::Foo(3, 5), Enum2::Foo(3, 5)      , true  );
compare_eq(Enum3::Foo(5, 8), Enum2::Foo(5, 8)      , true  );
compare_eq(Enum3::Foo(3, 5), Enum2::Foo(4, 5)      , false );
compare_eq(Enum3::Foo(3, 5), Enum2::Bar(Less, None), false );

compare_eq(Enum3::Bar(Less, None), Enum2::Foo(3, 5)       , false );
compare_eq(Enum3::Bar(Less, None), Enum2::Bar(Less, None) , true  );
compare_eq(Enum3::Bar(Equal,None), Enum2::Bar(Equal, None), true  );
compare_eq(Enum3::Bar(Less, None), Enum2::Bar(Equal, None), false );

// `Enum3::Baz{..}` is never equal to an `Enum2`,because it doesn't have a `Baz` variant.
compare_eq(Enum3::Baz{foom: "hello"}, Enum2::Foo(3, 5)       , false );
compare_eq(Enum3::Baz{foom: "hello"}, Enum2::Bar(Less, None) , false );

fn compare_eq<T, U>(left: T, right: U, expected: bool)
where
    T: TryIntoStructural<U>,
    U: PartialEq,
{
    let is_equal = match left.try_into_structural() {
        Ok(left) => left==right,
        Err(_) => false,
    };

    assert_eq!( is_equal, expected );
}

Associated Types

type Error

The error parameter of TryFromError, returned from try_into_structural on conversion error.

Loading content...

Required methods

fn try_into_structural(self) -> Result<T, TryFromError<Self, Self::Error>>

Performs the conversion

Loading content...

Implementors

impl<This, T> TryIntoStructural<T> for This where
    T: TryFromStructural<This>, 
[src]

type Error = T::Error

Loading content...