1use gistyr_lib::Gistyr;
5
6pub mod prelude;
7
8pub trait GoToU8 {fn goto_u8(&self) -> u8;}
10pub trait GoToU16 {fn goto_u16(&self) -> u16;}
11pub trait GoToU32 {fn goto_u32(&self) -> u32;}
12pub trait GoToU64 {fn goto_u64(&self) -> u64;}
13pub trait GoToU128 {fn goto_u128(&self) -> u128;}
14pub trait GoToUsize {fn goto_usize(&self) -> usize;}
15pub trait GoToF32 {fn goto_f32(&self) -> f32;}
16pub trait GoToF64 {fn goto_f64(&self) -> f64;}
17pub trait TryGoToU8 {fn try_goto_u8(&self) -> Result<u8, Gistyr>;}
18pub trait TryGoToU16 {fn try_goto_u16(&self) -> Result<u16, Gistyr>;}
19pub trait TryGoToU32 {fn try_goto_u32(&self) -> Result<u32, Gistyr>;}
20pub trait TryGoToU64 {fn try_goto_u64(&self) -> Result<u64, Gistyr>;}
21pub trait TryGoToU128 {fn try_goto_u128(&self) -> Result<u128, Gistyr>;}
22pub trait TryGoToUsize {fn try_goto_usize(&self) -> Result<usize, Gistyr>;}
23pub trait TryGoToF32 {fn try_goto_f32(&self) -> Result<f32, Gistyr>;}
24pub trait TryGoToF64 {fn try_goto_f64(&self) -> Result<f64, Gistyr>;}
25impl GoToU8 for u8 {fn goto_u8(&self) -> u8 {*self as u8}}
29impl GoToU16 for u8 {fn goto_u16(&self) -> u16 {*self as u16}}
30impl GoToU32 for u8 {fn goto_u32(&self) -> u32 {*self as u32}}
31impl GoToU64 for u8 {fn goto_u64(&self) -> u64 {*self as u64}}
32impl GoToU128 for u8 {fn goto_u128(&self) -> u128 {*self as u128}}
33impl GoToUsize for u8 {fn goto_usize(&self) -> usize {*self as usize}}
34impl GoToF32 for u8 {fn goto_f32(&self) -> f32 {*self as f32}}
35impl GoToF64 for u8 {fn goto_f64(&self) -> f64 {*self as f64}}
36impl TryGoToU8 for u16 {
40 fn try_goto_u8(&self) -> Result<u8, Gistyr> {
41 if *self <= u8::MAX as u16 {Ok(*self as u8)}
42 else {Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} out of u8 range", self)))}
43 }
44}
45impl GoToU16 for u16 {fn goto_u16(&self) -> u16 {*self as u16}}
46impl GoToU32 for u16 {fn goto_u32(&self) -> u32 {*self as u32}}
47impl GoToU64 for u16 {fn goto_u64(&self) -> u64 {*self as u64}}
48impl GoToU128 for u16 {fn goto_u128(&self) -> u128 {*self as u128}}
49impl GoToUsize for u16 {fn goto_usize(&self) -> usize {*self as usize}}
50impl GoToF32 for u16 {fn goto_f32(&self) -> f32 {*self as f32}}
51impl GoToF64 for u16 {fn goto_f64(&self) -> f64 {*self as f64}}
52impl TryGoToU8 for u32 {
56 fn try_goto_u8(&self) -> Result<u8, Gistyr> {
57 if *self <= u8::MAX as u32 {Ok(*self as u8)}
58 else {Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} out of u8 range", self)))}
59 }
60}
61impl TryGoToU16 for u32 {
62 fn try_goto_u16(&self) -> Result<u16, Gistyr> {
63 if *self <= u16::MAX as u32 {Ok(*self as u16)}
64 else {Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} out of u16 range", self)))}
65 }
66}
67impl GoToU32 for u32 {fn goto_u32(&self) -> u32 {*self as u32}}
68impl GoToU64 for u32 {fn goto_u64(&self) -> u64 {*self as u64}}
69impl GoToU128 for u32 {fn goto_u128(&self) -> u128 {*self as u128}}
70impl GoToUsize for u32 {fn goto_usize(&self) -> usize {*self as usize}}
71impl TryGoToF32 for u32 {
72 fn try_goto_f32(&self) -> Result<f32, Gistyr> {
73 let converted = *self as f32;
74 if converted as u32 == *self {Ok(converted)}
75 else {Err(Gistyr::error("better-conversions", "TryGoToF32::try_goto_f32", format!("{} lost precision as a f32", self)))}
76 }
77}
78impl GoToF64 for u32 {fn goto_f64(&self) -> f64 {*self as f64}}
79impl TryGoToU8 for u64 {
83 fn try_goto_u8(&self) -> Result<u8, Gistyr> {
84 if *self <= u8::MAX as u64 {Ok(*self as u8)}
85 else {Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} out of u8 range", self)))}
86 }
87}
88impl TryGoToU16 for u64 {
89 fn try_goto_u16(&self) -> Result<u16, Gistyr> {
90 if *self <= u16::MAX as u64 {Ok(*self as u16)}
91 else {Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} out of u16 range", self)))}
92 }
93}
94impl TryGoToU32 for u64 {
95 fn try_goto_u32(&self) -> Result<u32, Gistyr> {
96 if *self <= u32::MAX as u64 {Ok(*self as u32)}
97 else {Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} out of u32 range", self)))}
98 }
99}
100impl GoToU64 for u64 {fn goto_u64(&self) -> u64 {*self as u64}}
101impl GoToU128 for u64 {fn goto_u128(&self) -> u128 {*self as u128}}
102impl TryGoToUsize for u64 {
103 fn try_goto_usize(&self) -> Result<usize, Gistyr> {
104 if *self <= usize::MAX as u64 {Ok(*self as usize)}
105 else {Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} out of usize range", self)))}
106 }
107}
108impl TryGoToF32 for u64 {
109 fn try_goto_f32(&self) -> Result<f32, Gistyr> {
110 let converted = *self as f32;
111 if converted as u64 == *self {Ok(converted)}
112 else {Err(Gistyr::error("better-conversions", "TryGoToF32::try_goto_f32", format!("{} lost precision as a f32", self)))}
113 }
114}
115impl TryGoToF64 for u64 {
116 fn try_goto_f64(&self) -> Result<f64, Gistyr> {
117 let converted = *self as f64;
118 if converted as u64 == *self {Ok(converted)}
119 else {Err(Gistyr::error("better-conversions", "TryGoToF64::try_goto_f64", format!("{} lost precision as a f64", self)))}
120 }
121}
122impl TryGoToU8 for u128 {
126 fn try_goto_u8(&self) -> Result<u8, Gistyr> {
127 if *self <= u8::MAX as u128 {Ok(*self as u8)}
128 else {Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} out of u8 range", self)))}
129 }
130}
131impl TryGoToU16 for u128 {
132 fn try_goto_u16(&self) -> Result<u16, Gistyr> {
133 if *self <= u16::MAX as u128 {Ok(*self as u16)}
134 else {Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} out of u16 range", self)))}
135 }
136}
137impl TryGoToU32 for u128 {
138 fn try_goto_u32(&self) -> Result<u32, Gistyr> {
139 if *self <= u32::MAX as u128 {Ok(*self as u32)}
140 else {Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} out of u32 range", self)))}
141 }
142}
143impl TryGoToU64 for u128 {
144 fn try_goto_u64(&self) -> Result<u64, Gistyr> {
145 if *self <= u64::MAX as u128 {Ok(*self as u64)}
146 else {Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} out of u64 range", self)))}
147 }
148}
149impl GoToU128 for u128 {fn goto_u128(&self) -> u128 {*self as u128}}
150impl TryGoToUsize for u128 {
151 fn try_goto_usize(&self) -> Result<usize, Gistyr> {
152 if *self <= usize::MAX as u128 {Ok(*self as usize)}
153 else {Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} out of usize range", self)))}
154 }
155}
156impl TryGoToF32 for u128 {
157 fn try_goto_f32(&self) -> Result<f32, Gistyr> {
158 let converted = *self as f32;
159 if converted as u128 == *self {Ok(converted)}
160 else {Err(Gistyr::error("better-conversions", "TryGoToF32::try_goto_f32", format!("{} lost precision as a f32", self)))}
161 }
162}
163impl TryGoToF64 for u128 {
164 fn try_goto_f64(&self) -> Result<f64, Gistyr> {
165 let converted = *self as f64;
166 if converted as u128 == *self {Ok(converted)}
167 else {Err(Gistyr::error("better-conversions", "TryGoToF64::try_goto_f64", format!("{} lost precision as a f64", self)))}
168 }
169}
170impl TryGoToU8 for usize {
174 fn try_goto_u8(&self) -> Result<u8, Gistyr> {
175 if *self <= u8::MAX as usize { Ok(*self as u8) }
176 else { Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} out of u8 range", self))) }
177 }
178}
179impl TryGoToU16 for usize {
180 fn try_goto_u16(&self) -> Result<u16, Gistyr> {
181 if *self <= u16::MAX as usize { Ok(*self as u16) }
182 else { Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} out of u16 range", self))) }
183 }
184}
185impl TryGoToU32 for usize {
186 fn try_goto_u32(&self) -> Result<u32, Gistyr> {
187 if *self <= u32::MAX as usize { Ok(*self as u32) }
188 else { Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} out of u32 range", self))) }
189 }
190}
191impl TryGoToU64 for usize {
192 fn try_goto_u64(&self) -> Result<u64, Gistyr> {
193 if *self <= u64::MAX as usize { Ok(*self as u64) }
194 else { Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} out of u64 range", self))) }
195 }
196}
197impl GoToU128 for usize { fn goto_u128(&self) -> u128 { *self as u128 } }
198impl GoToUsize for usize { fn goto_usize(&self) -> usize { *self } }
199impl TryGoToF32 for usize {
200 fn try_goto_f32(&self) -> Result<f32, Gistyr> {
201 let converted = *self as f32;
202 if converted as usize == *self { Ok(converted) }
203 else { Err(Gistyr::error("better-conversions", "TryGoToF32::try_goto_f32", format!("{} lost precision as a f32", self))) }
204 }
205}
206impl TryGoToF64 for usize {
207 fn try_goto_f64(&self) -> Result<f64, Gistyr> {
208 let converted = *self as f64;
209 if converted as usize == *self { Ok(converted) }
210 else { Err(Gistyr::error("better-conversions", "TryGoToF64::try_goto_f64", format!("{} lost precision as a f64", self))) }
211 }
212}
213impl TryGoToU8 for f32 {
217 fn try_goto_u8(&self) -> Result<u8, Gistyr> {
218 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} is not finite", self)));}
219 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} has a fractional", self)));}
220 if *self >= 0.0 && *self <= u8::MAX as f32 {Ok(*self as u8)}
221 else {Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} out of u8 range", self)))}
222 }
223}
224impl TryGoToU16 for f32 {
225 fn try_goto_u16(&self) -> Result<u16, Gistyr> {
226 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} is not finite", self)));}
227 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} has a fractional", self)));}
228 if *self >= 0.0 && *self <= u16::MAX as f32 {Ok(*self as u16)}
229 else {Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} out of u16 range", self)))}
230 }
231}
232impl TryGoToU32 for f32 {
233 fn try_goto_u32(&self) -> Result<u32, Gistyr> {
234 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} is not finite", self)));}
235 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} has a fractional", self)));}
236 if *self >= 0.0 && *self <= u32::MAX as f32 {Ok(*self as u32)}
237 else {Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} out of u32 range", self)))}
238 }
239}
240impl TryGoToU64 for f32 {
241 fn try_goto_u64(&self) -> Result<u64, Gistyr> {
242 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} is not finite", self)));}
243 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} has a fractional", self)));}
244 if *self >= 0.0 && *self <= u64::MAX as f32 {Ok(*self as u64)}
245 else {Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} out of u64 range", self)))}
246 }
247}
248impl TryGoToU128 for f32 {
249 fn try_goto_u128(&self) -> Result<u128, Gistyr> {
250 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU128::try_goto_u128", format!("{} is not finite", self)));}
251 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU128::try_goto_u128", format!("{} has a fractional", self)));}
252 if *self >= 0.0 && *self <= u128::MAX as f32 {Ok(*self as u128)}
253 else {Err(Gistyr::error("better-conversions", "TryGoToU128::try_goto_u128", format!("{} out of u128 range", self)))}
254 }
255}
256impl TryGoToUsize for f32 {
257 fn try_goto_usize(&self) -> Result<usize, Gistyr> {
258 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} is not finite", self)));}
259 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} has a fractional", self)));
260 }
261 if *self >= 0.0 && *self <= usize::MAX as f32 {Ok(*self as usize)}
262 else {Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} out of usize range", self)))}
263 }
264}
265impl GoToF32 for f32 {fn goto_f32(&self) -> f32 {*self as f32}}
266impl GoToF64 for f32 {fn goto_f64(&self) -> f64 {*self as f64}}
267impl TryGoToU8 for f64 {
271 fn try_goto_u8(&self) -> Result<u8, Gistyr> {
272 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} is not finite", self)));}
273 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} has a fractional", self)));}
274 if *self >= 0.0 && *self <= u8::MAX as f64 {Ok(*self as u8)}
275 else {Err(Gistyr::error("better-conversions", "TryGoToU8::try_goto_u8", format!("{} out of u8 range", self)))}
276 }
277}
278impl TryGoToU16 for f64 {
279 fn try_goto_u16(&self) -> Result<u16, Gistyr> {
280 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} is not finite", self)));}
281 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} has a fractional", self)));}
282 if *self >= 0.0 && *self <= u16::MAX as f64 {Ok(*self as u16)}
283 else {Err(Gistyr::error("better-conversions", "TryGoToU16::try_goto_u16", format!("{} out of u16 range", self)))}
284 }
285}
286impl TryGoToU32 for f64 {
287 fn try_goto_u32(&self) -> Result<u32, Gistyr> {
288 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} is not finite", self)));}
289 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} has a fractional", self)));}
290 if *self >= 0.0 && *self <= u32::MAX as f64 {Ok(*self as u32)}
291 else {Err(Gistyr::error("better-conversions", "TryGoToU32::try_goto_u32", format!("{} out of u32 range", self)))}
292 }
293}
294impl TryGoToU64 for f64 {
295 fn try_goto_u64(&self) -> Result<u64, Gistyr> {
296 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} is not finite", self)));}
297 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} has a fractional", self)));}
298 if *self >= 0.0 && *self <= u64::MAX as f64 {Ok(*self as u64)}
299 else {Err(Gistyr::error("better-conversions", "TryGoToU64::try_goto_u64", format!("{} out of u64 range", self)))}
300 }
301}
302impl TryGoToU128 for f64 {
303 fn try_goto_u128(&self) -> Result<u128, Gistyr> {
304 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToU128::try_goto_u128", format!("{} is not finite", self)));}
305 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToU128::try_goto_u128", format!("{} has a fractional", self)));}
306 if *self >= 0.0 && *self <= u128::MAX as f64 {Ok(*self as u128)}
307 else {Err(Gistyr::error("better-conversions", "TryGoToU128::try_goto_u128", format!("{} out of u128 range", self)))}
308 }
309}
310impl TryGoToUsize for f64 {
311 fn try_goto_usize(&self) -> Result<usize, Gistyr> {
312 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} is not finite", self)));}
313 if self.fract() != 0.0 {return Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} has a fractional", self)));}
314 if *self >= 0.0 && *self <= usize::MAX as f64 {Ok(*self as usize)}
315 else {Err(Gistyr::error("better-conversions", "TryGoToUsize::try_goto_usize", format!("{} out of usize range", self)))}
316 }
317}
318impl TryGoToF32 for f64 {
319 fn try_goto_f32(&self) -> Result<f32, Gistyr> {
320 if !self.is_finite() {return Err(Gistyr::error("better-conversions", "TryGoToF32::try_goto_f32", format!("{} is not finite", self)));}
321 if *self >= f32::MIN as f64 && *self <= f32::MAX as f64 {Ok(*self as f32)}
322 else {Err(Gistyr::error("better-conversions", "TryGoToF32::try_goto_f32", format!("{} out of f32 range", self)))}
323 }
324}
325impl GoToF64 for f64 {fn goto_f64(&self) -> f64 {*self as f64}}
326