@@ -296,65 +296,35 @@ pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
296
296
}
297
297
}
298
298
299
- /// Numbers which have upper and lower bounds
300
- pub trait Bounded {
301
- // FIXME (#5527): These should be associated constants
302
- /// returns the smallest finite number this type can represent
303
- fn min_value ( ) -> Self ;
304
- /// returns the largest finite number this type can represent
305
- fn max_value ( ) -> Self ;
306
- }
307
-
308
- macro_rules! bounded_impl(
309
- ( $t: ty, $min: expr, $max: expr) => {
310
- impl Bounded for $t {
311
- #[ inline]
312
- fn min_value( ) -> $t { $min }
313
-
314
- #[ inline]
315
- fn max_value( ) -> $t { $max }
316
- }
317
- }
318
- )
319
-
320
- bounded_impl ! ( uint, uint:: MIN , uint:: MAX )
321
- bounded_impl ! ( u8 , u8 :: MIN , u8 :: MAX )
322
- bounded_impl ! ( u16 , u16 :: MIN , u16 :: MAX )
323
- bounded_impl ! ( u32 , u32 :: MIN , u32 :: MAX )
324
- bounded_impl ! ( u64 , u64 :: MIN , u64 :: MAX )
325
-
326
- bounded_impl ! ( int, int:: MIN , int:: MAX )
327
- bounded_impl ! ( i8 , i8 :: MIN , i8 :: MAX )
328
- bounded_impl ! ( i16 , i16 :: MIN , i16 :: MAX )
329
- bounded_impl ! ( i32 , i32 :: MIN , i32 :: MAX )
330
- bounded_impl ! ( i64 , i64 :: MIN , i64 :: MAX )
331
-
332
- bounded_impl ! ( f32 , f32 :: MIN_VALUE , f32 :: MAX_VALUE )
333
- bounded_impl ! ( f64 , f64 :: MIN_VALUE , f64 :: MAX_VALUE )
334
-
335
299
/// Specifies the available operations common to all of Rust's core numeric primitives.
336
300
/// These may not always make sense from a purely mathematical point of view, but
337
301
/// may be useful for systems programming.
338
302
pub trait Primitive : Copy
339
303
+ Clone
340
304
+ Num
341
305
+ NumCast
342
- + PartialOrd
343
- + Bounded { }
306
+ + PartialOrd { }
344
307
345
308
trait_impl ! ( Primitive for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 )
346
309
347
310
/// A primitive signed or unsigned integer equipped with various bitwise
348
311
/// operators, bit counting methods, and endian conversion functions.
349
312
pub trait Int : Primitive
350
313
+ Ord
351
- + Bounded
352
314
+ Not < Self >
353
315
+ BitAnd < Self , Self >
354
316
+ BitOr < Self , Self >
355
317
+ BitXor < Self , Self >
356
318
+ Shl < uint , Self >
357
319
+ Shr < uint , Self > {
320
+ /// Returns the smallest value that can be represented by this integer.
321
+ // FIXME (#5527): Should be and associated constant
322
+ fn min_value ( ) -> Self ;
323
+
324
+ /// Returns the largest value that can be represented by this integer.
325
+ // FIXME (#5527): Should be and associated constant
326
+ fn max_value ( ) -> Self ;
327
+
358
328
/// Returns the number of ones in the binary representation of the integer.
359
329
///
360
330
/// # Example
@@ -582,8 +552,8 @@ pub trait Int: Primitive
582
552
fn saturating_add ( self , other : Self ) -> Self {
583
553
match self . checked_add ( other) {
584
554
Some ( x) => x,
585
- None if other >= Zero :: zero ( ) => Bounded :: max_value ( ) ,
586
- None => Bounded :: min_value ( ) ,
555
+ None if other >= Zero :: zero ( ) => Int :: max_value ( ) ,
556
+ None => Int :: min_value ( ) ,
587
557
}
588
558
}
589
559
@@ -593,8 +563,8 @@ pub trait Int: Primitive
593
563
fn saturating_sub ( self , other : Self ) -> Self {
594
564
match self . checked_sub ( other) {
595
565
Some ( x) => x,
596
- None if other >= Zero :: zero ( ) => Bounded :: min_value ( ) ,
597
- None => Bounded :: max_value ( ) ,
566
+ None if other >= Zero :: zero ( ) => Int :: min_value ( ) ,
567
+ None => Int :: max_value ( ) ,
598
568
}
599
569
}
600
570
}
@@ -616,6 +586,12 @@ macro_rules! uint_impl {
616
586
$sub_with_overflow: path,
617
587
$mul_with_overflow: path) => {
618
588
impl Int for $T {
589
+ #[ inline]
590
+ fn min_value( ) -> $T { 0 }
591
+
592
+ #[ inline]
593
+ fn max_value( ) -> $T { -1 }
594
+
619
595
#[ inline]
620
596
fn count_ones( self ) -> uint { unsafe { $ctpop( self as $ActualT) as uint } }
621
597
@@ -729,11 +705,17 @@ uint_impl!(uint = u64, 64,
729
705
intrinsics:: u64_mul_with_overflow)
730
706
731
707
macro_rules! int_impl {
732
- ( $T: ty = $ActualT: ty, $UnsignedT: ty,
708
+ ( $T: ty = $ActualT: ty, $UnsignedT: ty, $BITS : expr ,
733
709
$add_with_overflow: path,
734
710
$sub_with_overflow: path,
735
711
$mul_with_overflow: path) => {
736
712
impl Int for $T {
713
+ #[ inline]
714
+ fn min_value( ) -> $T { ( -1 as $T) << ( $BITS - 1 ) }
715
+
716
+ #[ inline]
717
+ fn max_value( ) -> $T { let min: $T = Int :: min_value( ) ; !min }
718
+
737
719
#[ inline]
738
720
fn count_ones( self ) -> uint { ( self as $UnsignedT) . count_ones( ) }
739
721
@@ -771,7 +753,7 @@ macro_rules! int_impl {
771
753
fn checked_div( self , v: $T) -> Option <$T> {
772
754
match v {
773
755
0 => None ,
774
- -1 if self == Bounded :: min_value( )
756
+ -1 if self == Int :: min_value( )
775
757
=> None ,
776
758
v => Some ( self / v) ,
777
759
}
@@ -780,34 +762,34 @@ macro_rules! int_impl {
780
762
}
781
763
}
782
764
783
- int_impl ! ( i8 = i8 , u8 ,
765
+ int_impl ! ( i8 = i8 , u8 , 8 ,
784
766
intrinsics:: i8_add_with_overflow,
785
767
intrinsics:: i8_sub_with_overflow,
786
768
intrinsics:: i8_mul_with_overflow)
787
769
788
- int_impl ! ( i16 = i16 , u16 ,
770
+ int_impl ! ( i16 = i16 , u16 , 16 ,
789
771
intrinsics:: i16_add_with_overflow,
790
772
intrinsics:: i16_sub_with_overflow,
791
773
intrinsics:: i16_mul_with_overflow)
792
774
793
- int_impl ! ( i32 = i32 , u32 ,
775
+ int_impl ! ( i32 = i32 , u32 , 32 ,
794
776
intrinsics:: i32_add_with_overflow,
795
777
intrinsics:: i32_sub_with_overflow,
796
778
intrinsics:: i32_mul_with_overflow)
797
779
798
- int_impl ! ( i64 = i64 , u64 ,
780
+ int_impl ! ( i64 = i64 , u64 , 64 ,
799
781
intrinsics:: i64_add_with_overflow,
800
782
intrinsics:: i64_sub_with_overflow,
801
783
intrinsics:: i64_mul_with_overflow)
802
784
803
785
#[ cfg( target_word_size = "32" ) ]
804
- int_impl ! ( int = i32 , u32 ,
786
+ int_impl ! ( int = i32 , u32 , 32 ,
805
787
intrinsics:: i32_add_with_overflow,
806
788
intrinsics:: i32_sub_with_overflow,
807
789
intrinsics:: i32_mul_with_overflow)
808
790
809
791
#[ cfg( target_word_size = "64" ) ]
810
- int_impl ! ( int = i64 , u64 ,
792
+ int_impl ! ( int = i64 , u64 , 64 ,
811
793
intrinsics:: i64_add_with_overflow,
812
794
intrinsics:: i64_sub_with_overflow,
813
795
intrinsics:: i64_mul_with_overflow)
@@ -930,8 +912,8 @@ macro_rules! impl_to_primitive_int_to_int(
930
912
Some ( $slf as $DstT)
931
913
} else {
932
914
let n = $slf as i64 ;
933
- let min_value: $DstT = Bounded :: min_value( ) ;
934
- let max_value: $DstT = Bounded :: max_value( ) ;
915
+ let min_value: $DstT = Int :: min_value( ) ;
916
+ let max_value: $DstT = Int :: max_value( ) ;
935
917
if min_value as i64 <= n && n <= max_value as i64 {
936
918
Some ( $slf as $DstT)
937
919
} else {
@@ -946,7 +928,7 @@ macro_rules! impl_to_primitive_int_to_uint(
946
928
( $SrcT: ty, $DstT: ty, $slf: expr) => (
947
929
{
948
930
let zero: $SrcT = Zero :: zero( ) ;
949
- let max_value: $DstT = Bounded :: max_value( ) ;
931
+ let max_value: $DstT = Int :: max_value( ) ;
950
932
if zero <= $slf && $slf as u64 <= max_value as u64 {
951
933
Some ( $slf as $DstT)
952
934
} else {
@@ -998,7 +980,7 @@ impl_to_primitive_int!(i64)
998
980
macro_rules! impl_to_primitive_uint_to_int(
999
981
( $DstT: ty, $slf: expr) => (
1000
982
{
1001
- let max_value: $DstT = Bounded :: max_value( ) ;
983
+ let max_value: $DstT = Int :: max_value( ) ;
1002
984
if $slf as u64 <= max_value as u64 {
1003
985
Some ( $slf as $DstT)
1004
986
} else {
@@ -1015,7 +997,7 @@ macro_rules! impl_to_primitive_uint_to_uint(
1015
997
Some ( $slf as $DstT)
1016
998
} else {
1017
999
let zero: $SrcT = Zero :: zero( ) ;
1018
- let max_value: $DstT = Bounded :: max_value( ) ;
1000
+ let max_value: $DstT = Int :: max_value( ) ;
1019
1001
if zero <= $slf && $slf as u64 <= max_value as u64 {
1020
1002
Some ( $slf as $DstT)
1021
1003
} else {
@@ -1071,7 +1053,7 @@ macro_rules! impl_to_primitive_float_to_float(
1071
1053
Some ( $slf as $DstT)
1072
1054
} else {
1073
1055
let n = $slf as f64 ;
1074
- let max_value: $SrcT = Bounded :: max_value( ) ;
1056
+ let max_value: $SrcT = Float :: max_value( ) ;
1075
1057
if -max_value as f64 <= n && n <= max_value as f64 {
1076
1058
Some ( $slf as $DstT)
1077
1059
} else {
@@ -1400,8 +1382,12 @@ pub trait Float: Signed + Primitive {
1400
1382
fn min_10_exp ( unused_self : Option < Self > ) -> int ;
1401
1383
/// Returns the maximum base-10 exponent that this type can represent.
1402
1384
fn max_10_exp ( unused_self : Option < Self > ) -> int ;
1385
+ /// Returns the smallest finite value that this type can represent.
1386
+ fn min_value ( ) -> Self ;
1403
1387
/// Returns the smallest normalized positive number that this type can represent.
1404
1388
fn min_pos_value ( unused_self : Option < Self > ) -> Self ;
1389
+ /// Returns the largest finite value that this type can represent.
1390
+ fn max_value ( ) -> Self ;
1405
1391
1406
1392
/// Returns the mantissa, exponent and sign as integers, respectively.
1407
1393
fn integer_decode ( self ) -> ( u64 , i16 , i8 ) ;
@@ -1515,3 +1501,34 @@ pub fn is_power_of_two<T: UnsignedInt>(n: T) -> bool {
1515
1501
pub fn checked_next_power_of_two < T : UnsignedInt > ( n : T ) -> Option < T > {
1516
1502
n. checked_next_power_of_two ( )
1517
1503
}
1504
+
1505
+ #[ deprecated = "Generalised bounded values are no longer supported" ]
1506
+ pub trait Bounded {
1507
+ #[ deprecated = "Use `Int::min_value` or `Float::min_value`" ]
1508
+ fn min_value ( ) -> Self ;
1509
+ #[ deprecated = "Use `Int::max_value` or `Float::max_value`" ]
1510
+ fn max_value ( ) -> Self ;
1511
+ }
1512
+ macro_rules! bounded_impl {
1513
+ ( $T: ty, $min: expr, $max: expr) => {
1514
+ impl Bounded for $T {
1515
+ #[ inline]
1516
+ fn min_value( ) -> $T { $min }
1517
+
1518
+ #[ inline]
1519
+ fn max_value( ) -> $T { $max }
1520
+ }
1521
+ } ;
1522
+ }
1523
+ bounded_impl ! ( uint, uint:: MIN , uint:: MAX )
1524
+ bounded_impl ! ( u8 , u8 :: MIN , u8 :: MAX )
1525
+ bounded_impl ! ( u16 , u16 :: MIN , u16 :: MAX )
1526
+ bounded_impl ! ( u32 , u32 :: MIN , u32 :: MAX )
1527
+ bounded_impl ! ( u64 , u64 :: MIN , u64 :: MAX )
1528
+ bounded_impl ! ( int, int:: MIN , int:: MAX )
1529
+ bounded_impl ! ( i8 , i8 :: MIN , i8 :: MAX )
1530
+ bounded_impl ! ( i16 , i16 :: MIN , i16 :: MAX )
1531
+ bounded_impl ! ( i32 , i32 :: MIN , i32 :: MAX )
1532
+ bounded_impl ! ( i64 , i64 :: MIN , i64 :: MAX )
1533
+ bounded_impl ! ( f32 , f32 :: MIN_VALUE , f32 :: MAX_VALUE )
1534
+ bounded_impl ! ( f64 , f64 :: MIN_VALUE , f64 :: MAX_VALUE )
0 commit comments