Skip to content

Commit 0da49dc

Browse files
committed
Deprecate Bounded trait
1 parent e51cc08 commit 0da49dc

File tree

6 files changed

+91
-66
lines changed

6 files changed

+91
-66
lines changed

src/libcore/num/f32.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,15 @@ impl Float for f32 {
177177
#[inline]
178178
fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
179179

180+
#[inline]
181+
fn min_value() -> f32 { MIN_VALUE }
182+
180183
#[inline]
181184
fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
182185

186+
#[inline]
187+
fn max_value() -> f32 { MAX_VALUE }
188+
183189
/// Returns the mantissa, exponent and sign as integers.
184190
fn integer_decode(self) -> (u64, i16, i8) {
185191
let bits: u32 = unsafe { mem::transmute(self) };

src/libcore/num/f64.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,15 @@ impl Float for f64 {
183183
#[inline]
184184
fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP }
185185

186+
#[inline]
187+
fn min_value() -> f64 { MIN_VALUE }
188+
186189
#[inline]
187190
fn min_pos_value(_: Option<f64>) -> f64 { MIN_POS_VALUE }
188191

192+
#[inline]
193+
fn max_value() -> f64 { MAX_VALUE }
194+
189195
/// Returns the mantissa, exponent and sign as integers.
190196
fn integer_decode(self) -> (u64, i16, i8) {
191197
let bits: u64 = unsafe { mem::transmute(self) };

src/libcore/num/mod.rs

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -296,65 +296,35 @@ pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
296296
}
297297
}
298298

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-
335299
/// Specifies the available operations common to all of Rust's core numeric primitives.
336300
/// These may not always make sense from a purely mathematical point of view, but
337301
/// may be useful for systems programming.
338302
pub trait Primitive: Copy
339303
+ Clone
340304
+ Num
341305
+ NumCast
342-
+ PartialOrd
343-
+ Bounded {}
306+
+ PartialOrd {}
344307

345308
trait_impl!(Primitive for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
346309

347310
/// A primitive signed or unsigned integer equipped with various bitwise
348311
/// operators, bit counting methods, and endian conversion functions.
349312
pub trait Int: Primitive
350313
+ Ord
351-
+ Bounded
352314
+ Not<Self>
353315
+ BitAnd<Self,Self>
354316
+ BitOr<Self,Self>
355317
+ BitXor<Self,Self>
356318
+ Shl<uint,Self>
357319
+ 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+
358328
/// Returns the number of ones in the binary representation of the integer.
359329
///
360330
/// # Example
@@ -582,8 +552,8 @@ pub trait Int: Primitive
582552
fn saturating_add(self, other: Self) -> Self {
583553
match self.checked_add(other) {
584554
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(),
587557
}
588558
}
589559

@@ -593,8 +563,8 @@ pub trait Int: Primitive
593563
fn saturating_sub(self, other: Self) -> Self {
594564
match self.checked_sub(other) {
595565
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(),
598568
}
599569
}
600570
}
@@ -616,6 +586,12 @@ macro_rules! uint_impl {
616586
$sub_with_overflow:path,
617587
$mul_with_overflow:path) => {
618588
impl Int for $T {
589+
#[inline]
590+
fn min_value() -> $T { 0 }
591+
592+
#[inline]
593+
fn max_value() -> $T { -1 }
594+
619595
#[inline]
620596
fn count_ones(self) -> uint { unsafe { $ctpop(self as $ActualT) as uint } }
621597

@@ -729,11 +705,17 @@ uint_impl!(uint = u64, 64,
729705
intrinsics::u64_mul_with_overflow)
730706

731707
macro_rules! int_impl {
732-
($T:ty = $ActualT:ty, $UnsignedT:ty,
708+
($T:ty = $ActualT:ty, $UnsignedT:ty, $BITS:expr,
733709
$add_with_overflow:path,
734710
$sub_with_overflow:path,
735711
$mul_with_overflow:path) => {
736712
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+
737719
#[inline]
738720
fn count_ones(self) -> uint { (self as $UnsignedT).count_ones() }
739721

@@ -771,7 +753,7 @@ macro_rules! int_impl {
771753
fn checked_div(self, v: $T) -> Option<$T> {
772754
match v {
773755
0 => None,
774-
-1 if self == Bounded::min_value()
756+
-1 if self == Int::min_value()
775757
=> None,
776758
v => Some(self / v),
777759
}
@@ -780,34 +762,34 @@ macro_rules! int_impl {
780762
}
781763
}
782764

783-
int_impl!(i8 = i8, u8,
765+
int_impl!(i8 = i8, u8, 8,
784766
intrinsics::i8_add_with_overflow,
785767
intrinsics::i8_sub_with_overflow,
786768
intrinsics::i8_mul_with_overflow)
787769

788-
int_impl!(i16 = i16, u16,
770+
int_impl!(i16 = i16, u16, 16,
789771
intrinsics::i16_add_with_overflow,
790772
intrinsics::i16_sub_with_overflow,
791773
intrinsics::i16_mul_with_overflow)
792774

793-
int_impl!(i32 = i32, u32,
775+
int_impl!(i32 = i32, u32, 32,
794776
intrinsics::i32_add_with_overflow,
795777
intrinsics::i32_sub_with_overflow,
796778
intrinsics::i32_mul_with_overflow)
797779

798-
int_impl!(i64 = i64, u64,
780+
int_impl!(i64 = i64, u64, 64,
799781
intrinsics::i64_add_with_overflow,
800782
intrinsics::i64_sub_with_overflow,
801783
intrinsics::i64_mul_with_overflow)
802784

803785
#[cfg(target_word_size = "32")]
804-
int_impl!(int = i32, u32,
786+
int_impl!(int = i32, u32, 32,
805787
intrinsics::i32_add_with_overflow,
806788
intrinsics::i32_sub_with_overflow,
807789
intrinsics::i32_mul_with_overflow)
808790

809791
#[cfg(target_word_size = "64")]
810-
int_impl!(int = i64, u64,
792+
int_impl!(int = i64, u64, 64,
811793
intrinsics::i64_add_with_overflow,
812794
intrinsics::i64_sub_with_overflow,
813795
intrinsics::i64_mul_with_overflow)
@@ -930,8 +912,8 @@ macro_rules! impl_to_primitive_int_to_int(
930912
Some($slf as $DstT)
931913
} else {
932914
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();
935917
if min_value as i64 <= n && n <= max_value as i64 {
936918
Some($slf as $DstT)
937919
} else {
@@ -946,7 +928,7 @@ macro_rules! impl_to_primitive_int_to_uint(
946928
($SrcT:ty, $DstT:ty, $slf:expr) => (
947929
{
948930
let zero: $SrcT = Zero::zero();
949-
let max_value: $DstT = Bounded::max_value();
931+
let max_value: $DstT = Int::max_value();
950932
if zero <= $slf && $slf as u64 <= max_value as u64 {
951933
Some($slf as $DstT)
952934
} else {
@@ -998,7 +980,7 @@ impl_to_primitive_int!(i64)
998980
macro_rules! impl_to_primitive_uint_to_int(
999981
($DstT:ty, $slf:expr) => (
1000982
{
1001-
let max_value: $DstT = Bounded::max_value();
983+
let max_value: $DstT = Int::max_value();
1002984
if $slf as u64 <= max_value as u64 {
1003985
Some($slf as $DstT)
1004986
} else {
@@ -1015,7 +997,7 @@ macro_rules! impl_to_primitive_uint_to_uint(
1015997
Some($slf as $DstT)
1016998
} else {
1017999
let zero: $SrcT = Zero::zero();
1018-
let max_value: $DstT = Bounded::max_value();
1000+
let max_value: $DstT = Int::max_value();
10191001
if zero <= $slf && $slf as u64 <= max_value as u64 {
10201002
Some($slf as $DstT)
10211003
} else {
@@ -1071,7 +1053,7 @@ macro_rules! impl_to_primitive_float_to_float(
10711053
Some($slf as $DstT)
10721054
} else {
10731055
let n = $slf as f64;
1074-
let max_value: $SrcT = Bounded::max_value();
1056+
let max_value: $SrcT = Float::max_value();
10751057
if -max_value as f64 <= n && n <= max_value as f64 {
10761058
Some($slf as $DstT)
10771059
} else {
@@ -1400,8 +1382,12 @@ pub trait Float: Signed + Primitive {
14001382
fn min_10_exp(unused_self: Option<Self>) -> int;
14011383
/// Returns the maximum base-10 exponent that this type can represent.
14021384
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;
14031387
/// Returns the smallest normalized positive number that this type can represent.
14041388
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;
14051391

14061392
/// Returns the mantissa, exponent and sign as integers, respectively.
14071393
fn integer_decode(self) -> (u64, i16, i8);
@@ -1515,3 +1501,34 @@ pub fn is_power_of_two<T: UnsignedInt>(n: T) -> bool {
15151501
pub fn checked_next_power_of_two<T: UnsignedInt>(n: T) -> Option<T> {
15161502
n.checked_next_power_of_two()
15171503
}
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)

src/librand/distributions/range.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// this is surprisingly complicated to be both generic & correct
1414

1515
use core::prelude::*;
16-
use core::num::Bounded;
1716

1817
use Rng;
1918
use distributions::{Sample, IndependentSample};
@@ -98,7 +97,7 @@ macro_rules! integer_impl {
9897

9998
fn construct_range(low: $ty, high: $ty) -> Range<$ty> {
10099
let range = high as $unsigned - low as $unsigned;
101-
let unsigned_max: $unsigned = Bounded::max_value();
100+
let unsigned_max: $unsigned = Int::max_value();
102101

103102
// this is the largest number that fits into $unsigned
104103
// that `range` divides evenly, so, if we've sampled
@@ -166,7 +165,6 @@ mod tests {
166165
use std::prelude::*;
167166
use distributions::{Sample, IndependentSample};
168167
use super::Range;
169-
use std::num::Bounded;
170168

171169
#[should_fail]
172170
#[test]
@@ -187,7 +185,7 @@ mod tests {
187185
$(
188186
let v: &[($ty, $ty)] = [(0, 10),
189187
(10, 127),
190-
(Bounded::min_value(), Bounded::max_value())];
188+
(Int::min_value(), Int::max_value())];
191189
for &(low, high) in v.iter() {
192190
let mut sampler: Range<$ty> = Range::new(low, high);
193191
for _ in range(0u, 1000) {

src/librustc_back/sha2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@ mod tests {
528528
extern crate rand;
529529

530530
use super::{Digest, Sha256, FixedBuffer};
531-
use std::num::Bounded;
532531
use self::rand::isaac::IsaacRng;
533532
use self::rand::Rng;
534533
use serialize::hex::FromHex;
@@ -543,7 +542,7 @@ mod tests {
543542
#[test]
544543
#[should_fail]
545544
fn test_add_bytes_to_bits_overflow() {
546-
super::add_bytes_to_bits::<u64>(Bounded::max_value(), 1);
545+
super::add_bytes_to_bits::<u64>(Int::max_value(), 1);
547546
}
548547

549548
struct Test {

src/libstd/num/strconv.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use char::Char;
1717
use from_str::from_str;
1818
use iter::Iterator;
1919
use num;
20-
use num::{Int, Bounded};
21-
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
20+
use num::{Int, Float, FPNaN, FPInfinite, ToPrimitive};
2221
use option::{None, Option, Some};
2322
use slice::{SlicePrelude, CloneSliceAllocPrelude};
2423
use str::StrPrelude;
@@ -581,7 +580,7 @@ pub fn from_str_radix_int<T: Int>(src: &str, radix: uint) -> Option<T> {
581580

582581
let _0: T = num::zero();
583582
let _1: T = num::one();
584-
let is_signed = _0 > Bounded::min_value();
583+
let is_signed = _0 > Int::min_value();
585584

586585
let (is_positive, src) = match src.slice_shift_char() {
587586
(Some('-'), src) if is_signed => (false, src),

0 commit comments

Comments
 (0)