jiff_icu/traits.rs
1use core::convert::Infallible;
2
3/// Adds infallible conversions between crates that mirrors [`From`].
4pub trait ConvertFrom<F>: Sized {
5 /// Infallibly converts a value of type `F` to a value of type `Self`.
6 fn convert_from(value: F) -> Self;
7}
8
9/// Adds infallible conversions between crates that mirrors [`Into`].
10pub trait ConvertInto<T>: Sized {
11 /// Infallibly converts a value of type `Self` to a value of type `T`.
12 fn convert_into(self) -> T;
13}
14
15/// Adds fallible conversions between crates that mirrors [`TryFrom`].
16pub trait ConvertTryFrom<F>: Sized {
17 /// The type of an error that can occur during a conversion.
18 ///
19 /// In this crate, all errors correspond to the [`Error`](crate::Error)
20 /// type.
21 type Error;
22
23 /// Fallibly converts a value of type `F` to a value of type `Self`.
24 fn convert_try_from(value: F) -> Result<Self, Self::Error>;
25}
26
27/// Adds fallible conversions between crates that mirrors [`TryInto`].
28pub trait ConvertTryInto<T>: Sized {
29 /// The type of an error that can occur during a conversion.
30 ///
31 /// In this crate, all errors correspond to the [`Error`](crate::Error)
32 /// type.
33 type Error;
34
35 /// Fallibly converts a value of type `Self` to a value of type `T`.
36 fn convert_try_into(self) -> Result<T, Self::Error>;
37}
38
39impl<F: ConvertInto<T>, T> ConvertTryFrom<F> for T {
40 type Error = Infallible;
41
42 fn convert_try_from(value: F) -> Result<T, Infallible> {
43 Ok(value.convert_into())
44 }
45}
46
47impl<F, T: ConvertFrom<F>> ConvertInto<T> for F {
48 fn convert_into(self) -> T {
49 T::convert_from(self)
50 }
51}
52
53impl<F, T: ConvertTryFrom<F>> ConvertTryInto<T> for F {
54 type Error = T::Error;
55
56 fn convert_try_into(self) -> Result<T, T::Error> {
57 T::convert_try_from(self)
58 }
59}