To

Trait To 

Source
pub trait To {
    // Provided method
    fn to<T>(self) -> T
       where Self: Into<T> { ... }
}
Expand description

Simple and safe type conversions to self. Relies on reciprocals From and Into, so as a consequence the implementation of at least one of them is required.

§Examples

use core_ext::convert::To;

struct A(u8);
impl To for A {}

struct B(u8);
impl From<A> for B {
    fn from(value: A) -> Self {
        Self(value.0 + 1)
    }
}
impl To for B {}

struct C(u8);
impl From<B> for C {
    fn from(value: B) -> Self {
        Self(value.0 + 2)
    }
}
impl To for C {}

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

Provided Methods§

Source

fn to<T>(self) -> T
where Self: Into<T>,

Implementors§