use std::convert::TryInto;
pub trait To {
fn to<T>(self) -> T
where
Self: Into<T>,
{
self.into()
}
fn try_to<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
where
Self: TryInto<T>,
{
self.try_into()
}
}
impl<T> To for T {}
#[test]
fn test_it() {
assert_eq!(42u32.to::<u64>(), 42u64);
assert_eq!(42u32.try_to::<u8>(), Ok(42u8));
assert!(500.try_to::<u8>().is_err());
}