rust_utils/
convert.rs

1pub trait FromExt: Sized {
2    fn from_<T>(t: T) -> Self
3    where
4        Self: From<T>,
5    {
6        From::from(t)
7    }
8}
9
10pub trait IntoExt: Sized {
11    fn into_<T>(self) -> T
12    where
13        Self: Into<T>,
14    {
15        Into::into(self)
16    }
17}
18
19pub trait TryFromExt: Sized {
20    fn try_from_<T>(t: T) -> Result<Self, <Self as TryFrom<T>>::Error>
21    where
22        Self: TryFrom<T>,
23    {
24        TryFrom::try_from(t)
25    }
26}
27
28pub trait TryIntoExt: Sized {
29    fn try_into_<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
30    where
31        Self: TryInto<T>,
32    {
33        TryInto::try_into(self)
34    }
35}
36
37impl<T> FromExt for T {}
38impl<T> IntoExt for T {}
39
40impl<T> TryFromExt for T {}
41impl<T> TryIntoExt for T {}
42
43pub fn from<T, U>(t: T) -> U
44where
45    U: From<T>,
46{
47    U::from(t)
48}
49
50pub fn into<T, U>(t: T) -> U
51where
52    T: Into<U>,
53{
54    T::into(t)
55}
56
57pub fn try_from<T, U>(t: T) -> Result<U, <U as TryFrom<T>>::Error>
58where
59    U: TryFrom<T>,
60{
61    U::try_from(t)
62}
63
64pub fn try_into<T, U>(t: T) -> Result<U, <T as TryInto<U>>::Error>
65where
66    T: TryInto<U>,
67{
68    T::try_into(t)
69}