1 use core::mem::size_of;
2 use core::num::Wrapping;
3 use core::{f32, f64};
4 use core::{i128, i16, i32, i64, i8, isize};
5 use core::{u128, u16, u32, u64, u8, usize};
6
7 /// A generic trait for converting a value to a number.
8 ///
9 /// A value can be represented by the target type when it lies within
10 /// the range of scalars supported by the target type.
11 /// For example, a negative integer cannot be represented by an unsigned
12 /// integer type, and an `i64` with a very high magnitude might not be
13 /// convertible to an `i32`.
14 /// On the other hand, conversions with possible precision loss or truncation
15 /// are admitted, like an `f32` with a decimal part to an integer type, or
16 /// even a large `f64` saturating to `f32` infinity.
17 pub trait ToPrimitive {
18 /// Converts the value of `self` to an `isize`. If the value cannot be
19 /// represented by an `isize`, then `None` is returned.
20 #[inline]
to_isize(&self) -> Option<isize>21 fn to_isize(&self) -> Option<isize> {
22 self.to_i64().as_ref().and_then(ToPrimitive::to_isize)
23 }
24
25 /// Converts the value of `self` to an `i8`. If the value cannot be
26 /// represented by an `i8`, then `None` is returned.
27 #[inline]
to_i8(&self) -> Option<i8>28 fn to_i8(&self) -> Option<i8> {
29 self.to_i64().as_ref().and_then(ToPrimitive::to_i8)
30 }
31
32 /// Converts the value of `self` to an `i16`. If the value cannot be
33 /// represented by an `i16`, then `None` is returned.
34 #[inline]
to_i16(&self) -> Option<i16>35 fn to_i16(&self) -> Option<i16> {
36 self.to_i64().as_ref().and_then(ToPrimitive::to_i16)
37 }
38
39 /// Converts the value of `self` to an `i32`. If the value cannot be
40 /// represented by an `i32`, then `None` is returned.
41 #[inline]
to_i32(&self) -> Option<i32>42 fn to_i32(&self) -> Option<i32> {
43 self.to_i64().as_ref().and_then(ToPrimitive::to_i32)
44 }
45
46 /// Converts the value of `self` to an `i64`. If the value cannot be
47 /// represented by an `i64`, then `None` is returned.
to_i64(&self) -> Option<i64>48 fn to_i64(&self) -> Option<i64>;
49
50 /// Converts the value of `self` to an `i128`. If the value cannot be
51 /// represented by an `i128` (`i64` under the default implementation), then
52 /// `None` is returned.
53 ///
54 /// The default implementation converts through `to_i64()`. Types implementing
55 /// this trait should override this method if they can represent a greater range.
56 #[inline]
to_i128(&self) -> Option<i128>57 fn to_i128(&self) -> Option<i128> {
58 self.to_i64().map(From::from)
59 }
60
61 /// Converts the value of `self` to a `usize`. If the value cannot be
62 /// represented by a `usize`, then `None` is returned.
63 #[inline]
to_usize(&self) -> Option<usize>64 fn to_usize(&self) -> Option<usize> {
65 self.to_u64().as_ref().and_then(ToPrimitive::to_usize)
66 }
67
68 /// Converts the value of `self` to a `u8`. If the value cannot be
69 /// represented by a `u8`, then `None` is returned.
70 #[inline]
to_u8(&self) -> Option<u8>71 fn to_u8(&self) -> Option<u8> {
72 self.to_u64().as_ref().and_then(ToPrimitive::to_u8)
73 }
74
75 /// Converts the value of `self` to a `u16`. If the value cannot be
76 /// represented by a `u16`, then `None` is returned.
77 #[inline]
to_u16(&self) -> Option<u16>78 fn to_u16(&self) -> Option<u16> {
79 self.to_u64().as_ref().and_then(ToPrimitive::to_u16)
80 }
81
82 /// Converts the value of `self` to a `u32`. If the value cannot be
83 /// represented by a `u32`, then `None` is returned.
84 #[inline]
to_u32(&self) -> Option<u32>85 fn to_u32(&self) -> Option<u32> {
86 self.to_u64().as_ref().and_then(ToPrimitive::to_u32)
87 }
88
89 /// Converts the value of `self` to a `u64`. If the value cannot be
90 /// represented by a `u64`, then `None` is returned.
to_u64(&self) -> Option<u64>91 fn to_u64(&self) -> Option<u64>;
92
93 /// Converts the value of `self` to a `u128`. If the value cannot be
94 /// represented by a `u128` (`u64` under the default implementation), then
95 /// `None` is returned.
96 ///
97 /// The default implementation converts through `to_u64()`. Types implementing
98 /// this trait should override this method if they can represent a greater range.
99 #[inline]
to_u128(&self) -> Option<u128>100 fn to_u128(&self) -> Option<u128> {
101 self.to_u64().map(From::from)
102 }
103
104 /// Converts the value of `self` to an `f32`. Overflows may map to positive
105 /// or negative inifinity, otherwise `None` is returned if the value cannot
106 /// be represented by an `f32`.
107 #[inline]
to_f32(&self) -> Option<f32>108 fn to_f32(&self) -> Option<f32> {
109 self.to_f64().as_ref().and_then(ToPrimitive::to_f32)
110 }
111
112 /// Converts the value of `self` to an `f64`. Overflows may map to positive
113 /// or negative inifinity, otherwise `None` is returned if the value cannot
114 /// be represented by an `f64`.
115 ///
116 /// The default implementation tries to convert through `to_i64()`, and
117 /// failing that through `to_u64()`. Types implementing this trait should
118 /// override this method if they can represent a greater range.
119 #[inline]
to_f64(&self) -> Option<f64>120 fn to_f64(&self) -> Option<f64> {
121 match self.to_i64() {
122 Some(i) => i.to_f64(),
123 None => self.to_u64().as_ref().and_then(ToPrimitive::to_f64),
124 }
125 }
126 }
127
128 macro_rules! impl_to_primitive_int_to_int {
129 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
130 #[inline]
131 $(#[$cfg])*
132 fn $method(&self) -> Option<$DstT> {
133 let min = $DstT::MIN as $SrcT;
134 let max = $DstT::MAX as $SrcT;
135 if size_of::<$SrcT>() <= size_of::<$DstT>() || (min <= *self && *self <= max) {
136 Some(*self as $DstT)
137 } else {
138 None
139 }
140 }
141 )*}
142 }
143
144 macro_rules! impl_to_primitive_int_to_uint {
145 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
146 #[inline]
147 $(#[$cfg])*
148 fn $method(&self) -> Option<$DstT> {
149 let max = $DstT::MAX as $SrcT;
150 if 0 <= *self && (size_of::<$SrcT>() <= size_of::<$DstT>() || *self <= max) {
151 Some(*self as $DstT)
152 } else {
153 None
154 }
155 }
156 )*}
157 }
158
159 macro_rules! impl_to_primitive_int {
160 ($T:ident) => {
161 impl ToPrimitive for $T {
162 impl_to_primitive_int_to_int! { $T:
163 fn to_isize -> isize;
164 fn to_i8 -> i8;
165 fn to_i16 -> i16;
166 fn to_i32 -> i32;
167 fn to_i64 -> i64;
168 fn to_i128 -> i128;
169 }
170
171 impl_to_primitive_int_to_uint! { $T:
172 fn to_usize -> usize;
173 fn to_u8 -> u8;
174 fn to_u16 -> u16;
175 fn to_u32 -> u32;
176 fn to_u64 -> u64;
177 fn to_u128 -> u128;
178 }
179
180 #[inline]
181 fn to_f32(&self) -> Option<f32> {
182 Some(*self as f32)
183 }
184 #[inline]
185 fn to_f64(&self) -> Option<f64> {
186 Some(*self as f64)
187 }
188 }
189 };
190 }
191
192 impl_to_primitive_int!(isize);
193 impl_to_primitive_int!(i8);
194 impl_to_primitive_int!(i16);
195 impl_to_primitive_int!(i32);
196 impl_to_primitive_int!(i64);
197 impl_to_primitive_int!(i128);
198
199 macro_rules! impl_to_primitive_uint_to_int {
200 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
201 #[inline]
202 $(#[$cfg])*
203 fn $method(&self) -> Option<$DstT> {
204 let max = $DstT::MAX as $SrcT;
205 if size_of::<$SrcT>() < size_of::<$DstT>() || *self <= max {
206 Some(*self as $DstT)
207 } else {
208 None
209 }
210 }
211 )*}
212 }
213
214 macro_rules! impl_to_primitive_uint_to_uint {
215 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
216 #[inline]
217 $(#[$cfg])*
218 fn $method(&self) -> Option<$DstT> {
219 let max = $DstT::MAX as $SrcT;
220 if size_of::<$SrcT>() <= size_of::<$DstT>() || *self <= max {
221 Some(*self as $DstT)
222 } else {
223 None
224 }
225 }
226 )*}
227 }
228
229 macro_rules! impl_to_primitive_uint {
230 ($T:ident) => {
231 impl ToPrimitive for $T {
232 impl_to_primitive_uint_to_int! { $T:
233 fn to_isize -> isize;
234 fn to_i8 -> i8;
235 fn to_i16 -> i16;
236 fn to_i32 -> i32;
237 fn to_i64 -> i64;
238 fn to_i128 -> i128;
239 }
240
241 impl_to_primitive_uint_to_uint! { $T:
242 fn to_usize -> usize;
243 fn to_u8 -> u8;
244 fn to_u16 -> u16;
245 fn to_u32 -> u32;
246 fn to_u64 -> u64;
247 fn to_u128 -> u128;
248 }
249
250 #[inline]
251 fn to_f32(&self) -> Option<f32> {
252 Some(*self as f32)
253 }
254 #[inline]
255 fn to_f64(&self) -> Option<f64> {
256 Some(*self as f64)
257 }
258 }
259 };
260 }
261
262 impl_to_primitive_uint!(usize);
263 impl_to_primitive_uint!(u8);
264 impl_to_primitive_uint!(u16);
265 impl_to_primitive_uint!(u32);
266 impl_to_primitive_uint!(u64);
267 impl_to_primitive_uint!(u128);
268
269 macro_rules! impl_to_primitive_float_to_float {
270 ($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
271 #[inline]
272 fn $method(&self) -> Option<$DstT> {
273 // We can safely cast all values, whether NaN, +-inf, or finite.
274 // Finite values that are reducing size may saturate to +-inf.
275 Some(*self as $DstT)
276 }
277 )*}
278 }
279
280 #[cfg(has_to_int_unchecked)]
281 macro_rules! float_to_int_unchecked {
282 // SAFETY: Must not be NaN or infinite; must be representable as the integer after truncating.
283 // We already checked that the float is in the exclusive range `(MIN-1, MAX+1)`.
284 ($float:expr => $int:ty) => {
285 unsafe { $float.to_int_unchecked::<$int>() }
286 };
287 }
288
289 #[cfg(not(has_to_int_unchecked))]
290 macro_rules! float_to_int_unchecked {
291 ($float:expr => $int:ty) => {
292 $float as $int
293 };
294 }
295
296 macro_rules! impl_to_primitive_float_to_signed_int {
297 ($f:ident : $( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
298 #[inline]
299 $(#[$cfg])*
300 fn $method(&self) -> Option<$i> {
301 // Float as int truncates toward zero, so we want to allow values
302 // in the exclusive range `(MIN-1, MAX+1)`.
303 if size_of::<$f>() > size_of::<$i>() {
304 // With a larger size, we can represent the range exactly.
305 const MIN_M1: $f = $i::MIN as $f - 1.0;
306 const MAX_P1: $f = $i::MAX as $f + 1.0;
307 if *self > MIN_M1 && *self < MAX_P1 {
308 return Some(float_to_int_unchecked!(*self => $i));
309 }
310 } else {
311 // We can't represent `MIN-1` exactly, but there's no fractional part
312 // at this magnitude, so we can just use a `MIN` inclusive boundary.
313 const MIN: $f = $i::MIN as $f;
314 // We can't represent `MAX` exactly, but it will round up to exactly
315 // `MAX+1` (a power of two) when we cast it.
316 const MAX_P1: $f = $i::MAX as $f;
317 if *self >= MIN && *self < MAX_P1 {
318 return Some(float_to_int_unchecked!(*self => $i));
319 }
320 }
321 None
322 }
323 )*}
324 }
325
326 macro_rules! impl_to_primitive_float_to_unsigned_int {
327 ($f:ident : $( $(#[$cfg:meta])* fn $method:ident -> $u:ident ; )*) => {$(
328 #[inline]
329 $(#[$cfg])*
330 fn $method(&self) -> Option<$u> {
331 // Float as int truncates toward zero, so we want to allow values
332 // in the exclusive range `(-1, MAX+1)`.
333 if size_of::<$f>() > size_of::<$u>() {
334 // With a larger size, we can represent the range exactly.
335 const MAX_P1: $f = $u::MAX as $f + 1.0;
336 if *self > -1.0 && *self < MAX_P1 {
337 return Some(float_to_int_unchecked!(*self => $u));
338 }
339 } else {
340 // We can't represent `MAX` exactly, but it will round up to exactly
341 // `MAX+1` (a power of two) when we cast it.
342 // (`u128::MAX as f32` is infinity, but this is still ok.)
343 const MAX_P1: $f = $u::MAX as $f;
344 if *self > -1.0 && *self < MAX_P1 {
345 return Some(float_to_int_unchecked!(*self => $u));
346 }
347 }
348 None
349 }
350 )*}
351 }
352
353 macro_rules! impl_to_primitive_float {
354 ($T:ident) => {
355 impl ToPrimitive for $T {
356 impl_to_primitive_float_to_signed_int! { $T:
357 fn to_isize -> isize;
358 fn to_i8 -> i8;
359 fn to_i16 -> i16;
360 fn to_i32 -> i32;
361 fn to_i64 -> i64;
362 fn to_i128 -> i128;
363 }
364
365 impl_to_primitive_float_to_unsigned_int! { $T:
366 fn to_usize -> usize;
367 fn to_u8 -> u8;
368 fn to_u16 -> u16;
369 fn to_u32 -> u32;
370 fn to_u64 -> u64;
371 fn to_u128 -> u128;
372 }
373
374 impl_to_primitive_float_to_float! { $T:
375 fn to_f32 -> f32;
376 fn to_f64 -> f64;
377 }
378 }
379 };
380 }
381
382 impl_to_primitive_float!(f32);
383 impl_to_primitive_float!(f64);
384
385 /// A generic trait for converting a number to a value.
386 ///
387 /// A value can be represented by the target type when it lies within
388 /// the range of scalars supported by the target type.
389 /// For example, a negative integer cannot be represented by an unsigned
390 /// integer type, and an `i64` with a very high magnitude might not be
391 /// convertible to an `i32`.
392 /// On the other hand, conversions with possible precision loss or truncation
393 /// are admitted, like an `f32` with a decimal part to an integer type, or
394 /// even a large `f64` saturating to `f32` infinity.
395 pub trait FromPrimitive: Sized {
396 /// Converts an `isize` to return an optional value of this type. If the
397 /// value cannot be represented by this type, then `None` is returned.
398 #[inline]
from_isize(n: isize) -> Option<Self>399 fn from_isize(n: isize) -> Option<Self> {
400 n.to_i64().and_then(FromPrimitive::from_i64)
401 }
402
403 /// Converts an `i8` to return an optional value of this type. If the
404 /// value cannot be represented by this type, then `None` is returned.
405 #[inline]
from_i8(n: i8) -> Option<Self>406 fn from_i8(n: i8) -> Option<Self> {
407 FromPrimitive::from_i64(From::from(n))
408 }
409
410 /// Converts an `i16` to return an optional value of this type. If the
411 /// value cannot be represented by this type, then `None` is returned.
412 #[inline]
from_i16(n: i16) -> Option<Self>413 fn from_i16(n: i16) -> Option<Self> {
414 FromPrimitive::from_i64(From::from(n))
415 }
416
417 /// Converts an `i32` to return an optional value of this type. If the
418 /// value cannot be represented by this type, then `None` is returned.
419 #[inline]
from_i32(n: i32) -> Option<Self>420 fn from_i32(n: i32) -> Option<Self> {
421 FromPrimitive::from_i64(From::from(n))
422 }
423
424 /// Converts an `i64` to return an optional value of this type. If the
425 /// value cannot be represented by this type, then `None` is returned.
from_i64(n: i64) -> Option<Self>426 fn from_i64(n: i64) -> Option<Self>;
427
428 /// Converts an `i128` to return an optional value of this type. If the
429 /// value cannot be represented by this type, then `None` is returned.
430 ///
431 /// The default implementation converts through `from_i64()`. Types implementing
432 /// this trait should override this method if they can represent a greater range.
433 #[inline]
from_i128(n: i128) -> Option<Self>434 fn from_i128(n: i128) -> Option<Self> {
435 n.to_i64().and_then(FromPrimitive::from_i64)
436 }
437
438 /// Converts a `usize` to return an optional value of this type. If the
439 /// value cannot be represented by this type, then `None` is returned.
440 #[inline]
from_usize(n: usize) -> Option<Self>441 fn from_usize(n: usize) -> Option<Self> {
442 n.to_u64().and_then(FromPrimitive::from_u64)
443 }
444
445 /// Converts an `u8` to return an optional value of this type. If the
446 /// value cannot be represented by this type, then `None` is returned.
447 #[inline]
from_u8(n: u8) -> Option<Self>448 fn from_u8(n: u8) -> Option<Self> {
449 FromPrimitive::from_u64(From::from(n))
450 }
451
452 /// Converts an `u16` to return an optional value of this type. If the
453 /// value cannot be represented by this type, then `None` is returned.
454 #[inline]
from_u16(n: u16) -> Option<Self>455 fn from_u16(n: u16) -> Option<Self> {
456 FromPrimitive::from_u64(From::from(n))
457 }
458
459 /// Converts an `u32` to return an optional value of this type. If the
460 /// value cannot be represented by this type, then `None` is returned.
461 #[inline]
from_u32(n: u32) -> Option<Self>462 fn from_u32(n: u32) -> Option<Self> {
463 FromPrimitive::from_u64(From::from(n))
464 }
465
466 /// Converts an `u64` to return an optional value of this type. If the
467 /// value cannot be represented by this type, then `None` is returned.
from_u64(n: u64) -> Option<Self>468 fn from_u64(n: u64) -> Option<Self>;
469
470 /// Converts an `u128` to return an optional value of this type. If the
471 /// value cannot be represented by this type, then `None` is returned.
472 ///
473 /// The default implementation converts through `from_u64()`. Types implementing
474 /// this trait should override this method if they can represent a greater range.
475 #[inline]
from_u128(n: u128) -> Option<Self>476 fn from_u128(n: u128) -> Option<Self> {
477 n.to_u64().and_then(FromPrimitive::from_u64)
478 }
479
480 /// Converts a `f32` to return an optional value of this type. If the
481 /// value cannot be represented by this type, then `None` is returned.
482 #[inline]
from_f32(n: f32) -> Option<Self>483 fn from_f32(n: f32) -> Option<Self> {
484 FromPrimitive::from_f64(From::from(n))
485 }
486
487 /// Converts a `f64` to return an optional value of this type. If the
488 /// value cannot be represented by this type, then `None` is returned.
489 ///
490 /// The default implementation tries to convert through `from_i64()`, and
491 /// failing that through `from_u64()`. Types implementing this trait should
492 /// override this method if they can represent a greater range.
493 #[inline]
from_f64(n: f64) -> Option<Self>494 fn from_f64(n: f64) -> Option<Self> {
495 match n.to_i64() {
496 Some(i) => FromPrimitive::from_i64(i),
497 None => n.to_u64().and_then(FromPrimitive::from_u64),
498 }
499 }
500 }
501
502 macro_rules! impl_from_primitive {
503 ($T:ty, $to_ty:ident) => {
504 #[allow(deprecated)]
505 impl FromPrimitive for $T {
506 #[inline]
507 fn from_isize(n: isize) -> Option<$T> {
508 n.$to_ty()
509 }
510 #[inline]
511 fn from_i8(n: i8) -> Option<$T> {
512 n.$to_ty()
513 }
514 #[inline]
515 fn from_i16(n: i16) -> Option<$T> {
516 n.$to_ty()
517 }
518 #[inline]
519 fn from_i32(n: i32) -> Option<$T> {
520 n.$to_ty()
521 }
522 #[inline]
523 fn from_i64(n: i64) -> Option<$T> {
524 n.$to_ty()
525 }
526 #[inline]
527 fn from_i128(n: i128) -> Option<$T> {
528 n.$to_ty()
529 }
530
531 #[inline]
532 fn from_usize(n: usize) -> Option<$T> {
533 n.$to_ty()
534 }
535 #[inline]
536 fn from_u8(n: u8) -> Option<$T> {
537 n.$to_ty()
538 }
539 #[inline]
540 fn from_u16(n: u16) -> Option<$T> {
541 n.$to_ty()
542 }
543 #[inline]
544 fn from_u32(n: u32) -> Option<$T> {
545 n.$to_ty()
546 }
547 #[inline]
548 fn from_u64(n: u64) -> Option<$T> {
549 n.$to_ty()
550 }
551 #[inline]
552 fn from_u128(n: u128) -> Option<$T> {
553 n.$to_ty()
554 }
555
556 #[inline]
557 fn from_f32(n: f32) -> Option<$T> {
558 n.$to_ty()
559 }
560 #[inline]
561 fn from_f64(n: f64) -> Option<$T> {
562 n.$to_ty()
563 }
564 }
565 };
566 }
567
568 impl_from_primitive!(isize, to_isize);
569 impl_from_primitive!(i8, to_i8);
570 impl_from_primitive!(i16, to_i16);
571 impl_from_primitive!(i32, to_i32);
572 impl_from_primitive!(i64, to_i64);
573 impl_from_primitive!(i128, to_i128);
574 impl_from_primitive!(usize, to_usize);
575 impl_from_primitive!(u8, to_u8);
576 impl_from_primitive!(u16, to_u16);
577 impl_from_primitive!(u32, to_u32);
578 impl_from_primitive!(u64, to_u64);
579 impl_from_primitive!(u128, to_u128);
580 impl_from_primitive!(f32, to_f32);
581 impl_from_primitive!(f64, to_f64);
582
583 macro_rules! impl_to_primitive_wrapping {
584 ($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
585 #[inline]
586 $(#[$cfg])*
587 fn $method(&self) -> Option<$i> {
588 (self.0).$method()
589 }
590 )*}
591 }
592
593 impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
594 impl_to_primitive_wrapping! {
595 fn to_isize -> isize;
596 fn to_i8 -> i8;
597 fn to_i16 -> i16;
598 fn to_i32 -> i32;
599 fn to_i64 -> i64;
600 fn to_i128 -> i128;
601
602 fn to_usize -> usize;
603 fn to_u8 -> u8;
604 fn to_u16 -> u16;
605 fn to_u32 -> u32;
606 fn to_u64 -> u64;
607 fn to_u128 -> u128;
608
609 fn to_f32 -> f32;
610 fn to_f64 -> f64;
611 }
612 }
613
614 macro_rules! impl_from_primitive_wrapping {
615 ($( $(#[$cfg:meta])* fn $method:ident ( $i:ident ); )*) => {$(
616 #[inline]
617 $(#[$cfg])*
618 fn $method(n: $i) -> Option<Self> {
619 T::$method(n).map(Wrapping)
620 }
621 )*}
622 }
623
624 impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
625 impl_from_primitive_wrapping! {
626 fn from_isize(isize);
627 fn from_i8(i8);
628 fn from_i16(i16);
629 fn from_i32(i32);
630 fn from_i64(i64);
631 fn from_i128(i128);
632
633 fn from_usize(usize);
634 fn from_u8(u8);
635 fn from_u16(u16);
636 fn from_u32(u32);
637 fn from_u64(u64);
638 fn from_u128(u128);
639
640 fn from_f32(f32);
641 fn from_f64(f64);
642 }
643 }
644
645 /// Cast from one machine scalar to another.
646 ///
647 /// # Examples
648 ///
649 /// ```
650 /// # use num_traits as num;
651 /// let twenty: f32 = num::cast(0x14).unwrap();
652 /// assert_eq!(twenty, 20f32);
653 /// ```
654 ///
655 #[inline]
cast<T: NumCast, U: NumCast>(n: T) -> Option<U>656 pub fn cast<T: NumCast, U: NumCast>(n: T) -> Option<U> {
657 NumCast::from(n)
658 }
659
660 /// An interface for casting between machine scalars.
661 pub trait NumCast: Sized + ToPrimitive {
662 /// Creates a number from another value that can be converted into
663 /// a primitive via the `ToPrimitive` trait. If the source value cannot be
664 /// represented by the target type, then `None` is returned.
665 ///
666 /// A value can be represented by the target type when it lies within
667 /// the range of scalars supported by the target type.
668 /// For example, a negative integer cannot be represented by an unsigned
669 /// integer type, and an `i64` with a very high magnitude might not be
670 /// convertible to an `i32`.
671 /// On the other hand, conversions with possible precision loss or truncation
672 /// are admitted, like an `f32` with a decimal part to an integer type, or
673 /// even a large `f64` saturating to `f32` infinity.
from<T: ToPrimitive>(n: T) -> Option<Self>674 fn from<T: ToPrimitive>(n: T) -> Option<Self>;
675 }
676
677 macro_rules! impl_num_cast {
678 ($T:ty, $conv:ident) => {
679 impl NumCast for $T {
680 #[inline]
681 #[allow(deprecated)]
682 fn from<N: ToPrimitive>(n: N) -> Option<$T> {
683 // `$conv` could be generated using `concat_idents!`, but that
684 // macro seems to be broken at the moment
685 n.$conv()
686 }
687 }
688 };
689 }
690
691 impl_num_cast!(u8, to_u8);
692 impl_num_cast!(u16, to_u16);
693 impl_num_cast!(u32, to_u32);
694 impl_num_cast!(u64, to_u64);
695 impl_num_cast!(u128, to_u128);
696 impl_num_cast!(usize, to_usize);
697 impl_num_cast!(i8, to_i8);
698 impl_num_cast!(i16, to_i16);
699 impl_num_cast!(i32, to_i32);
700 impl_num_cast!(i64, to_i64);
701 impl_num_cast!(i128, to_i128);
702 impl_num_cast!(isize, to_isize);
703 impl_num_cast!(f32, to_f32);
704 impl_num_cast!(f64, to_f64);
705
706 impl<T: NumCast> NumCast for Wrapping<T> {
from<U: ToPrimitive>(n: U) -> Option<Self>707 fn from<U: ToPrimitive>(n: U) -> Option<Self> {
708 T::from(n).map(Wrapping)
709 }
710 }
711
712 /// A generic interface for casting between machine scalars with the
713 /// `as` operator, which admits narrowing and precision loss.
714 /// Implementers of this trait `AsPrimitive` should behave like a primitive
715 /// numeric type (e.g. a newtype around another primitive), and the
716 /// intended conversion must never fail.
717 ///
718 /// # Examples
719 ///
720 /// ```
721 /// # use num_traits::AsPrimitive;
722 /// let three: i32 = (3.14159265f32).as_();
723 /// assert_eq!(three, 3);
724 /// ```
725 ///
726 /// # Safety
727 ///
728 /// **In Rust versions before 1.45.0**, some uses of the `as` operator were not entirely safe.
729 /// In particular, it was undefined behavior if
730 /// a truncated floating point value could not fit in the target integer
731 /// type ([#10184](https://github.com/rust-lang/rust/issues/10184)).
732 ///
733 /// ```ignore
734 /// # use num_traits::AsPrimitive;
735 /// let x: u8 = (1.04E+17).as_(); // UB
736 /// ```
737 ///
738 pub trait AsPrimitive<T>: 'static + Copy
739 where
740 T: 'static + Copy,
741 {
742 /// Convert a value to another, using the `as` operator.
as_(self) -> T743 fn as_(self) -> T;
744 }
745
746 macro_rules! impl_as_primitive {
747 (@ $T: ty => $(#[$cfg:meta])* impl $U: ty ) => {
748 $(#[$cfg])*
749 impl AsPrimitive<$U> for $T {
750 #[inline] fn as_(self) -> $U { self as $U }
751 }
752 };
753 (@ $T: ty => { $( $U: ty ),* } ) => {$(
754 impl_as_primitive!(@ $T => impl $U);
755 )*};
756 ($T: ty => { $( $U: ty ),* } ) => {
757 impl_as_primitive!(@ $T => { $( $U ),* });
758 impl_as_primitive!(@ $T => { u8, u16, u32, u64, u128, usize });
759 impl_as_primitive!(@ $T => { i8, i16, i32, i64, i128, isize });
760 };
761 }
762
763 impl_as_primitive!(u8 => { char, f32, f64 });
764 impl_as_primitive!(i8 => { f32, f64 });
765 impl_as_primitive!(u16 => { f32, f64 });
766 impl_as_primitive!(i16 => { f32, f64 });
767 impl_as_primitive!(u32 => { f32, f64 });
768 impl_as_primitive!(i32 => { f32, f64 });
769 impl_as_primitive!(u64 => { f32, f64 });
770 impl_as_primitive!(i64 => { f32, f64 });
771 impl_as_primitive!(u128 => { f32, f64 });
772 impl_as_primitive!(i128 => { f32, f64 });
773 impl_as_primitive!(usize => { f32, f64 });
774 impl_as_primitive!(isize => { f32, f64 });
775 impl_as_primitive!(f32 => { f32, f64 });
776 impl_as_primitive!(f64 => { f32, f64 });
777 impl_as_primitive!(char => { char });
778 impl_as_primitive!(bool => {});
779