hexga_math/convert/
cast_primitive.rs

1use super::*;
2
3/// Macro to implement a "cast-to" trait for primitive numeric types
4/// and for generic containers that implement `MapGeneric`.
5macro_rules! impl_cast_to {
6    ($trait_name:ident, $fn_map_name:ident, $fn_map_range_name:ident, $output_type:ty) => {
7        /// Helper trait based on [CastInto] and [CastRangeInto]
8        /// Also work on composite like [std::array], [Vector]...
9        pub trait $trait_name : CastInto<Self::Output>
10        {
11            type Output;
12
13            /// Same semantics as the [as](https://practice.course.rs/type-conversions/as.html) 
14            /// keyword: `4f32 as u64`, and the [From] trait, but generic friendly.
15            ///
16            /// Like the [as](https://practice.course.rs/type-conversions/as.html) keyword, the result might lose some precision.
17            fn $fn_map_name(self) -> Self::Output;
18            /// Remap the value [RangeDefault] to the [RangeDefault] of the target type,
19            /// in a generic friendly way, and similar to the [From] trait.
20            fn $fn_map_range_name(self) -> Self::Output;
21        }
22
23        map_on_number_and_bool! { 
24            ($type_name:tt) => {
25            impl $trait_name for $type_name {
26                type Output = $output_type;
27                fn $fn_map_name(self) -> Self::Output {
28                    self.cast_into()
29                }
30                fn $fn_map_range_name(self) -> Self::Output {
31                    self.cast_range_into()
32                }
33            }
34        }}
35
36        impl<S> $trait_name for S
37        where
38            S: MapGeneric + CastInto<S::WithType<<S::Item as $trait_name>::Output>>,
39            S::Item: $trait_name,
40        {
41            type Output = S::WithType<<S::Item as $trait_name>::Output>;
42            fn $fn_map_name(self) -> Self::Output {
43                self.map($trait_name::$fn_map_name)
44            }
45            fn $fn_map_range_name(self) -> Self::Output {
46                self.map($trait_name::$fn_map_range_name)
47            }
48        }
49    };
50}
51
52impl_cast_to!(ToFloat, to_float, to_float_range, float);
53impl_cast_to!(ToInt, to_int, to_int_range, int);
54impl_cast_to!(ToUInt, to_uint, to_uint_range, uint);
55impl_cast_to!(ToF32, to_f32, to_f32_range, f32);
56impl_cast_to!(ToF64, to_f64, to_f64_range, f64);
57impl_cast_to!(ToU8, to_u8, to_u8_range, u8);
58impl_cast_to!(ToU16, to_u16, to_u16_range, u16);
59impl_cast_to!(ToU32, to_u32, to_u32_range, u32);
60impl_cast_to!(ToU64, to_u64, to_u64_range, u64);
61impl_cast_to!(ToUSize, to_usize, to_usize_range, usize);
62impl_cast_to!(ToI8, to_i8, to_i8_range, i8);
63impl_cast_to!(ToI16, to_i16, to_i16_range, i16);
64impl_cast_to!(ToI32, to_i32, to_i32_range, i32);
65impl_cast_to!(ToI64, to_i64, to_i64_range, i64);
66impl_cast_to!(ToISize, to_isize, to_isize_range, isize);
67impl_cast_to!(ToBool, to_bool, to_bool_range, bool);