Deprecate Bounded trait

This commit is contained in:
Brendan Zabarauskas 2014-11-10 01:20:13 +11:00
parent e51cc089da
commit 0da49dcf13
6 changed files with 91 additions and 66 deletions

View File

@ -177,9 +177,15 @@ fn min_10_exp(_: Option<f32>) -> int { MIN_10_EXP }
#[inline]
fn max_10_exp(_: Option<f32>) -> int { MAX_10_EXP }
#[inline]
fn min_value() -> f32 { MIN_VALUE }
#[inline]
fn min_pos_value(_: Option<f32>) -> f32 { MIN_POS_VALUE }
#[inline]
fn max_value() -> f32 { MAX_VALUE }
/// Returns the mantissa, exponent and sign as integers.
fn integer_decode(self) -> (u64, i16, i8) {
let bits: u32 = unsafe { mem::transmute(self) };

View File

@ -183,9 +183,15 @@ fn min_10_exp(_: Option<f64>) -> int { MIN_10_EXP }
#[inline]
fn max_10_exp(_: Option<f64>) -> int { MAX_10_EXP }
#[inline]
fn min_value() -> f64 { MIN_VALUE }
#[inline]
fn min_pos_value(_: Option<f64>) -> f64 { MIN_POS_VALUE }
#[inline]
fn max_value() -> f64 { MAX_VALUE }
/// Returns the mantissa, exponent and sign as integers.
fn integer_decode(self) -> (u64, i16, i8) {
let bits: u64 = unsafe { mem::transmute(self) };

View File

@ -296,42 +296,6 @@ pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
}
}
/// Numbers which have upper and lower bounds
pub trait Bounded {
// FIXME (#5527): These should be associated constants
/// returns the smallest finite number this type can represent
fn min_value() -> Self;
/// returns the largest finite number this type can represent
fn max_value() -> Self;
}
macro_rules! bounded_impl(
($t:ty, $min:expr, $max:expr) => {
impl Bounded for $t {
#[inline]
fn min_value() -> $t { $min }
#[inline]
fn max_value() -> $t { $max }
}
}
)
bounded_impl!(uint, uint::MIN, uint::MAX)
bounded_impl!(u8, u8::MIN, u8::MAX)
bounded_impl!(u16, u16::MIN, u16::MAX)
bounded_impl!(u32, u32::MIN, u32::MAX)
bounded_impl!(u64, u64::MIN, u64::MAX)
bounded_impl!(int, int::MIN, int::MAX)
bounded_impl!(i8, i8::MIN, i8::MAX)
bounded_impl!(i16, i16::MIN, i16::MAX)
bounded_impl!(i32, i32::MIN, i32::MAX)
bounded_impl!(i64, i64::MIN, i64::MAX)
bounded_impl!(f32, f32::MIN_VALUE, f32::MAX_VALUE)
bounded_impl!(f64, f64::MIN_VALUE, f64::MAX_VALUE)
/// Specifies the available operations common to all of Rust's core numeric primitives.
/// These may not always make sense from a purely mathematical point of view, but
/// may be useful for systems programming.
@ -339,8 +303,7 @@ pub trait Primitive: Copy
+ Clone
+ Num
+ NumCast
+ PartialOrd
+ Bounded {}
+ PartialOrd {}
trait_impl!(Primitive for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
@ -348,13 +311,20 @@ pub trait Primitive: Copy
/// operators, bit counting methods, and endian conversion functions.
pub trait Int: Primitive
+ Ord
+ Bounded
+ Not<Self>
+ BitAnd<Self,Self>
+ BitOr<Self,Self>
+ BitXor<Self,Self>
+ Shl<uint,Self>
+ Shr<uint,Self> {
/// Returns the smallest value that can be represented by this integer.
// FIXME (#5527): Should be and associated constant
fn min_value() -> Self;
/// Returns the largest value that can be represented by this integer.
// FIXME (#5527): Should be and associated constant
fn max_value() -> Self;
/// Returns the number of ones in the binary representation of the integer.
///
/// # Example
@ -582,8 +552,8 @@ fn to_le(self) -> Self {
fn saturating_add(self, other: Self) -> Self {
match self.checked_add(other) {
Some(x) => x,
None if other >= Zero::zero() => Bounded::max_value(),
None => Bounded::min_value(),
None if other >= Zero::zero() => Int::max_value(),
None => Int::min_value(),
}
}
@ -593,8 +563,8 @@ fn saturating_add(self, other: Self) -> Self {
fn saturating_sub(self, other: Self) -> Self {
match self.checked_sub(other) {
Some(x) => x,
None if other >= Zero::zero() => Bounded::min_value(),
None => Bounded::max_value(),
None if other >= Zero::zero() => Int::min_value(),
None => Int::max_value(),
}
}
}
@ -616,6 +586,12 @@ macro_rules! uint_impl {
$sub_with_overflow:path,
$mul_with_overflow:path) => {
impl Int for $T {
#[inline]
fn min_value() -> $T { 0 }
#[inline]
fn max_value() -> $T { -1 }
#[inline]
fn count_ones(self) -> uint { unsafe { $ctpop(self as $ActualT) as uint } }
@ -729,11 +705,17 @@ unsafe fn bswap8(x: u8) -> u8 { x }
intrinsics::u64_mul_with_overflow)
macro_rules! int_impl {
($T:ty = $ActualT:ty, $UnsignedT:ty,
($T:ty = $ActualT:ty, $UnsignedT:ty, $BITS:expr,
$add_with_overflow:path,
$sub_with_overflow:path,
$mul_with_overflow:path) => {
impl Int for $T {
#[inline]
fn min_value() -> $T { (-1 as $T) << ($BITS - 1) }
#[inline]
fn max_value() -> $T { let min: $T = Int::min_value(); !min }
#[inline]
fn count_ones(self) -> uint { (self as $UnsignedT).count_ones() }
@ -771,7 +753,7 @@ fn checked_mul(self, other: $T) -> Option<$T> {
fn checked_div(self, v: $T) -> Option<$T> {
match v {
0 => None,
-1 if self == Bounded::min_value()
-1 if self == Int::min_value()
=> None,
v => Some(self / v),
}
@ -780,34 +762,34 @@ fn checked_div(self, v: $T) -> Option<$T> {
}
}
int_impl!(i8 = i8, u8,
int_impl!(i8 = i8, u8, 8,
intrinsics::i8_add_with_overflow,
intrinsics::i8_sub_with_overflow,
intrinsics::i8_mul_with_overflow)
int_impl!(i16 = i16, u16,
int_impl!(i16 = i16, u16, 16,
intrinsics::i16_add_with_overflow,
intrinsics::i16_sub_with_overflow,
intrinsics::i16_mul_with_overflow)
int_impl!(i32 = i32, u32,
int_impl!(i32 = i32, u32, 32,
intrinsics::i32_add_with_overflow,
intrinsics::i32_sub_with_overflow,
intrinsics::i32_mul_with_overflow)
int_impl!(i64 = i64, u64,
int_impl!(i64 = i64, u64, 64,
intrinsics::i64_add_with_overflow,
intrinsics::i64_sub_with_overflow,
intrinsics::i64_mul_with_overflow)
#[cfg(target_word_size = "32")]
int_impl!(int = i32, u32,
int_impl!(int = i32, u32, 32,
intrinsics::i32_add_with_overflow,
intrinsics::i32_sub_with_overflow,
intrinsics::i32_mul_with_overflow)
#[cfg(target_word_size = "64")]
int_impl!(int = i64, u64,
int_impl!(int = i64, u64, 64,
intrinsics::i64_add_with_overflow,
intrinsics::i64_sub_with_overflow,
intrinsics::i64_mul_with_overflow)
@ -930,8 +912,8 @@ macro_rules! impl_to_primitive_int_to_int(
Some($slf as $DstT)
} else {
let n = $slf as i64;
let min_value: $DstT = Bounded::min_value();
let max_value: $DstT = Bounded::max_value();
let min_value: $DstT = Int::min_value();
let max_value: $DstT = Int::max_value();
if min_value as i64 <= n && n <= max_value as i64 {
Some($slf as $DstT)
} else {
@ -946,7 +928,7 @@ macro_rules! impl_to_primitive_int_to_uint(
($SrcT:ty, $DstT:ty, $slf:expr) => (
{
let zero: $SrcT = Zero::zero();
let max_value: $DstT = Bounded::max_value();
let max_value: $DstT = Int::max_value();
if zero <= $slf && $slf as u64 <= max_value as u64 {
Some($slf as $DstT)
} else {
@ -998,7 +980,7 @@ fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
macro_rules! impl_to_primitive_uint_to_int(
($DstT:ty, $slf:expr) => (
{
let max_value: $DstT = Bounded::max_value();
let max_value: $DstT = Int::max_value();
if $slf as u64 <= max_value as u64 {
Some($slf as $DstT)
} else {
@ -1015,7 +997,7 @@ macro_rules! impl_to_primitive_uint_to_uint(
Some($slf as $DstT)
} else {
let zero: $SrcT = Zero::zero();
let max_value: $DstT = Bounded::max_value();
let max_value: $DstT = Int::max_value();
if zero <= $slf && $slf as u64 <= max_value as u64 {
Some($slf as $DstT)
} else {
@ -1071,7 +1053,7 @@ macro_rules! impl_to_primitive_float_to_float(
Some($slf as $DstT)
} else {
let n = $slf as f64;
let max_value: $SrcT = Bounded::max_value();
let max_value: $SrcT = Float::max_value();
if -max_value as f64 <= n && n <= max_value as f64 {
Some($slf as $DstT)
} else {
@ -1400,8 +1382,12 @@ pub trait Float: Signed + Primitive {
fn min_10_exp(unused_self: Option<Self>) -> int;
/// Returns the maximum base-10 exponent that this type can represent.
fn max_10_exp(unused_self: Option<Self>) -> int;
/// Returns the smallest finite value that this type can represent.
fn min_value() -> Self;
/// Returns the smallest normalized positive number that this type can represent.
fn min_pos_value(unused_self: Option<Self>) -> Self;
/// Returns the largest finite value that this type can represent.
fn max_value() -> Self;
/// Returns the mantissa, exponent and sign as integers, respectively.
fn integer_decode(self) -> (u64, i16, i8);
@ -1515,3 +1501,34 @@ pub fn is_power_of_two<T: UnsignedInt>(n: T) -> bool {
pub fn checked_next_power_of_two<T: UnsignedInt>(n: T) -> Option<T> {
n.checked_next_power_of_two()
}
#[deprecated = "Generalised bounded values are no longer supported"]
pub trait Bounded {
#[deprecated = "Use `Int::min_value` or `Float::min_value`"]
fn min_value() -> Self;
#[deprecated = "Use `Int::max_value` or `Float::max_value`"]
fn max_value() -> Self;
}
macro_rules! bounded_impl {
($T:ty, $min:expr, $max:expr) => {
impl Bounded for $T {
#[inline]
fn min_value() -> $T { $min }
#[inline]
fn max_value() -> $T { $max }
}
};
}
bounded_impl!(uint, uint::MIN, uint::MAX)
bounded_impl!(u8, u8::MIN, u8::MAX)
bounded_impl!(u16, u16::MIN, u16::MAX)
bounded_impl!(u32, u32::MIN, u32::MAX)
bounded_impl!(u64, u64::MIN, u64::MAX)
bounded_impl!(int, int::MIN, int::MAX)
bounded_impl!(i8, i8::MIN, i8::MAX)
bounded_impl!(i16, i16::MIN, i16::MAX)
bounded_impl!(i32, i32::MIN, i32::MAX)
bounded_impl!(i64, i64::MIN, i64::MAX)
bounded_impl!(f32, f32::MIN_VALUE, f32::MAX_VALUE)
bounded_impl!(f64, f64::MIN_VALUE, f64::MAX_VALUE)

View File

@ -13,7 +13,6 @@
// this is surprisingly complicated to be both generic & correct
use core::prelude::*;
use core::num::Bounded;
use Rng;
use distributions::{Sample, IndependentSample};
@ -98,7 +97,7 @@ impl SampleRange for $ty {
fn construct_range(low: $ty, high: $ty) -> Range<$ty> {
let range = high as $unsigned - low as $unsigned;
let unsigned_max: $unsigned = Bounded::max_value();
let unsigned_max: $unsigned = Int::max_value();
// this is the largest number that fits into $unsigned
// that `range` divides evenly, so, if we've sampled
@ -166,7 +165,6 @@ mod tests {
use std::prelude::*;
use distributions::{Sample, IndependentSample};
use super::Range;
use std::num::Bounded;
#[should_fail]
#[test]
@ -187,7 +185,7 @@ macro_rules! t (
$(
let v: &[($ty, $ty)] = [(0, 10),
(10, 127),
(Bounded::min_value(), Bounded::max_value())];
(Int::min_value(), Int::max_value())];
for &(low, high) in v.iter() {
let mut sampler: Range<$ty> = Range::new(low, high);
for _ in range(0u, 1000) {

View File

@ -528,7 +528,6 @@ mod tests {
extern crate rand;
use super::{Digest, Sha256, FixedBuffer};
use std::num::Bounded;
use self::rand::isaac::IsaacRng;
use self::rand::Rng;
use serialize::hex::FromHex;
@ -543,7 +542,7 @@ fn test_add_bytes_to_bits_ok() {
#[test]
#[should_fail]
fn test_add_bytes_to_bits_overflow() {
super::add_bytes_to_bits::<u64>(Bounded::max_value(), 1);
super::add_bytes_to_bits::<u64>(Int::max_value(), 1);
}
struct Test {

View File

@ -17,8 +17,7 @@
use from_str::from_str;
use iter::Iterator;
use num;
use num::{Int, Bounded};
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
use num::{Int, Float, FPNaN, FPInfinite, ToPrimitive};
use option::{None, Option, Some};
use slice::{SlicePrelude, CloneSliceAllocPrelude};
use str::StrPrelude;
@ -581,7 +580,7 @@ fn cast<T: Int>(x: uint) -> T {
let _0: T = num::zero();
let _1: T = num::one();
let is_signed = _0 > Bounded::min_value();
let is_signed = _0 > Int::min_value();
let (is_positive, src) = match src.slice_shift_char() {
(Some('-'), src) if is_signed => (false, src),