cv_convert_fork/
traits.rs

1pub use from::*;
2pub use try_from::*;
3
4mod try_from {
5    /// Fallible type conversion that is analogous to [TryFrom](std::convert::TryFrom).
6    pub trait TryFromCv<T>
7    where
8        Self: Sized,
9    {
10        type Error;
11
12        fn try_from_cv(from: T) -> Result<Self, Self::Error>;
13    }
14
15    /// Fallible type conversion that is analogous to [TryInto](std::convert::TryInto).
16    pub trait TryIntoCv<T> {
17        type Error;
18
19        fn try_into_cv(self) -> Result<T, Self::Error>;
20    }
21
22    impl<T, U> TryIntoCv<U> for T
23    where
24        U: TryFromCv<T>,
25    {
26        type Error = <U as TryFromCv<T>>::Error;
27
28        fn try_into_cv(self) -> Result<U, Self::Error> {
29            U::try_from_cv(self)
30        }
31    }
32}
33
34mod from {
35    /// Type conversion that is analogous to [From](std::convert::From).
36    pub trait FromCv<T> {
37        fn from_cv(from: T) -> Self;
38    }
39
40    /// Type conversion that is analogous to [Into](std::convert::Into).
41    pub trait IntoCv<T> {
42        fn into_cv(self) -> T;
43    }
44
45    impl<T, U> IntoCv<U> for T
46    where
47        U: FromCv<T>,
48    {
49        fn into_cv(self) -> U {
50            U::from_cv(self)
51        }
52    }
53}