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};
6
7pub trait ToPrimitive {
18 #[inline]
21 fn to_isize(&self) -> Option<isize> {
22 self.to_i64().as_ref().and_then(ToPrimitive::to_isize)
23 }
24
25 #[inline]
28 fn to_i8(&self) -> Option<i8> {
29 self.to_i64().as_ref().and_then(ToPrimitive::to_i8)
30 }
31
32 #[inline]
35 fn to_i16(&self) -> Option<i16> {
36 self.to_i64().as_ref().and_then(ToPrimitive::to_i16)
37 }
38
39 #[inline]
42 fn to_i32(&self) -> Option<i32> {
43 self.to_i64().as_ref().and_then(ToPrimitive::to_i32)
44 }
45
46 fn to_i64(&self) -> Option<i64>;
49
50 #[inline]
57 fn to_i128(&self) -> Option<i128> {
58 self.to_i64().map(From::from)
59 }
60
61 #[inline]
64 fn to_usize(&self) -> Option<usize> {
65 self.to_u64().as_ref().and_then(ToPrimitive::to_usize)
66 }
67
68 #[inline]
71 fn to_u8(&self) -> Option<u8> {
72 self.to_u64().as_ref().and_then(ToPrimitive::to_u8)
73 }
74
75 #[inline]
78 fn to_u16(&self) -> Option<u16> {
79 self.to_u64().as_ref().and_then(ToPrimitive::to_u16)
80 }
81
82 #[inline]
85 fn to_u32(&self) -> Option<u32> {
86 self.to_u64().as_ref().and_then(ToPrimitive::to_u32)
87 }
88
89 fn to_u64(&self) -> Option<u64>;
92
93 #[inline]
100 fn to_u128(&self) -> Option<u128> {
101 self.to_u64().map(From::from)
102 }
103
104 #[inline]
108 fn to_f32(&self) -> Option<f32> {
109 self.to_f64().as_ref().and_then(ToPrimitive::to_f32)
110 }
111
112 #[inline]
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
128macro_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
144macro_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
159macro_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
192impl_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);
198
199macro_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
214macro_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
229macro_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
262impl_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);
268
269macro_rules! impl_to_primitive_float_to_float {
270 ($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
271 #[inline]
272 fn $method(&self) -> Option<$DstT> {
273 Some(*self as $DstT)
276 }
277 )*}
278}
279
280macro_rules! float_to_int_unchecked {
281 ($float:expr => $int:ty) => {
284 unsafe { $float.to_int_unchecked::<$int>() }
285 };
286}
287
288macro_rules! impl_to_primitive_float_to_signed_int {
289 ($f:ident : $( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
290 #[inline]
291 $(#[$cfg])*
292 fn $method(&self) -> Option<$i> {
293 if size_of::<$f>() > size_of::<$i>() {
296 const MIN_M1: $f = $i::MIN as $f - 1.0;
298 const MAX_P1: $f = $i::MAX as $f + 1.0;
299 if *self > MIN_M1 && *self < MAX_P1 {
300 return Some(float_to_int_unchecked!(*self => $i));
301 }
302 } else {
303 const MIN: $f = $i::MIN as $f;
306 const MAX_P1: $f = $i::MAX as $f;
309 if *self >= MIN && *self < MAX_P1 {
310 return Some(float_to_int_unchecked!(*self => $i));
311 }
312 }
313 None
314 }
315 )*}
316}
317
318macro_rules! impl_to_primitive_float_to_unsigned_int {
319 ($f:ident : $( $(#[$cfg:meta])* fn $method:ident -> $u:ident ; )*) => {$(
320 #[inline]
321 $(#[$cfg])*
322 fn $method(&self) -> Option<$u> {
323 if size_of::<$f>() > size_of::<$u>() {
326 const MAX_P1: $f = $u::MAX as $f + 1.0;
328 if *self > -1.0 && *self < MAX_P1 {
329 return Some(float_to_int_unchecked!(*self => $u));
330 }
331 } else {
332 const MAX_P1: $f = $u::MAX as $f;
336 if *self > -1.0 && *self < MAX_P1 {
337 return Some(float_to_int_unchecked!(*self => $u));
338 }
339 }
340 None
341 }
342 )*}
343}
344
345macro_rules! impl_to_primitive_float {
346 ($T:ident) => {
347 impl ToPrimitive for $T {
348 impl_to_primitive_float_to_signed_int! { $T:
349 fn to_isize -> isize;
350 fn to_i8 -> i8;
351 fn to_i16 -> i16;
352 fn to_i32 -> i32;
353 fn to_i64 -> i64;
354 fn to_i128 -> i128;
355 }
356
357 impl_to_primitive_float_to_unsigned_int! { $T:
358 fn to_usize -> usize;
359 fn to_u8 -> u8;
360 fn to_u16 -> u16;
361 fn to_u32 -> u32;
362 fn to_u64 -> u64;
363 fn to_u128 -> u128;
364 }
365
366 impl_to_primitive_float_to_float! { $T:
367 fn to_f32 -> f32;
368 fn to_f64 -> f64;
369 }
370 }
371 };
372}
373
374impl_to_primitive_float!(f32);
375impl_to_primitive_float!(f64);
376
377pub trait FromPrimitive: Sized {
388 #[inline]
391 fn from_isize(n: isize) -> Option<Self> {
392 n.to_i64().and_then(FromPrimitive::from_i64)
393 }
394
395 #[inline]
398 fn from_i8(n: i8) -> Option<Self> {
399 FromPrimitive::from_i64(From::from(n))
400 }
401
402 #[inline]
405 fn from_i16(n: i16) -> Option<Self> {
406 FromPrimitive::from_i64(From::from(n))
407 }
408
409 #[inline]
412 fn from_i32(n: i32) -> Option<Self> {
413 FromPrimitive::from_i64(From::from(n))
414 }
415
416 fn from_i64(n: i64) -> Option<Self>;
419
420 #[inline]
426 fn from_i128(n: i128) -> Option<Self> {
427 n.to_i64().and_then(FromPrimitive::from_i64)
428 }
429
430 #[inline]
433 fn from_usize(n: usize) -> Option<Self> {
434 n.to_u64().and_then(FromPrimitive::from_u64)
435 }
436
437 #[inline]
440 fn from_u8(n: u8) -> Option<Self> {
441 FromPrimitive::from_u64(From::from(n))
442 }
443
444 #[inline]
447 fn from_u16(n: u16) -> Option<Self> {
448 FromPrimitive::from_u64(From::from(n))
449 }
450
451 #[inline]
454 fn from_u32(n: u32) -> Option<Self> {
455 FromPrimitive::from_u64(From::from(n))
456 }
457
458 fn from_u64(n: u64) -> Option<Self>;
461
462 #[inline]
468 fn from_u128(n: u128) -> Option<Self> {
469 n.to_u64().and_then(FromPrimitive::from_u64)
470 }
471
472 #[inline]
475 fn from_f32(n: f32) -> Option<Self> {
476 FromPrimitive::from_f64(From::from(n))
477 }
478
479 #[inline]
486 fn from_f64(n: f64) -> Option<Self> {
487 match n.to_i64() {
488 Some(i) => FromPrimitive::from_i64(i),
489 None => n.to_u64().and_then(FromPrimitive::from_u64),
490 }
491 }
492}
493
494macro_rules! impl_from_primitive {
495 ($T:ty, $to_ty:ident) => {
496 #[allow(deprecated)]
497 impl FromPrimitive for $T {
498 #[inline]
499 fn from_isize(n: isize) -> Option<$T> {
500 n.$to_ty()
501 }
502 #[inline]
503 fn from_i8(n: i8) -> Option<$T> {
504 n.$to_ty()
505 }
506 #[inline]
507 fn from_i16(n: i16) -> Option<$T> {
508 n.$to_ty()
509 }
510 #[inline]
511 fn from_i32(n: i32) -> Option<$T> {
512 n.$to_ty()
513 }
514 #[inline]
515 fn from_i64(n: i64) -> Option<$T> {
516 n.$to_ty()
517 }
518 #[inline]
519 fn from_i128(n: i128) -> Option<$T> {
520 n.$to_ty()
521 }
522
523 #[inline]
524 fn from_usize(n: usize) -> Option<$T> {
525 n.$to_ty()
526 }
527 #[inline]
528 fn from_u8(n: u8) -> Option<$T> {
529 n.$to_ty()
530 }
531 #[inline]
532 fn from_u16(n: u16) -> Option<$T> {
533 n.$to_ty()
534 }
535 #[inline]
536 fn from_u32(n: u32) -> Option<$T> {
537 n.$to_ty()
538 }
539 #[inline]
540 fn from_u64(n: u64) -> Option<$T> {
541 n.$to_ty()
542 }
543 #[inline]
544 fn from_u128(n: u128) -> Option<$T> {
545 n.$to_ty()
546 }
547
548 #[inline]
549 fn from_f32(n: f32) -> Option<$T> {
550 n.$to_ty()
551 }
552 #[inline]
553 fn from_f64(n: f64) -> Option<$T> {
554 n.$to_ty()
555 }
556 }
557 };
558}
559
560impl_from_primitive!(isize, to_isize);
561impl_from_primitive!(i8, to_i8);
562impl_from_primitive!(i16, to_i16);
563impl_from_primitive!(i32, to_i32);
564impl_from_primitive!(i64, to_i64);
565impl_from_primitive!(i128, to_i128);
566impl_from_primitive!(usize, to_usize);
567impl_from_primitive!(u8, to_u8);
568impl_from_primitive!(u16, to_u16);
569impl_from_primitive!(u32, to_u32);
570impl_from_primitive!(u64, to_u64);
571impl_from_primitive!(u128, to_u128);
572impl_from_primitive!(f32, to_f32);
573impl_from_primitive!(f64, to_f64);
574
575macro_rules! impl_to_primitive_wrapping {
576 ($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
577 #[inline]
578 $(#[$cfg])*
579 fn $method(&self) -> Option<$i> {
580 (self.0).$method()
581 }
582 )*}
583}
584
585impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
586 impl_to_primitive_wrapping! {
587 fn to_isize -> isize;
588 fn to_i8 -> i8;
589 fn to_i16 -> i16;
590 fn to_i32 -> i32;
591 fn to_i64 -> i64;
592 fn to_i128 -> i128;
593
594 fn to_usize -> usize;
595 fn to_u8 -> u8;
596 fn to_u16 -> u16;
597 fn to_u32 -> u32;
598 fn to_u64 -> u64;
599 fn to_u128 -> u128;
600
601 fn to_f32 -> f32;
602 fn to_f64 -> f64;
603 }
604}
605
606macro_rules! impl_from_primitive_wrapping {
607 ($( $(#[$cfg:meta])* fn $method:ident ( $i:ident ); )*) => {$(
608 #[inline]
609 $(#[$cfg])*
610 fn $method(n: $i) -> Option<Self> {
611 T::$method(n).map(Wrapping)
612 }
613 )*}
614}
615
616impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
617 impl_from_primitive_wrapping! {
618 fn from_isize(isize);
619 fn from_i8(i8);
620 fn from_i16(i16);
621 fn from_i32(i32);
622 fn from_i64(i64);
623 fn from_i128(i128);
624
625 fn from_usize(usize);
626 fn from_u8(u8);
627 fn from_u16(u16);
628 fn from_u32(u32);
629 fn from_u64(u64);
630 fn from_u128(u128);
631
632 fn from_f32(f32);
633 fn from_f64(f64);
634 }
635}
636
637#[inline]
648pub fn cast<T: NumCast, U: NumCast>(n: T) -> Option<U> {
649 NumCast::from(n)
650}
651
652pub trait NumCast: Sized + ToPrimitive {
654 fn from<T: ToPrimitive>(n: T) -> Option<Self>;
667}
668
669macro_rules! impl_num_cast {
670 ($T:ty, $conv:ident) => {
671 impl NumCast for $T {
672 #[inline]
673 #[allow(deprecated)]
674 fn from<N: ToPrimitive>(n: N) -> Option<$T> {
675 n.$conv()
678 }
679 }
680 };
681}
682
683impl_num_cast!(u8, to_u8);
684impl_num_cast!(u16, to_u16);
685impl_num_cast!(u32, to_u32);
686impl_num_cast!(u64, to_u64);
687impl_num_cast!(u128, to_u128);
688impl_num_cast!(usize, to_usize);
689impl_num_cast!(i8, to_i8);
690impl_num_cast!(i16, to_i16);
691impl_num_cast!(i32, to_i32);
692impl_num_cast!(i64, to_i64);
693impl_num_cast!(i128, to_i128);
694impl_num_cast!(isize, to_isize);
695impl_num_cast!(f32, to_f32);
696impl_num_cast!(f64, to_f64);
697
698impl<T: NumCast> NumCast for Wrapping<T> {
699 fn from<U: ToPrimitive>(n: U) -> Option<Self> {
700 T::from(n).map(Wrapping)
701 }
702}
703
704pub trait AsPrimitive<T>: 'static + Copy
731where
732 T: 'static + Copy,
733{
734 fn as_(self) -> T;
736}
737
738macro_rules! impl_as_primitive {
739 (@ $T: ty => $(#[$cfg:meta])* impl $U: ty ) => {
740 $(#[$cfg])*
741 impl AsPrimitive<$U> for $T {
742 #[inline] fn as_(self) -> $U { self as $U }
743 }
744 };
745 (@ $T: ty => { $( $U: ty ),* } ) => {$(
746 impl_as_primitive!(@ $T => impl $U);
747 )*};
748 ($T: ty => { $( $U: ty ),* } ) => {
749 impl_as_primitive!(@ $T => { $( $U ),* });
750 impl_as_primitive!(@ $T => { u8, u16, u32, u64, u128, usize });
751 impl_as_primitive!(@ $T => { i8, i16, i32, i64, i128, isize });
752 };
753}
754
755impl_as_primitive!(u8 => { char, f32, f64 });
756impl_as_primitive!(i8 => { f32, f64 });
757impl_as_primitive!(u16 => { f32, f64 });
758impl_as_primitive!(i16 => { f32, f64 });
759impl_as_primitive!(u32 => { f32, f64 });
760impl_as_primitive!(i32 => { f32, f64 });
761impl_as_primitive!(u64 => { f32, f64 });
762impl_as_primitive!(i64 => { f32, f64 });
763impl_as_primitive!(u128 => { f32, f64 });
764impl_as_primitive!(i128 => { f32, f64 });
765impl_as_primitive!(usize => { f32, f64 });
766impl_as_primitive!(isize => { f32, f64 });
767impl_as_primitive!(f32 => { f32, f64 });
768impl_as_primitive!(f64 => { f32, f64 });
769impl_as_primitive!(char => { char });
770impl_as_primitive!(bool => {});