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