hexga_math/convert/
cast.rs1use super::*;
2
3
4
5pub trait CastFrom<T>
35{
36 fn cast_from(value : T) -> Self;
37}
38impl<C1,C2> CastFrom<C2> for C1 where C1: Map, C2: Map<WithType<C1::Item> = Self>, C1::Item : CastFrom<C2::Item>
39{
40 fn cast_from(value : C2) -> Self
41 {
42 value.map(|v| C1::Item::cast_from(v))
43 }
44}
45
46
47pub trait CastInto<T> : Sized
77{
78 fn cast_into(self) -> T;
79}
80impl<S,T> CastInto<T> for S where T:CastFrom<S>
81{
82 fn cast_into(self) -> T {
83 T::cast_from(self)
84 }
85}
86
87
88macro_rules! impl_cast_to
90{
91 ($src: ty, $dest: ty) =>
92 {
93 impl CastFrom<$src> for $dest
94 {
95 fn cast_from(value: $src) -> $dest { value as $dest }
96 }
97 };
98
99 ($cast_into: ty) =>
100 {
101 map_on_number!(impl_cast_to,$cast_into);
102 };
103}
104map_on_number!(impl_cast_to);
105
106
107map_on_integer!(
108 ($itself: ty) =>
109 {
110 impl CastFrom<bool> for $itself
111 {
112 fn cast_from(value: bool) -> $itself { if value { 1 } else { 0 } }
113 }
114
115 impl CastFrom<$itself> for bool
116 {
117 fn cast_from(value: $itself) -> bool { value != (0) }
118 }
119 };
120);
121map_on_float!(
122 ($itself: ty) =>
123 {
124 impl CastFrom<bool> for $itself
125 {
126 fn cast_from(value: bool) -> $itself { if value { 1. } else { 0. } }
127 }
128
129 impl CastFrom<$itself> for bool
130 {
131 fn cast_from(value: $itself) -> bool { value as $itself >= 0.5 }
132 }
133 };
134);
135impl CastFrom<bool> for bool { fn cast_from(value : bool) -> Self { value } }
136
137
138
139trait_marker!(
140 CastIntoFloat: CastInto<f32> + ToF32<Output = f32> + CastInto<f64> + ToF64<Output = f64>
142);
143
144trait_marker!(
145 CastFromFloat: CastFrom<f32> + CastFrom<f64>
147);
148
149trait_marker!(
150 CastFloat: CastIntoFloat + CastFromFloat
152);
153
154trait_marker!(
155CastIntoIntegerUnsigned:
157 CastInto<u8 > + ToU8<Output = u8> +
158 CastInto<u16> + ToU16<Output = u16> +
159 CastInto<u32> + ToU32<Output = u32> +
160 CastInto<u64> + ToU64<Output = u64> +
161 CastInto<usize> + ToUSize<Output = usize> +
162);
163
164trait_marker!(
165CastFromIntegerUnsigned:
167 CastFrom<u8 > +
168 CastFrom<u16> +
169 CastFrom<u32> +
170 CastFrom<u64> +
171 CastFrom<usize>
172);
173
174
175trait_marker!(
176CastIntegerUnsigned: CastFromIntegerUnsigned + CastFromIntegerUnsigned
178);
179
180trait_marker!(
181CastIntoIntegerSigned:
183 CastInto<i8 > + ToI8<Output = i8> +
184 CastInto<i16> + ToI16<Output = i16> +
185 CastInto<i32> + ToI32<Output = i32> +
186 CastInto<i64> + ToI64<Output = i64> +
187 CastInto<isize> + ToISize<Output = isize>
188);
189
190
191trait_marker!(
192CastFromIntegerSigned:
194 CastFrom<i8 > +
195 CastFrom<i16> +
196 CastFrom<i32> +
197 CastFrom<i64> +
198 CastFrom<isize>
199);
200
201
202trait_marker!(
203CastIntegerSigned: CastFromIntegerSigned + CastFromIntegerUnsigned
205);
206
207trait_marker!(
208CastIntoInteger: CastIntoIntegerSigned + CastIntoIntegerUnsigned
210);
211
212trait_marker!(
213CastFromInteger: CastFromIntegerSigned + CastFromIntegerUnsigned
215);
216
217trait_marker!(
218CastInteger: CastIntoInteger + CastFromInteger
220);
221
222
223trait_marker!(
224CastIntoBool: CastInto<bool> + ToBool<Output = bool>
226);
227
228trait_marker!(
229CastFromBool: CastFrom<bool>
231);
232
233trait_marker!(
234CastBool: CastIntoBool + CastFromBool
236);
237
238trait_marker!(
239CastIntoNumber: CastIntoInteger + CastIntoFloat
241);
242
243trait_marker!(
244CastFromNumber: CastFromInteger + CastFromFloat
246);
247
248trait_marker!(
249CastNumber: CastInteger + CastFloat
251);
252
253trait_marker!(
254CastIntoPrimitive: CastIntoNumber + CastIntoBool
256);
257
258trait_marker!(
259CastFromPrimitive: CastFromNumber + CastFromBool
261);
262
263trait_marker!(
264CastPrimitive: CastIntoPrimitive + CastFromPrimitive
266);