tierkreis_core/
prelude.rs

1//! We make our own TryFrom trait to avoid a blanket implementation causing conflicts
2//! See <https://github.com/rust-lang/rust/issues/50133>
3use std::convert::Infallible;
4
5/// Like [std::convert::TryFrom] (but without the library blanket impls!)
6pub trait TryFrom<T>: Sized {
7    /// The type returned in the event of a conversion error.
8    type Error;
9
10    /// Performs the conversion.
11    fn try_from(value: T) -> Result<Self, Self::Error>;
12}
13
14/// Like [std::convert::TryInto] (but without the library blanket impls!)
15pub trait TryInto<T>: Sized {
16    /// The type returned in the event of a conversion error.
17    type Error;
18
19    /// Performs the conversion.
20    fn try_into(self) -> Result<T, Self::Error>;
21}
22
23impl<T, U> TryInto<U> for T
24where
25    U: TryFrom<T>,
26{
27    type Error = U::Error;
28
29    fn try_into(self) -> Result<U, U::Error> {
30        <U as TryFrom<_>>::try_from(self)
31    }
32}
33
34impl<T> TryFrom<T> for T {
35    type Error = Infallible;
36
37    fn try_from(value: T) -> Result<Self, Self::Error> {
38        Ok(value)
39    }
40}
41
42impl TryFrom<i64> for u32 {
43    type Error = <u32 as std::convert::TryFrom<i64>>::Error;
44
45    fn try_from(value: i64) -> Result<Self, Self::Error> {
46        std::convert::TryFrom::try_from(value)
47    }
48}