1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! # Core Convert Extensions
//!
//! Useful extensions to `core::convert` library.
//!

use core::convert::{Into, TryInto};
use core::result::Result;

/// Simple and safe type conversions to `self`. Relies on reciprocals `From` and `Into`, so as a consequence the implementation of at least one of them is required.
///
/// # Examples
///
/// ```
/// use core_ext::convert::To;
///
/// struct A(u8);
/// impl To for A {}
///
/// struct B(u8);
/// impl From<A> for B {
///     fn from(value: A) -> Self {
///         Self(value.0 + 1)
///     }
/// }
/// impl To for B {}
///
/// struct C(u8);
/// impl From<B> for C {
///     fn from(value: B) -> Self {
///         Self(value.0 + 2)
///     }
/// }
/// impl To for C {}
///
/// assert_eq!(5_u8, A(2).to::<B>().to::<C>().0)
/// ```
pub trait To {
    fn to<T>(self) -> T
    where
        Self: Into<T>,
    {
        self.into()
    }
}

/// Simple and safe type conversions to `self` that may fail in a controlled way under some circunstances.
/// Relies on reciprocals `TryFrom` and `TryInto`, so as a consequence the implementation of at least one of them is required.
///
/// # Examples
///
/// ```
/// use core_ext::convert::TryTo;
///
/// #[derive(Debug)]
/// enum Error {}
///
/// struct A(u8);
/// impl TryTo<Error> for A {}
///
/// struct B(u8);
/// impl TryFrom<A> for B {
///     type Error = Error;
///     fn try_from(value: A) -> Result<Self,Error> {
///         Ok(Self(value.0 + 1))
///     }
/// }
/// impl TryTo<Error> for B {}
///
/// struct C(u8);
/// impl TryFrom<B> for C {
///     type Error = Error;
///     fn try_from(value: B) -> Result<Self,Error> {
///         Ok(Self(value.0 + 2))
///     }
/// }
/// impl TryTo<Error> for C {}
///
/// assert_eq!(5_u8, A(2).try_to::<B>().unwrap().try_to::<C>().unwrap().0)
/// ```
pub trait TryTo<E> {
    fn try_to<T>(self) -> Result<T, E>
    where
        Self: TryInto<T, Error = E>,
    {
        self.try_into()
    }
}

#[cfg(test)]
mod tests {
    use core::convert::Infallible;

    use super::*;

    #[test]
    fn test_to() {
        #[derive(PartialEq, Debug)]
        struct Type(u8);
        impl From<u8> for Type {
            fn from(value: u8) -> Self {
                Type(value)
            }
        }
        impl To for u8 {}
        // insta::assert_yaml_snapshot!(2_u8.to::<A>());
        assert_eq!(Type(2), 2_u8.to())
    }

    #[test]
    fn test_to_chain() {
        #[derive(PartialEq, Debug)]
        struct A(u8);
        #[derive(PartialEq, Debug)]
        struct B(u8);
        #[derive(PartialEq, Debug)]
        struct C(u8);
        impl From<u8> for A {
            fn from(value: u8) -> Self {
                A(value + 1)
            }
        }
        impl From<A> for B {
            fn from(value: A) -> Self {
                B(value.0 + 1)
            }
        }
        impl From<B> for C {
            fn from(value: B) -> Self {
                Self(value.0 + 1)
            }
        }
        impl To for A {}
        // impl To for u8 {}
        impl To for B {}
        assert_eq!(A(1), 0_u8.to());
        assert_eq!(
            C(4_u8),
            1_u8.to::<A>() //
                .to::<B>() //
                .to::<C>()
        );
    }

    #[test]
    fn test_try_to() -> Result<(), Infallible> {
        #[derive(PartialEq, Debug)]
        struct Type(u8);
        impl TryFrom<u8> for Type {
            type Error = Infallible;

            fn try_from(value: u8) -> Result<Self, Self::Error> {
                Ok(Type(value))
            }
        }
        impl TryTo<Infallible> for u8 {}
        assert_eq!(Type(2), 2_u8.try_to()?);
        Ok(())
    }

    #[test]
    fn test_try_to_chain() -> Result<(), Infallible> {
        #[derive(PartialEq, Debug)]
        struct A(u8);
        #[derive(PartialEq, Debug)]
        struct B(u8);
        #[derive(PartialEq, Debug)]
        struct C(u8);
        impl TryFrom<u8> for A {
            type Error = Infallible;

            fn try_from(value: u8) -> Result<Self, Self::Error> {
                Ok(A(value + 1))
            }
        }
        impl TryFrom<A> for B {
            type Error = Infallible;

            fn try_from(value: A) -> Result<Self, Self::Error> {
                Ok(B(value.0 + 1))
            }
        }
        impl TryFrom<B> for C {
            type Error = Infallible;

            fn try_from(value: B) -> Result<Self, Self::Error> {
                Ok(Self(value.0 + 1))
            }
        }
        impl TryTo<Infallible> for A {}
        // impl To for u8 {}
        impl TryTo<Infallible> for B {}
        assert_eq!(A(1), 0_u8.try_to()?);
        assert_eq!(
            C(4_u8),
            1_u8.try_to::<A>()? //
                .try_to::<B>()? //
                .try_to::<C>()?
        );
        Ok(())
    }
}