TryTo

Trait TryTo 

Source
pub trait TryTo<E> {
    // Provided method
    fn try_to<T>(self) -> Result<T, E>
       where Self: TryInto<T, Error = E> { ... }
}
Expand description

Simple and safe type conversions to self that may fail in a controlled way under some circunstances. Relies on reciprocals TryFrom and TryInto, so as a consequence the implementation of at least one of them is required.

§Examples

use core_ext::convert::TryTo;

#[derive(Debug)]
enum Error {}

struct A(u8);
impl TryTo<Error> for A {}

struct B(u8);
impl TryFrom<A> for B {
    type Error = Error;
    fn try_from(value: A) -> Result<Self,Error> {
        Ok(Self(value.0 + 1))
    }
}
impl TryTo<Error> for B {}

struct C(u8);
impl TryFrom<B> for C {
    type Error = Error;
    fn try_from(value: B) -> Result<Self,Error> {
        Ok(Self(value.0 + 2))
    }
}
impl TryTo<Error> for C {}

assert_eq!(5_u8, A(2).try_to::<B>().unwrap().try_to::<C>().unwrap().0)

Provided Methods§

Source

fn try_to<T>(self) -> Result<T, E>
where Self: TryInto<T, Error = E>,

Implementors§