default_constructor/
convert.rs

1/// [`Into`] with relaxed orphan rule, you can define non-owned
2/// conversion with a owned `Marker`. Keep in mind this inference
3/// will fail if multiple conversion paths are found.
4pub trait InferInto<A, Marker>: Sized {
5    fn into(self) -> A;
6}
7
8impl<T, U> InferInto<U, ()> for T
9where
10    T: Into<U>,
11{
12    fn into(self) -> U {
13        Into::<U>::into(self)
14    }
15}
16
17/// Convert via [`InferInto`]
18pub fn infer_into<T, U, M>(item: T) -> U
19where
20    T: InferInto<U, M>,
21{
22    InferInto::into(item)
23}
24
25/// Provides conversion from integer literal `i32` to other numerical types.
26pub trait StandardConverters<F> {
27    fn into(self) -> F;
28}
29
30impl<T, F> InferInto<F, bool> for T
31where
32    T: StandardConverters<F>,
33{
34    fn into(self) -> F {
35        StandardConverters::<F>::into(self)
36    }
37}
38
39impl StandardConverters<f64> for i64 {
40    fn into(self) -> f64 {
41        self as f64
42    }
43}
44
45macro_rules! std_convert {
46    ($($ty: ty),*) => {
47        $(
48            impl StandardConverters<$ty> for i32 {
49                fn into(self) -> $ty {
50                    self as $ty
51                }
52            }
53        )*
54    };
55}
56
57std_convert!(u8, u16, u32, u64, usize, u128, i8, i16, f32);