Skip to main content

TryIntoStructural

Trait TryIntoStructural 

Source
pub trait TryIntoStructural<T>: Sized {
    type Error;

    // Required method
    fn try_into_structural(self) -> Result<T, TryFromError<Self, Self::Error>>;
}
Expand description

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

Required Associated Types§

Source

type Error

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

Required Methods§

Source

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

Performs the conversion

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<This, T> TryIntoStructural<T> for This
where T: TryFromStructural<This>,