1
1
use core:: mem:: size_of;
2
2
use core:: num:: Wrapping ;
3
+ use core:: num:: {
4
+ NonZeroI128 , NonZeroI16 , NonZeroI32 , NonZeroI64 , NonZeroI8 , NonZeroIsize , NonZeroU128 ,
5
+ NonZeroU16 , NonZeroU32 , NonZeroU64 , NonZeroU8 , NonZeroUsize ,
6
+ } ;
3
7
use core:: { f32, f64} ;
4
8
use core:: { i128, i16, i32, i64, i8, isize} ;
5
9
use core:: { u128, u16, u32, u64, u8, usize} ;
@@ -126,9 +130,8 @@ pub trait ToPrimitive {
126
130
}
127
131
128
132
macro_rules! impl_to_primitive_int_to_int {
129
- ( $SrcT: ident : $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $DstT: ident ; ) * ) => { $(
133
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
130
134
#[ inline]
131
- $( #[ $cfg] ) *
132
135
fn $method( & self ) -> Option <$DstT> {
133
136
let min = $DstT:: MIN as $SrcT;
134
137
let max = $DstT:: MAX as $SrcT;
@@ -142,9 +145,8 @@ macro_rules! impl_to_primitive_int_to_int {
142
145
}
143
146
144
147
macro_rules! impl_to_primitive_int_to_uint {
145
- ( $SrcT: ident : $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $DstT: ident ; ) * ) => { $(
148
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
146
149
#[ inline]
147
- $( #[ $cfg] ) *
148
150
fn $method( & self ) -> Option <$DstT> {
149
151
let max = $DstT:: MAX as $SrcT;
150
152
if 0 <= * self && ( size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) || * self <= max) {
@@ -197,9 +199,8 @@ impl_to_primitive_int!(i64);
197
199
impl_to_primitive_int ! ( i128 ) ;
198
200
199
201
macro_rules! impl_to_primitive_uint_to_int {
200
- ( $SrcT: ident : $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $DstT: ident ; ) * ) => { $(
202
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
201
203
#[ inline]
202
- $( #[ $cfg] ) *
203
204
fn $method( & self ) -> Option <$DstT> {
204
205
let max = $DstT:: MAX as $SrcT;
205
206
if size_of:: <$SrcT>( ) < size_of:: <$DstT>( ) || * self <= max {
@@ -212,9 +213,8 @@ macro_rules! impl_to_primitive_uint_to_int {
212
213
}
213
214
214
215
macro_rules! impl_to_primitive_uint_to_uint {
215
- ( $SrcT: ident : $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $DstT: ident ; ) * ) => { $(
216
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
216
217
#[ inline]
217
- $( #[ $cfg] ) *
218
218
fn $method( & self ) -> Option <$DstT> {
219
219
let max = $DstT:: MAX as $SrcT;
220
220
if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) || * self <= max {
@@ -266,6 +266,54 @@ impl_to_primitive_uint!(u32);
266
266
impl_to_primitive_uint ! ( u64 ) ;
267
267
impl_to_primitive_uint ! ( u128 ) ;
268
268
269
+ macro_rules! impl_to_primitive_nonzero_to_method {
270
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
271
+ #[ inline]
272
+ fn $method( & self ) -> Option <$DstT> {
273
+ self . get( ) . $method( )
274
+ }
275
+ ) * }
276
+ }
277
+
278
+ macro_rules! impl_to_primitive_nonzero {
279
+ ( $T: ident) => {
280
+ impl ToPrimitive for $T {
281
+ impl_to_primitive_nonzero_to_method! { $T:
282
+ fn to_isize -> isize ;
283
+ fn to_i8 -> i8 ;
284
+ fn to_i16 -> i16 ;
285
+ fn to_i32 -> i32 ;
286
+ fn to_i64 -> i64 ;
287
+ fn to_i128 -> i128 ;
288
+
289
+ fn to_usize -> usize ;
290
+ fn to_u8 -> u8 ;
291
+ fn to_u16 -> u16 ;
292
+ fn to_u32 -> u32 ;
293
+ fn to_u64 -> u64 ;
294
+ fn to_u128 -> u128 ;
295
+
296
+ fn to_f32 -> f32 ;
297
+ fn to_f64 -> f64 ;
298
+ }
299
+ }
300
+ } ;
301
+ }
302
+
303
+ impl_to_primitive_nonzero ! ( NonZeroUsize ) ;
304
+ impl_to_primitive_nonzero ! ( NonZeroU8 ) ;
305
+ impl_to_primitive_nonzero ! ( NonZeroU16 ) ;
306
+ impl_to_primitive_nonzero ! ( NonZeroU32 ) ;
307
+ impl_to_primitive_nonzero ! ( NonZeroU64 ) ;
308
+ impl_to_primitive_nonzero ! ( NonZeroU128 ) ;
309
+
310
+ impl_to_primitive_nonzero ! ( NonZeroIsize ) ;
311
+ impl_to_primitive_nonzero ! ( NonZeroI8 ) ;
312
+ impl_to_primitive_nonzero ! ( NonZeroI16 ) ;
313
+ impl_to_primitive_nonzero ! ( NonZeroI32 ) ;
314
+ impl_to_primitive_nonzero ! ( NonZeroI64 ) ;
315
+ impl_to_primitive_nonzero ! ( NonZeroI128 ) ;
316
+
269
317
macro_rules! impl_to_primitive_float_to_float {
270
318
( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
271
319
#[ inline]
@@ -286,9 +334,8 @@ macro_rules! float_to_int_unchecked {
286
334
}
287
335
288
336
macro_rules! impl_to_primitive_float_to_signed_int {
289
- ( $f: ident : $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $i: ident ; ) * ) => { $(
337
+ ( $f: ident : $( fn $method: ident -> $i: ident ; ) * ) => { $(
290
338
#[ inline]
291
- $( #[ $cfg] ) *
292
339
fn $method( & self ) -> Option <$i> {
293
340
// Float as int truncates toward zero, so we want to allow values
294
341
// in the exclusive range `(MIN-1, MAX+1)`.
@@ -316,9 +363,8 @@ macro_rules! impl_to_primitive_float_to_signed_int {
316
363
}
317
364
318
365
macro_rules! impl_to_primitive_float_to_unsigned_int {
319
- ( $f: ident : $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $u: ident ; ) * ) => { $(
366
+ ( $f: ident : $( fn $method: ident -> $u: ident ; ) * ) => { $(
320
367
#[ inline]
321
- $( #[ $cfg] ) *
322
368
fn $method( & self ) -> Option <$u> {
323
369
// Float as int truncates toward zero, so we want to allow values
324
370
// in the exclusive range `(-1, MAX+1)`.
@@ -493,7 +539,6 @@ pub trait FromPrimitive: Sized {
493
539
494
540
macro_rules! impl_from_primitive {
495
541
( $T: ty, $to_ty: ident) => {
496
- #[ allow( deprecated) ]
497
542
impl FromPrimitive for $T {
498
543
#[ inline]
499
544
fn from_isize( n: isize ) -> Option <$T> {
@@ -572,10 +617,87 @@ impl_from_primitive!(u128, to_u128);
572
617
impl_from_primitive ! ( f32 , to_f32) ;
573
618
impl_from_primitive ! ( f64 , to_f64) ;
574
619
620
+ macro_rules! impl_from_primitive_nonzero {
621
+ ( $T: ty, $to_ty: ident) => {
622
+ impl FromPrimitive for $T {
623
+ #[ inline]
624
+ fn from_isize( n: isize ) -> Option <$T> {
625
+ n. $to_ty( ) . and_then( Self :: new)
626
+ }
627
+ #[ inline]
628
+ fn from_i8( n: i8 ) -> Option <$T> {
629
+ n. $to_ty( ) . and_then( Self :: new)
630
+ }
631
+ #[ inline]
632
+ fn from_i16( n: i16 ) -> Option <$T> {
633
+ n. $to_ty( ) . and_then( Self :: new)
634
+ }
635
+ #[ inline]
636
+ fn from_i32( n: i32 ) -> Option <$T> {
637
+ n. $to_ty( ) . and_then( Self :: new)
638
+ }
639
+ #[ inline]
640
+ fn from_i64( n: i64 ) -> Option <$T> {
641
+ n. $to_ty( ) . and_then( Self :: new)
642
+ }
643
+ #[ inline]
644
+ fn from_i128( n: i128 ) -> Option <$T> {
645
+ n. $to_ty( ) . and_then( Self :: new)
646
+ }
647
+
648
+ #[ inline]
649
+ fn from_usize( n: usize ) -> Option <$T> {
650
+ n. $to_ty( ) . and_then( Self :: new)
651
+ }
652
+ #[ inline]
653
+ fn from_u8( n: u8 ) -> Option <$T> {
654
+ n. $to_ty( ) . and_then( Self :: new)
655
+ }
656
+ #[ inline]
657
+ fn from_u16( n: u16 ) -> Option <$T> {
658
+ n. $to_ty( ) . and_then( Self :: new)
659
+ }
660
+ #[ inline]
661
+ fn from_u32( n: u32 ) -> Option <$T> {
662
+ n. $to_ty( ) . and_then( Self :: new)
663
+ }
664
+ #[ inline]
665
+ fn from_u64( n: u64 ) -> Option <$T> {
666
+ n. $to_ty( ) . and_then( Self :: new)
667
+ }
668
+ #[ inline]
669
+ fn from_u128( n: u128 ) -> Option <$T> {
670
+ n. $to_ty( ) . and_then( Self :: new)
671
+ }
672
+
673
+ #[ inline]
674
+ fn from_f32( n: f32 ) -> Option <$T> {
675
+ n. $to_ty( ) . and_then( Self :: new)
676
+ }
677
+ #[ inline]
678
+ fn from_f64( n: f64 ) -> Option <$T> {
679
+ n. $to_ty( ) . and_then( Self :: new)
680
+ }
681
+ }
682
+ } ;
683
+ }
684
+
685
+ impl_from_primitive_nonzero ! ( NonZeroIsize , to_isize) ;
686
+ impl_from_primitive_nonzero ! ( NonZeroI8 , to_i8) ;
687
+ impl_from_primitive_nonzero ! ( NonZeroI16 , to_i16) ;
688
+ impl_from_primitive_nonzero ! ( NonZeroI32 , to_i32) ;
689
+ impl_from_primitive_nonzero ! ( NonZeroI64 , to_i64) ;
690
+ impl_from_primitive_nonzero ! ( NonZeroI128 , to_i128) ;
691
+ impl_from_primitive_nonzero ! ( NonZeroUsize , to_usize) ;
692
+ impl_from_primitive_nonzero ! ( NonZeroU8 , to_u8) ;
693
+ impl_from_primitive_nonzero ! ( NonZeroU16 , to_u16) ;
694
+ impl_from_primitive_nonzero ! ( NonZeroU32 , to_u32) ;
695
+ impl_from_primitive_nonzero ! ( NonZeroU64 , to_u64) ;
696
+ impl_from_primitive_nonzero ! ( NonZeroU128 , to_u128) ;
697
+
575
698
macro_rules! impl_to_primitive_wrapping {
576
- ( $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $i: ident ; ) * ) => { $(
699
+ ( $( fn $method: ident -> $i: ident ; ) * ) => { $(
577
700
#[ inline]
578
- $( #[ $cfg] ) *
579
701
fn $method( & self ) -> Option <$i> {
580
702
( self . 0 ) . $method( )
581
703
}
@@ -604,9 +726,8 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
604
726
}
605
727
606
728
macro_rules! impl_from_primitive_wrapping {
607
- ( $( $ ( # [ $cfg : meta ] ) * fn $method: ident ( $i: ident ) ; ) * ) => { $(
729
+ ( $( fn $method: ident ( $i: ident ) ; ) * ) => { $(
608
730
#[ inline]
609
- $( #[ $cfg] ) *
610
731
fn $method( n: $i) -> Option <Self > {
611
732
T :: $method( n) . map( Wrapping )
612
733
}
@@ -670,10 +791,7 @@ macro_rules! impl_num_cast {
670
791
( $T: ty, $conv: ident) => {
671
792
impl NumCast for $T {
672
793
#[ inline]
673
- #[ allow( deprecated) ]
674
794
fn from<N : ToPrimitive >( n: N ) -> Option <$T> {
675
- // `$conv` could be generated using `concat_idents!`, but that
676
- // macro seems to be broken at the moment
677
795
n. $conv( )
678
796
}
679
797
}
@@ -695,6 +813,31 @@ impl_num_cast!(isize, to_isize);
695
813
impl_num_cast ! ( f32 , to_f32) ;
696
814
impl_num_cast ! ( f64 , to_f64) ;
697
815
816
+ macro_rules! impl_num_cast_nonzero {
817
+ ( $T: ty, $conv: ident) => {
818
+ impl NumCast for $T {
819
+ #[ inline]
820
+ fn from<N : ToPrimitive >( n: N ) -> Option <$T> {
821
+ n. $conv( ) . and_then( Self :: new)
822
+ }
823
+ }
824
+ } ;
825
+ }
826
+
827
+ impl_num_cast_nonzero ! ( NonZeroUsize , to_usize) ;
828
+ impl_num_cast_nonzero ! ( NonZeroU8 , to_u8) ;
829
+ impl_num_cast_nonzero ! ( NonZeroU16 , to_u16) ;
830
+ impl_num_cast_nonzero ! ( NonZeroU32 , to_u32) ;
831
+ impl_num_cast_nonzero ! ( NonZeroU64 , to_u64) ;
832
+ impl_num_cast_nonzero ! ( NonZeroU128 , to_u128) ;
833
+
834
+ impl_num_cast_nonzero ! ( NonZeroIsize , to_isize) ;
835
+ impl_num_cast_nonzero ! ( NonZeroI8 , to_i8) ;
836
+ impl_num_cast_nonzero ! ( NonZeroI16 , to_i16) ;
837
+ impl_num_cast_nonzero ! ( NonZeroI32 , to_i32) ;
838
+ impl_num_cast_nonzero ! ( NonZeroI64 , to_i64) ;
839
+ impl_num_cast_nonzero ! ( NonZeroI128 , to_i128) ;
840
+
698
841
impl < T : NumCast > NumCast for Wrapping < T > {
699
842
fn from < U : ToPrimitive > ( n : U ) -> Option < Self > {
700
843
T :: from ( n) . map ( Wrapping )
@@ -736,8 +879,7 @@ where
736
879
}
737
880
738
881
macro_rules! impl_as_primitive {
739
- ( @ $T: ty => $( #[ $cfg: meta] ) * impl $U: ty ) => {
740
- $( #[ $cfg] ) *
882
+ ( @ $T: ty => impl $U: ty ) => {
741
883
impl AsPrimitive <$U> for $T {
742
884
#[ inline] fn as_( self ) -> $U { self as $U }
743
885
}
0 commit comments