1use core::mem::size_of;
2use core::num::Wrapping;
3use core::{f32, f64};
4use core::{i128, i16, i32, i64, i8, isize};
5use core::{u128, u16, u32, u64, u8, usize};
67/// 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.
17pub 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]
21fn to_isize(&self) -> Option<isize> {
22self.to_i64().as_ref().and_then(ToPrimitive::to_isize)
23 }
2425/// Converts the value of `self` to an `i8`. If the value cannot be
26 /// represented by an `i8`, then `None` is returned.
27#[inline]
28fn to_i8(&self) -> Option<i8> {
29self.to_i64().as_ref().and_then(ToPrimitive::to_i8)
30 }
3132/// Converts the value of `self` to an `i16`. If the value cannot be
33 /// represented by an `i16`, then `None` is returned.
34#[inline]
35fn to_i16(&self) -> Option<i16> {
36self.to_i64().as_ref().and_then(ToPrimitive::to_i16)
37 }
3839/// Converts the value of `self` to an `i32`. If the value cannot be
40 /// represented by an `i32`, then `None` is returned.
41#[inline]
42fn to_i32(&self) -> Option<i32> {
43self.to_i64().as_ref().and_then(ToPrimitive::to_i32)
44 }
4546/// Converts the value of `self` to an `i64`. If the value cannot be
47 /// represented by an `i64`, then `None` is returned.
48fn to_i64(&self) -> Option<i64>;
4950/// 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]
57fn to_i128(&self) -> Option<i128> {
58self.to_i64().map(From::from)
59 }
6061/// Converts the value of `self` to a `usize`. If the value cannot be
62 /// represented by a `usize`, then `None` is returned.
63#[inline]
64fn to_usize(&self) -> Option<usize> {
65self.to_u64().as_ref().and_then(ToPrimitive::to_usize)
66 }
6768/// Converts the value of `self` to a `u8`. If the value cannot be
69 /// represented by a `u8`, then `None` is returned.
70#[inline]
71fn to_u8(&self) -> Option<u8> {
72self.to_u64().as_ref().and_then(ToPrimitive::to_u8)
73 }
7475/// Converts the value of `self` to a `u16`. If the value cannot be
76 /// represented by a `u16`, then `None` is returned.
77#[inline]
78fn to_u16(&self) -> Option<u16> {
79self.to_u64().as_ref().and_then(ToPrimitive::to_u16)
80 }
8182/// Converts the value of `self` to a `u32`. If the value cannot be
83 /// represented by a `u32`, then `None` is returned.
84#[inline]
85fn to_u32(&self) -> Option<u32> {
86self.to_u64().as_ref().and_then(ToPrimitive::to_u32)
87 }
8889/// Converts the value of `self` to a `u64`. If the value cannot be
90 /// represented by a `u64`, then `None` is returned.
91fn to_u64(&self) -> Option<u64>;
9293/// 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]
100fn to_u128(&self) -> Option<u128> {
101self.to_u64().map(From::from)
102 }
103104/// 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]
108fn to_f32(&self) -> Option<f32> {
109self.to_f64().as_ref().and_then(ToPrimitive::to_f32)
110 }
111112/// 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]
120fn to_f64(&self) -> Option<f64> {
121match self.to_i64() {
122Some(i) => i.to_f64(),
123None => self.to_u64().as_ref().and_then(ToPrimitive::to_f64),
124 }
125 }
126}
127128macro_rules! impl_to_primitive_int_to_int {
129 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
130#[inline]
131$(#[$cfg])*
132fn $method(&self) -> Option<$DstT> {
133let min = $DstT::MIN as $SrcT;
134let max = $DstT::MAX as $SrcT;
135if size_of::<$SrcT>() <= size_of::<$DstT>() || (min <= *self && *self <= max) {
136Some(*self as $DstT)
137 } else {
138None
139}
140 }
141 )*}
142}
143144macro_rules! impl_to_primitive_int_to_uint {
145 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
146#[inline]
147$(#[$cfg])*
148fn $method(&self) -> Option<$DstT> {
149let max = $DstT::MAX as $SrcT;
150if 0 <= *self && (size_of::<$SrcT>() <= size_of::<$DstT>() || *self <= max) {
151Some(*self as $DstT)
152 } else {
153None
154}
155 }
156 )*}
157}
158159macro_rules! impl_to_primitive_int {
160 ($T:ident) => {
161impl ToPrimitive for $T {
162impl_to_primitive_int_to_int! { $T:
163fn to_isize -> isize;
164fn to_i8 -> i8;
165fn to_i16 -> i16;
166fn to_i32 -> i32;
167fn to_i64 -> i64;
168fn to_i128 -> i128;
169 }
170171impl_to_primitive_int_to_uint! { $T:
172fn to_usize -> usize;
173fn to_u8 -> u8;
174fn to_u16 -> u16;
175fn to_u32 -> u32;
176fn to_u64 -> u64;
177fn to_u128 -> u128;
178 }
179180#[inline]
181fn to_f32(&self) -> Option<f32> {
182Some(*self as f32)
183 }
184#[inline]
185fn to_f64(&self) -> Option<f64> {
186Some(*self as f64)
187 }
188 }
189 };
190}
191192impl_to_primitive_int!(isize);
193impl_to_primitive_int!(i8);
194impl_to_primitive_int!(i16);
195impl_to_primitive_int!(i32);
196impl_to_primitive_int!(i64);
197impl_to_primitive_int!(i128);
198199macro_rules! impl_to_primitive_uint_to_int {
200 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
201#[inline]
202$(#[$cfg])*
203fn $method(&self) -> Option<$DstT> {
204let max = $DstT::MAX as $SrcT;
205if size_of::<$SrcT>() < size_of::<$DstT>() || *self <= max {
206Some(*self as $DstT)
207 } else {
208None
209}
210 }
211 )*}
212}
213214macro_rules! impl_to_primitive_uint_to_uint {
215 ($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
216#[inline]
217$(#[$cfg])*
218fn $method(&self) -> Option<$DstT> {
219let max = $DstT::MAX as $SrcT;
220if size_of::<$SrcT>() <= size_of::<$DstT>() || *self <= max {
221Some(*self as $DstT)
222 } else {
223None
224}
225 }
226 )*}
227}
228229macro_rules! impl_to_primitive_uint {
230 ($T:ident) => {
231impl ToPrimitive for $T {
232impl_to_primitive_uint_to_int! { $T:
233fn to_isize -> isize;
234fn to_i8 -> i8;
235fn to_i16 -> i16;
236fn to_i32 -> i32;
237fn to_i64 -> i64;
238fn to_i128 -> i128;
239 }
240241impl_to_primitive_uint_to_uint! { $T:
242fn to_usize -> usize;
243fn to_u8 -> u8;
244fn to_u16 -> u16;
245fn to_u32 -> u32;
246fn to_u64 -> u64;
247fn to_u128 -> u128;
248 }
249250#[inline]
251fn to_f32(&self) -> Option<f32> {
252Some(*self as f32)
253 }
254#[inline]
255fn to_f64(&self) -> Option<f64> {
256Some(*self as f64)
257 }
258 }
259 };
260}
261262impl_to_primitive_uint!(usize);
263impl_to_primitive_uint!(u8);
264impl_to_primitive_uint!(u16);
265impl_to_primitive_uint!(u32);
266impl_to_primitive_uint!(u64);
267impl_to_primitive_uint!(u128);
268269macro_rules! impl_to_primitive_float_to_float {
270 ($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
271#[inline]
272fn $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.
275Some(*self as $DstT)
276 }
277 )*}
278}
279280#[cfg(has_to_int_unchecked)]
281macro_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) => {
285unsafe { $float.to_int_unchecked::<$int>() }
286 };
287}
288289#[cfg(not(has_to_int_unchecked))]
290macro_rules! float_to_int_unchecked {
291 ($float:expr => $int:ty) => {
292$float as $int
293};
294}
295296macro_rules! impl_to_primitive_float_to_signed_int {
297 ($f:ident : $( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
298#[inline]
299$(#[$cfg])*
300fn $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)`.
303if size_of::<$f>() > size_of::<$i>() {
304// With a larger size, we can represent the range exactly.
305const MIN_M1: $f = $i::MIN as $f - 1.0;
306const MAX_P1: $f = $i::MAX as $f + 1.0;
307if *self > MIN_M1 && *self < MAX_P1 {
308return 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.
313const 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.
316const MAX_P1: $f = $i::MAX as $f;
317if *self >= MIN && *self < MAX_P1 {
318return Some(float_to_int_unchecked!(*self => $i));
319 }
320 }
321None
322}
323 )*}
324}
325326macro_rules! impl_to_primitive_float_to_unsigned_int {
327 ($f:ident : $( $(#[$cfg:meta])* fn $method:ident -> $u:ident ; )*) => {$(
328#[inline]
329$(#[$cfg])*
330fn $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)`.
333if size_of::<$f>() > size_of::<$u>() {
334// With a larger size, we can represent the range exactly.
335const MAX_P1: $f = $u::MAX as $f + 1.0;
336if *self > -1.0 && *self < MAX_P1 {
337return 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.)
343const MAX_P1: $f = $u::MAX as $f;
344if *self > -1.0 && *self < MAX_P1 {
345return Some(float_to_int_unchecked!(*self => $u));
346 }
347 }
348None
349}
350 )*}
351}
352353macro_rules! impl_to_primitive_float {
354 ($T:ident) => {
355impl ToPrimitive for $T {
356impl_to_primitive_float_to_signed_int! { $T:
357fn to_isize -> isize;
358fn to_i8 -> i8;
359fn to_i16 -> i16;
360fn to_i32 -> i32;
361fn to_i64 -> i64;
362fn to_i128 -> i128;
363 }
364365impl_to_primitive_float_to_unsigned_int! { $T:
366fn to_usize -> usize;
367fn to_u8 -> u8;
368fn to_u16 -> u16;
369fn to_u32 -> u32;
370fn to_u64 -> u64;
371fn to_u128 -> u128;
372 }
373374impl_to_primitive_float_to_float! { $T:
375fn to_f32 -> f32;
376fn to_f64 -> f64;
377 }
378 }
379 };
380}
381382impl_to_primitive_float!(f32);
383impl_to_primitive_float!(f64);
384385/// 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.
395pub 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]
399fn from_isize(n: isize) -> Option<Self> {
400 n.to_i64().and_then(FromPrimitive::from_i64)
401 }
402403/// 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]
406fn from_i8(n: i8) -> Option<Self> {
407 FromPrimitive::from_i64(From::from(n))
408 }
409410/// 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]
413fn from_i16(n: i16) -> Option<Self> {
414 FromPrimitive::from_i64(From::from(n))
415 }
416417/// 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]
420fn from_i32(n: i32) -> Option<Self> {
421 FromPrimitive::from_i64(From::from(n))
422 }
423424/// 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.
426fn from_i64(n: i64) -> Option<Self>;
427428/// 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]
434fn from_i128(n: i128) -> Option<Self> {
435 n.to_i64().and_then(FromPrimitive::from_i64)
436 }
437438/// 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]
441fn from_usize(n: usize) -> Option<Self> {
442 n.to_u64().and_then(FromPrimitive::from_u64)
443 }
444445/// 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]
448fn from_u8(n: u8) -> Option<Self> {
449 FromPrimitive::from_u64(From::from(n))
450 }
451452/// 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]
455fn from_u16(n: u16) -> Option<Self> {
456 FromPrimitive::from_u64(From::from(n))
457 }
458459/// 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]
462fn from_u32(n: u32) -> Option<Self> {
463 FromPrimitive::from_u64(From::from(n))
464 }
465466/// 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.
468fn from_u64(n: u64) -> Option<Self>;
469470/// 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]
476fn from_u128(n: u128) -> Option<Self> {
477 n.to_u64().and_then(FromPrimitive::from_u64)
478 }
479480/// 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]
483fn from_f32(n: f32) -> Option<Self> {
484 FromPrimitive::from_f64(From::from(n))
485 }
486487/// 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]
494fn from_f64(n: f64) -> Option<Self> {
495match n.to_i64() {
496Some(i) => FromPrimitive::from_i64(i),
497None => n.to_u64().and_then(FromPrimitive::from_u64),
498 }
499 }
500}
501502macro_rules! impl_from_primitive {
503 ($T:ty, $to_ty:ident) => {
504#[allow(deprecated)]
505impl FromPrimitive for $T {
506#[inline]
507fn from_isize(n: isize) -> Option<$T> {
508 n.$to_ty()
509 }
510#[inline]
511fn from_i8(n: i8) -> Option<$T> {
512 n.$to_ty()
513 }
514#[inline]
515fn from_i16(n: i16) -> Option<$T> {
516 n.$to_ty()
517 }
518#[inline]
519fn from_i32(n: i32) -> Option<$T> {
520 n.$to_ty()
521 }
522#[inline]
523fn from_i64(n: i64) -> Option<$T> {
524 n.$to_ty()
525 }
526#[inline]
527fn from_i128(n: i128) -> Option<$T> {
528 n.$to_ty()
529 }
530531#[inline]
532fn from_usize(n: usize) -> Option<$T> {
533 n.$to_ty()
534 }
535#[inline]
536fn from_u8(n: u8) -> Option<$T> {
537 n.$to_ty()
538 }
539#[inline]
540fn from_u16(n: u16) -> Option<$T> {
541 n.$to_ty()
542 }
543#[inline]
544fn from_u32(n: u32) -> Option<$T> {
545 n.$to_ty()
546 }
547#[inline]
548fn from_u64(n: u64) -> Option<$T> {
549 n.$to_ty()
550 }
551#[inline]
552fn from_u128(n: u128) -> Option<$T> {
553 n.$to_ty()
554 }
555556#[inline]
557fn from_f32(n: f32) -> Option<$T> {
558 n.$to_ty()
559 }
560#[inline]
561fn from_f64(n: f64) -> Option<$T> {
562 n.$to_ty()
563 }
564 }
565 };
566}
567568impl_from_primitive!(isize, to_isize);
569impl_from_primitive!(i8, to_i8);
570impl_from_primitive!(i16, to_i16);
571impl_from_primitive!(i32, to_i32);
572impl_from_primitive!(i64, to_i64);
573impl_from_primitive!(i128, to_i128);
574impl_from_primitive!(usize, to_usize);
575impl_from_primitive!(u8, to_u8);
576impl_from_primitive!(u16, to_u16);
577impl_from_primitive!(u32, to_u32);
578impl_from_primitive!(u64, to_u64);
579impl_from_primitive!(u128, to_u128);
580impl_from_primitive!(f32, to_f32);
581impl_from_primitive!(f64, to_f64);
582583macro_rules! impl_to_primitive_wrapping {
584 ($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
585#[inline]
586$(#[$cfg])*
587fn $method(&self) -> Option<$i> {
588 (self.0).$method()
589 }
590 )*}
591}
592593impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
594impl_to_primitive_wrapping! {
595fn to_isize -> isize;
596fn to_i8 -> i8;
597fn to_i16 -> i16;
598fn to_i32 -> i32;
599fn to_i64 -> i64;
600fn to_i128 -> i128;
601602fn to_usize -> usize;
603fn to_u8 -> u8;
604fn to_u16 -> u16;
605fn to_u32 -> u32;
606fn to_u64 -> u64;
607fn to_u128 -> u128;
608609fn to_f32 -> f32;
610fn to_f64 -> f64;
611 }
612}
613614macro_rules! impl_from_primitive_wrapping {
615 ($( $(#[$cfg:meta])* fn $method:ident ( $i:ident ); )*) => {$(
616#[inline]
617$(#[$cfg])*
618fn $method(n: $i) -> Option<Self> {
619 T::$method(n).map(Wrapping)
620 }
621 )*}
622}
623624impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
625impl_from_primitive_wrapping! {
626fn from_isize(isize);
627fn from_i8(i8);
628fn from_i16(i16);
629fn from_i32(i32);
630fn from_i64(i64);
631fn from_i128(i128);
632633fn from_usize(usize);
634fn from_u8(u8);
635fn from_u16(u16);
636fn from_u32(u32);
637fn from_u64(u64);
638fn from_u128(u128);
639640fn from_f32(f32);
641fn from_f64(f64);
642 }
643}
644645/// 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]
656pub fn cast<T: NumCast, U: NumCast>(n: T) -> Option<U> {
657 NumCast::from(n)
658}
659660/// An interface for casting between machine scalars.
661pub 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.
674fn from<T: ToPrimitive>(n: T) -> Option<Self>;
675}
676677macro_rules! impl_num_cast {
678 ($T:ty, $conv:ident) => {
679impl NumCast for $T {
680#[inline]
681 #[allow(deprecated)]
682fn 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
685n.$conv()
686 }
687 }
688 };
689}
690691impl_num_cast!(u8, to_u8);
692impl_num_cast!(u16, to_u16);
693impl_num_cast!(u32, to_u32);
694impl_num_cast!(u64, to_u64);
695impl_num_cast!(u128, to_u128);
696impl_num_cast!(usize, to_usize);
697impl_num_cast!(i8, to_i8);
698impl_num_cast!(i16, to_i16);
699impl_num_cast!(i32, to_i32);
700impl_num_cast!(i64, to_i64);
701impl_num_cast!(i128, to_i128);
702impl_num_cast!(isize, to_isize);
703impl_num_cast!(f32, to_f32);
704impl_num_cast!(f64, to_f64);
705706impl<T: NumCast> NumCast for Wrapping<T> {
707fn from<U: ToPrimitive>(n: U) -> Option<Self> {
708 T::from(n).map(Wrapping)
709 }
710}
711712/// 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///
738pub trait AsPrimitive<T>: 'static + Copy
739where
740T: 'static + Copy,
741{
742/// Convert a value to another, using the `as` operator.
743fn as_(self) -> T;
744}
745746macro_rules! impl_as_primitive {
747 (@ $T: ty => $(#[$cfg:meta])* impl $U: ty ) => {
748 $(#[$cfg])*
749impl AsPrimitive<$U> for $T {
750#[inline] fn as_(self) -> $U { self as $U }
751 }
752 };
753 (@ $T: ty => { $( $U: ty ),* } ) => {$(
754impl_as_primitive!(@ $T => impl $U);
755 )*};
756 ($T: ty => { $( $U: ty ),* } ) => {
757impl_as_primitive!(@ $T => { $( $U ),* });
758impl_as_primitive!(@ $T => { u8, u16, u32, u64, u128, usize });
759impl_as_primitive!(@ $T => { i8, i16, i32, i64, i128, isize });
760 };
761}
762763impl_as_primitive!(u8 => { char, f32, f64 });
764impl_as_primitive!(i8 => { f32, f64 });
765impl_as_primitive!(u16 => { f32, f64 });
766impl_as_primitive!(i16 => { f32, f64 });
767impl_as_primitive!(u32 => { f32, f64 });
768impl_as_primitive!(i32 => { f32, f64 });
769impl_as_primitive!(u64 => { f32, f64 });
770impl_as_primitive!(i64 => { f32, f64 });
771impl_as_primitive!(u128 => { f32, f64 });
772impl_as_primitive!(i128 => { f32, f64 });
773impl_as_primitive!(usize => { f32, f64 });
774impl_as_primitive!(isize => { f32, f64 });
775impl_as_primitive!(f32 => { f32, f64 });
776impl_as_primitive!(f64 => { f32, f64 });
777impl_as_primitive!(char => { char });
778impl_as_primitive!(bool => {});