diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-03-31 10:15:26 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-31 10:15:26 -0700 |
| commit | 5d0beb7d85e8e711334c7fb6f2c5da270e5200cb (patch) | |
| tree | cd6181ba8612c9233c3a47aaf5e956a5610c348e /src/libcore | |
| parent | b3317d68910900f135f9f38e43a7a699bc736b4a (diff) | |
| parent | 232424d9952700682373ccf2d578109f401ff023 (diff) | |
| download | rust-5d0beb7d85e8e711334c7fb6f2c5da270e5200cb.tar.gz rust-5d0beb7d85e8e711334c7fb6f2c5da270e5200cb.zip | |
rollup merge of #23549: aturon/stab-num
This commit stabilizes the `std::num` module: * The `Int` and `Float` traits are deprecated in favor of (1) the newly-added inherent methods and (2) the generic traits available in rust-lang/num. * The `Zero` and `One` traits are reintroduced in `std::num`, which together with various other traits allow you to recover the most common forms of generic programming. * The `FromStrRadix` trait, and associated free function, is deprecated in favor of inherent implementations. * A wide range of methods and constants for both integers and floating point numbers are now `#[stable]`, having been adjusted for integer guidelines. * `is_positive` and `is_negative` are renamed to `is_sign_positive` and `is_sign_negative`, in order to address #22985 * The `Wrapping` type is moved to `std::num` and stabilized; `WrappingOps` is deprecated in favor of inherent methods on the integer types, and direct implementation of operations on `Wrapping<X>` for each concrete integer type `X`. Closes #22985 Closes #21069 [breaking-change] r? @alexcrichton
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cmp.rs | 2 | ||||
| -rw-r--r-- | src/libcore/fmt/num.rs | 1 | ||||
| -rw-r--r-- | src/libcore/hash/sip.rs | 5 | ||||
| -rw-r--r-- | src/libcore/iter.rs | 158 | ||||
| -rw-r--r-- | src/libcore/num/f32.rs | 48 | ||||
| -rw-r--r-- | src/libcore/num/f64.rs | 55 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 313 | ||||
| -rw-r--r-- | src/libcore/num/wrapping.rs | 178 | ||||
| -rw-r--r-- | src/libcore/str/mod.rs | 4 |
9 files changed, 522 insertions, 242 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 9ab8ab8672d..9b8b59ec8ce 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -20,8 +20,6 @@ //! //! ``` //! # #![feature(core)] -//! use std::num::SignedInt; -//! //! struct FuzzyNum { //! num: i32, //! } diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index 974252a92af..f3f5a0b70cb 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -33,6 +33,7 @@ trait GenericRadix { fn digit(&self, x: u8) -> u8; /// Format an integer using the radix using a formatter. + #[allow(deprecated)] // Int fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result { // The radix can be as low as 2, so we need a buffer of at least 64 // characters for a base 2 number. diff --git a/src/libcore/hash/sip.rs b/src/libcore/hash/sip.rs index bd1516e0cfc..e65fbac926b 100644 --- a/src/libcore/hash/sip.rs +++ b/src/libcore/hash/sip.rs @@ -12,9 +12,10 @@ //! An implementation of SipHash 2-4. +#![allow(deprecated)] // until the next snapshot for inherent wrapping ops + use prelude::*; use default::Default; -use num::wrapping::WrappingOps; use super::Hasher; /// An implementation of SipHash 2-4. @@ -71,7 +72,7 @@ macro_rules! u8to64_le { macro_rules! rotl { ($x:expr, $b:expr) => - (($x << $b) | ($x >> (64.wrapping_sub($b)))) + (($x << $b) | ($x >> (64_i32.wrapping_sub($b)))) } macro_rules! compress { diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 39bf97ae1ce..44d48f9f4bf 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -64,8 +64,8 @@ use cmp::Ord; use default::Default; use marker; use mem; -use num::{ToPrimitive, Int}; -use ops::{Add, FnMut, RangeFrom}; +use num::{Int, Zero, One, ToPrimitive}; +use ops::{Add, Sub, FnMut, RangeFrom}; use option::Option; use option::Option::{Some, None}; use marker::Sized; @@ -843,9 +843,8 @@ pub trait Iterator { /// /// ``` /// # #![feature(core)] - /// use std::num::SignedInt; /// - /// let a = [-3, 0, 1, 5, -10]; + /// let a = [-3_i32, 0, 1, 5, -10]; /// assert_eq!(*a.iter().max_by(|x| x.abs()).unwrap(), -10); /// ``` #[inline] @@ -875,9 +874,8 @@ pub trait Iterator { /// /// ``` /// # #![feature(core)] - /// use std::num::SignedInt; /// - /// let a = [-3, 0, 1, 5, -10]; + /// let a = [-3_i32, 0, 1, 5, -10]; /// assert_eq!(*a.iter().min_by(|x| x.abs()).unwrap(), 0); /// ``` #[inline] @@ -2417,6 +2415,67 @@ impl<A, St, F> Iterator for Unfold<St, F> where F: FnMut(&mut St) -> Option<A> { } } +/// Objects that can be stepped over in both directions. +/// +/// The `steps_between` function provides a way to efficiently compare +/// two `Step` objects. +#[unstable(feature = "step_trait", + reason = "likely to be replaced by finer-grained traits")] +pub trait Step: Ord { + /// Steps `self` if possible. + fn step(&self, by: &Self) -> Option<Self>; + + /// The number of steps between two step objects. + /// + /// `start` should always be less than `end`, so the result should never + /// be negative. + /// + /// Return `None` if it is not possible to calculate steps_between + /// without overflow. + fn steps_between(start: &Self, end: &Self, by: &Self) -> Option<usize>; +} + +macro_rules! step_impl { + ($($t:ty)*) => ($( + impl Step for $t { + #[inline] + fn step(&self, by: &$t) -> Option<$t> { + (*self).checked_add(*by) + } + #[inline] + #[allow(trivial_numeric_casts)] + fn steps_between(start: &$t, end: &$t, by: &$t) -> Option<usize> { + if *start <= *end { + Some(((*end - *start) / *by) as usize) + } else { + Some(0) + } + } + } + )*) +} + +macro_rules! step_impl_no_between { + ($($t:ty)*) => ($( + impl Step for $t { + #[inline] + fn step(&self, by: &$t) -> Option<$t> { + (*self).checked_add(*by) + } + #[inline] + fn steps_between(_a: &$t, _b: &$t, _by: &$t) -> Option<usize> { + None + } + } + )*) +} + +step_impl!(usize u8 u16 u32 isize i8 i16 i32); +#[cfg(target_pointer_width = "64")] +step_impl!(u64 i64); +#[cfg(target_pointer_width = "32")] +step_impl_no_between!(u64 i64); + /// An adapter for stepping range iterators by a custom amount. /// /// The resulting iterator handles overflow by stopping. The `A` @@ -2429,7 +2488,7 @@ pub struct StepBy<A, R> { range: R, } -impl<A: Add> RangeFrom<A> { +impl<A: Step> RangeFrom<A> { /// Creates an iterator starting at the same point, but stepping by /// the given amount at each iteration. /// @@ -2451,7 +2510,8 @@ impl<A: Add> RangeFrom<A> { } } -impl<A: Int> ::ops::Range<A> { +#[allow(deprecated)] +impl<A: Step> ::ops::Range<A> { /// Creates an iterator with the same range, but stepping by the /// given amount at each iteration. /// @@ -2505,14 +2565,17 @@ pub fn count<A>(start: A, step: A) -> Counter<A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<A: Add<Output=A> + Clone> Iterator for StepBy<A, RangeFrom<A>> { +impl<A> Iterator for StepBy<A, RangeFrom<A>> where + A: Clone, + for<'a> &'a A: Add<&'a A, Output = A> +{ type Item = A; #[inline] fn next(&mut self) -> Option<A> { - let result = self.range.start.clone(); - self.range.start = result.clone() + self.step_by.clone(); - Some(result) + let mut n = &self.range.start + &self.step_by; + mem::swap(&mut n, &mut self.range.start); + Some(n) } #[inline] @@ -2715,19 +2778,27 @@ pub fn range_step<A: Int>(start: A, stop: A, step: A) -> RangeStep<A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<A: Int> Iterator for StepBy<A, ::ops::Range<A>> { +#[allow(deprecated)] +impl<A: Step + Zero + Clone> Iterator for StepBy<A, ::ops::Range<A>> { type Item = A; #[inline] fn next(&mut self) -> Option<A> { - let rev = self.step_by < Int::zero(); - let start = self.range.start; - if (rev && start > self.range.end) || (!rev && start < self.range.end) { - match start.checked_add(self.step_by) { - Some(x) => self.range.start = x, - None => self.range.start = self.range.end.clone() + let rev = self.step_by < A::zero(); + if (rev && self.range.start > self.range.end) || + (!rev && self.range.start < self.range.end) + { + match self.range.start.step(&self.step_by) { + Some(mut n) => { + mem::swap(&mut self.range.start, &mut n); + Some(n) + }, + None => { + let mut n = self.range.end.clone(); + mem::swap(&mut self.range.start, &mut n); + Some(n) + } } - Some(start) } else { None } @@ -2774,6 +2845,7 @@ pub struct RangeStepInclusive<A> { #[inline] #[unstable(feature = "core", reason = "likely to be replaced by range notation and adapters")] +#[allow(deprecated)] pub fn range_step_inclusive<A: Int>(start: A, stop: A, step: A) -> RangeStepInclusive<A> { let rev = step < Int::zero(); RangeStepInclusive { @@ -2787,6 +2859,7 @@ pub fn range_step_inclusive<A: Int>(start: A, stop: A, step: A) -> RangeStepIncl #[unstable(feature = "core", reason = "likely to be replaced by range notation and adapters")] +#[allow(deprecated)] impl<A: Int> Iterator for RangeStepInclusive<A> { type Item = A; @@ -2814,15 +2887,25 @@ macro_rules! range_exact_iter_impl { } #[stable(feature = "rust1", since = "1.0.0")] -impl<A: Int> Iterator for ::ops::Range<A> { +#[allow(deprecated)] +impl<A: Step + One + Clone> Iterator for ::ops::Range<A> { type Item = A; #[inline] fn next(&mut self) -> Option<A> { if self.start < self.end { - let result = self.start; - self.start = self.start + Int::one(); - Some(result) + match self.start.step(&A::one()) { + Some(mut n) => { + mem::swap(&mut n, &mut self.start); + Some(n) + }, + None => { + let mut n = self.end.clone(); + mem::swap(&mut n, &mut self.start); + Some(n) + + } + } } else { None } @@ -2830,11 +2913,10 @@ impl<A: Int> Iterator for ::ops::Range<A> { #[inline] fn size_hint(&self) -> (usize, Option<usize>) { - if self.start >= self.end { - (0, Some(0)) + if let Some(hint) = Step::steps_between(&self.start, &self.end, &A::one()) { + (hint, Some(hint)) } else { - let length = (self.end - self.start).to_usize(); - (length.unwrap_or(0), length) + (0, None) } } } @@ -2844,12 +2926,15 @@ impl<A: Int> Iterator for ::ops::Range<A> { range_exact_iter_impl!(usize u8 u16 u32 isize i8 i16 i32); #[stable(feature = "rust1", since = "1.0.0")] -impl<A: Int> DoubleEndedIterator for ::ops::Range<A> { +#[allow(deprecated)] +impl<A: Step + One + Clone> DoubleEndedIterator for ::ops::Range<A> where + for<'a> &'a A: Sub<&'a A, Output = A> +{ #[inline] fn next_back(&mut self) -> Option<A> { if self.start < self.end { - self.end = self.end - Int::one(); - Some(self.end) + self.end = &self.end - &A::one(); + Some(self.end.clone()) } else { None } @@ -2857,15 +2942,16 @@ impl<A: Int> DoubleEndedIterator for ::ops::Range<A> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<A: Int> Iterator for ::ops::RangeFrom<A> { +#[allow(deprecated)] +impl<A: Step + One> Iterator for ::ops::RangeFrom<A> { type Item = A; #[inline] fn next(&mut self) -> Option<A> { - let result = self.start; - self.start = self.start + Int::one(); - debug_assert!(result < self.start); - Some(result) + self.start.step(&A::one()).map(|mut n| { + mem::swap(&mut n, &mut self.start); + n + }) } } diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 5b660970b86..12b45a766b6 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -22,12 +22,12 @@ use num::Float; use num::FpCategory as Fp; use option::Option; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const RADIX: u32 = 2; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MANTISSA_DIGITS: u32 = 24; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const DIGITS: u32 = 6; #[stable(feature = "rust1", since = "1.0.0")] @@ -56,14 +56,14 @@ pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32; #[stable(feature = "rust1", since = "1.0.0")] pub const MAX: f32 = 3.40282347e+38_f32; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MIN_EXP: i32 = -125; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MAX_EXP: i32 = 128; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MIN_10_EXP: i32 = -37; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MAX_10_EXP: i32 = 38; #[stable(feature = "rust1", since = "1.0.0")] @@ -73,61 +73,89 @@ pub const INFINITY: f32 = 1.0_f32/0.0_f32; #[stable(feature = "rust1", since = "1.0.0")] pub const NEG_INFINITY: f32 = -1.0_f32/0.0_f32; -/// Various useful constants. -#[unstable(feature = "core", - reason = "naming scheme needs to be revisited")] +/// Basic mathematial constants. +#[stable(feature = "rust1", since = "1.0.0")] pub mod consts { // FIXME: replace with mathematical constants from cmath. /// Archimedes' constant + #[stable(feature = "rust1", since = "1.0.0")] pub const PI: f32 = 3.14159265358979323846264338327950288_f32; /// pi * 2.0 + #[unstable(feature = "core", reason = "unclear naming convention/usefulness")] pub const PI_2: f32 = 6.28318530717958647692528676655900576_f32; /// pi/2.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32; /// pi/3.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32; /// pi/4.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32; /// pi/6.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32; /// pi/8.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32; /// 1.0/pi + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32; /// 2.0/pi + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32; /// 2.0/sqrt(pi) + #[stable(feature = "rust1", since = "1.0.0")] + pub const FRAC_2_SQRT_PI: f32 = 1.12837916709551257389615890312154517_f32; + + #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "renamed to FRAC_2_SQRT_PI")] pub const FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32; /// sqrt(2.0) + #[stable(feature = "rust1", since = "1.0.0")] + pub const SQRT_2: f32 = 1.41421356237309504880168872420969808_f32; + + #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "renamed to SQRT_2")] pub const SQRT2: f32 = 1.41421356237309504880168872420969808_f32; /// 1.0/sqrt(2.0) + #[stable(feature = "rust1", since = "1.0.0")] + pub const FRAC_1_SQRT_2: f32 = 0.707106781186547524400844362104849039_f32; + + #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "renamed to FRAC_1_SQRT_2")] pub const FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32; /// Euler's number + #[stable(feature = "rust1", since = "1.0.0")] pub const E: f32 = 2.71828182845904523536028747135266250_f32; /// log2(e) + #[stable(feature = "rust1", since = "1.0.0")] pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32; /// log10(e) + #[stable(feature = "rust1", since = "1.0.0")] pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32; /// ln(2.0) + #[stable(feature = "rust1", since = "1.0.0")] pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32; /// ln(10.0) + #[stable(feature = "rust1", since = "1.0.0")] pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32; } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 729b9422d5c..058acedd9c9 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -22,15 +22,12 @@ use num::Float; use num::FpCategory as Fp; use option::Option; -// FIXME(#5527): These constants should be deprecated once associated -// constants are implemented in favour of referencing the respective -// members of `Bounded` and `Float`. - -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const RADIX: u32 = 2; +#[stable(feature = "rust1", since = "1.0.0")] pub const MANTISSA_DIGITS: u32 = 53; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const DIGITS: u32 = 15; #[stable(feature = "rust1", since = "1.0.0")] @@ -59,14 +56,14 @@ pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64; #[stable(feature = "rust1", since = "1.0.0")] pub const MAX: f64 = 1.7976931348623157e+308_f64; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MIN_EXP: i32 = -1021; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MAX_EXP: i32 = 1024; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MIN_10_EXP: i32 = -307; -#[unstable(feature = "core", reason = "pending integer conventions")] +#[stable(feature = "rust1", since = "1.0.0")] pub const MAX_10_EXP: i32 = 308; #[stable(feature = "rust1", since = "1.0.0")] @@ -76,65 +73,89 @@ pub const INFINITY: f64 = 1.0_f64/0.0_f64; #[stable(feature = "rust1", since = "1.0.0")] pub const NEG_INFINITY: f64 = -1.0_f64/0.0_f64; -/// Various useful constants. -#[unstable(feature = "core", - reason = "naming scheme needs to be revisited")] +/// Basic mathematial constants. +#[stable(feature = "rust1", since = "1.0.0")] pub mod consts { // FIXME: replace with mathematical constants from cmath. - // FIXME(#5527): These constants should be deprecated once associated - // constants are implemented in favour of referencing the respective members - // of `Float`. - /// Archimedes' constant + #[stable(feature = "rust1", since = "1.0.0")] pub const PI: f64 = 3.14159265358979323846264338327950288_f64; /// pi * 2.0 + #[unstable(feature = "core", reason = "unclear naming convention/usefulness")] pub const PI_2: f64 = 6.28318530717958647692528676655900576_f64; /// pi/2.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64; /// pi/3.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64; /// pi/4.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64; /// pi/6.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64; /// pi/8.0 + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64; /// 1.0/pi + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64; /// 2.0/pi + #[stable(feature = "rust1", since = "1.0.0")] pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64; /// 2.0/sqrt(pi) + #[stable(feature = "rust1", since = "1.0.0")] + pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64; + + #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "renamed to FRAC_2_SQRT_PI")] pub const FRAC_2_SQRTPI: f64 = 1.12837916709551257389615890312154517_f64; /// sqrt(2.0) + #[stable(feature = "rust1", since = "1.0.0")] + pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64; + + #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "renamed to SQRT_2")] pub const SQRT2: f64 = 1.41421356237309504880168872420969808_f64; /// 1.0/sqrt(2.0) + #[stable(feature = "rust1", since = "1.0.0")] + pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64; + + #[stable(feature = "rust1", since = "1.0.0")] + #[deprecated(since = "1.0.0", reason = "renamed to FRAC_1_SQRT_2")] pub const FRAC_1_SQRT2: f64 = 0.707106781186547524400844362104849039_f64; /// Euler's number + #[stable(feature = "rust1", since = "1.0.0")] pub const E: f64 = 2.71828182845904523536028747135266250_f64; /// log2(e) + #[stable(feature = "rust1", since = "1.0.0")] pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64; /// log10(e) + #[stable(feature = "rust1", since = "1.0.0")] pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64; /// ln(2.0) + #[stable(feature = "rust1", since = "1.0.0")] pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64; /// ln(10.0) + #[stable(feature = "rust1", since = "1.0.0")] pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64; } diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index dc98bb8e603..64f8e7055a5 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -32,11 +32,66 @@ use option::Option::{self, Some, None}; use result::Result::{self, Ok, Err}; use str::{FromStr, StrExt}; +/// Provides intentionally-wrapped arithmetic on `T`. +/// +/// Operations like `+` on `u32` values is intended to never overflow, +/// and in some debug configurations overflow is detected and results +/// in a panic. While most arithmetic falls into this category, some +/// code explicitly expects and relies upon modular arithmetic (e.g., +/// hashing). +/// +/// Wrapping arithmetic can be achieved either through methods like +/// `wrapping_add`, or through the `Wrapping<T>` type, which says that +/// all standard arithmetic operations on the underlying value are +/// intended to have wrapping semantics. +#[stable(feature = "rust1", since = "1.0.0")] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)] +pub struct Wrapping<T>(pub T); + #[unstable(feature = "core", reason = "may be removed or relocated")] pub mod wrapping; +/// Types that have a "zero" value. +/// +/// This trait is intended for use in conjunction with `Add`, as an identity: +/// `x + T::zero() == x`. +#[unstable(feature = "zero_one", + reason = "unsure of placement, wants to use associated constants")] +pub trait Zero { + /// The "zero" (usually, additive identity) for this type. + fn zero() -> Self; +} + +/// Types that have a "one" value. +/// +/// This trait is intended for use in conjunction with `Mul`, as an identity: +/// `x * T::one() == x`. +#[unstable(feature = "zero_one", + reason = "unsure of placement, wants to use associated constants")] +pub trait One { + /// The "one" (usually, multiplicative identity) for this type. + fn one() -> Self; +} + +macro_rules! zero_one_impl { + ($($t:ty)*) => ($( + impl Zero for $t { + #[inline] + fn zero() -> $t { 0 } + } + impl One for $t { + #[inline] + fn one() -> $t { 1 } + } + )*) +} +zero_one_impl! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize } + /// A built-in signed or unsigned integer. #[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", + reason = "replaced by inherent methods; for generics, use rust-lang/num")] +#[allow(deprecated)] pub trait Int : Copy + Clone + NumCast @@ -450,6 +505,7 @@ macro_rules! uint_impl { $sub_with_overflow:path, $mul_with_overflow:path) => { #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] impl Int for $T { #[inline] fn zero() -> $T { 0 } @@ -589,6 +645,7 @@ macro_rules! int_impl { $sub_with_overflow:path, $mul_with_overflow:path) => { #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] impl Int for $T { #[inline] fn zero() -> $T { 0 } @@ -692,6 +749,9 @@ int_impl! { isize = i64, u64, 64, /// A built-in two's complement integer. #[stable(feature = "rust1", since = "1.0.0")] +#[deprecated(since = "1.0.0", + reason = "replaced by inherent methods; for generics, use rust-lang/num")] +#[allow(deprecated)] pub trait SignedInt : Int + Neg<Output=Self> @@ -723,6 +783,7 @@ pub trait SignedInt macro_rules! signed_int_impl { ($T:ty) => { #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] impl SignedInt for $T { #[inline] fn abs(self) -> $T { @@ -759,35 +820,25 @@ macro_rules! int_impl { $add_with_overflow:path, $sub_with_overflow:path, $mul_with_overflow:path) => { - /// Returns the `0` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn zero() -> $T { 0 } - - /// Returns the `1` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn one() -> $T { 1 } - - /// Returns the smallest value that can be represented by this integer - /// type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn min_value() -> $T { (-1 as $T) << ($BITS - 1) } - - /// Returns the largest value that can be represented by this integer - /// type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn max_value() -> $T { let min: $T = <$T>::min_value(); !min } + /// Convert a string slice in a given base to an integer. + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * src - A string slice + /// * radix - The base to use. Must lie in the range [2 .. 36] + /// + /// # Return value + /// + /// `None` if the string did not represent a valid number. + /// Otherwise, `Some(n)` where `n` is the integer represented + /// by `src`. + #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] + pub fn from_str_radix(src: &str, radix: u32) -> Result<$T, ParseIntError> { + <Self as FromStrRadix>::from_str_radix(src, radix) + } /// Returns the number of ones in the binary representation of `self`. /// @@ -801,8 +852,7 @@ macro_rules! int_impl { /// /// assert_eq!(n.count_ones(), 3); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() } @@ -818,8 +868,7 @@ macro_rules! int_impl { /// /// assert_eq!(n.count_zeros(), 5); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn count_zeros(self) -> u32 { (!self).count_ones() @@ -838,8 +887,7 @@ macro_rules! int_impl { /// /// assert_eq!(n.leading_zeros(), 10); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn leading_zeros(self) -> u32 { (self as $UnsignedT).leading_zeros() @@ -858,8 +906,7 @@ macro_rules! int_impl { /// /// assert_eq!(n.trailing_zeros(), 3); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn trailing_zeros(self) -> u32 { (self as $UnsignedT).trailing_zeros() @@ -879,8 +926,7 @@ macro_rules! int_impl { /// /// assert_eq!(n.rotate_left(12), m); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn rotate_left(self, n: u32) -> $T { (self as $UnsignedT).rotate_left(n) as $T @@ -901,8 +947,7 @@ macro_rules! int_impl { /// /// assert_eq!(n.rotate_right(12), m); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn rotate_right(self, n: u32) -> $T { (self as $UnsignedT).rotate_right(n) as $T @@ -1103,8 +1148,8 @@ macro_rules! int_impl { pub fn saturating_add(self, other: $T) -> $T { match self.checked_add(other) { Some(x) => x, - None if other >= <$T>::zero() => <$T>::max_value(), - None => <$T>::min_value(), + None if other >= <$T as Zero>::zero() => <$T>::max_value(), + None => <$T>::min_value(), } } @@ -1115,8 +1160,38 @@ macro_rules! int_impl { pub fn saturating_sub(self, other: $T) -> $T { match self.checked_sub(other) { Some(x) => x, - None if other >= <$T>::zero() => <$T>::min_value(), - None => <$T>::max_value(), + None if other >= <$T as Zero>::zero() => <$T>::min_value(), + None => <$T>::max_value(), + } + } + + /// Wrapping (modular) addition. Computes `self + other`, + /// wrapping around at the boundary of the type. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn wrapping_add(self, rhs: $T) -> $T { + unsafe { + intrinsics::overflowing_add(self, rhs) + } + } + + /// Wrapping (modular) subtraction. Computes `self - other`, + /// wrapping around at the boundary of the type. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn wrapping_sub(self, rhs: $T) -> $T { + unsafe { + intrinsics::overflowing_sub(self, rhs) + } + } + + /// Wrapping (modular) multiplication. Computes `self * + /// other`, wrapping around at the boundary of the type. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn wrapping_mul(self, rhs: $T) -> $T { + unsafe { + intrinsics::overflowing_mul(self, rhs) } } @@ -1130,12 +1205,11 @@ macro_rules! int_impl { /// /// assert_eq!(2.pow(4), 16); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn pow(self, mut exp: u32) -> $T { let mut base = self; - let mut acc = <$T>::one(); + let mut acc = <$T as One>::one(); let mut prev_base = self; let mut base_oflo = false; @@ -1161,7 +1235,7 @@ macro_rules! int_impl { /// Computes the absolute value of `self`. `Int::min_value()` will be /// returned if the number is `Int::min_value()`. - #[unstable(feature = "core", reason = "overflow in debug builds?")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn abs(self) -> $T { if self.is_negative() { -self } else { self } @@ -1256,35 +1330,25 @@ macro_rules! uint_impl { $add_with_overflow:path, $sub_with_overflow:path, $mul_with_overflow:path) => { - /// Returns the `0` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn zero() -> $T { 0 } - - /// Returns the `1` value of this integer type. - // FIXME (#5527): Should be an associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn one() -> $T { 1 } - - /// Returns the smallest value that can be represented by this integer - /// type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn min_value() -> $T { 0 } - - /// Returns the largest value that can be represented by this integer - /// type. - // FIXME (#5527): Should be and associated constant - #[unstable(feature = "core", - reason = "unsure about its place in the world")] - #[inline] - pub fn max_value() -> $T { -1 } + /// Convert a string slice in a given base to an integer. + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * src - A string slice + /// * radix - The base to use. Must lie in the range [2 .. 36] + /// + /// # Return value + /// + /// `None` if the string did not represent a valid number. + /// Otherwise, `Some(n)` where `n` is the integer represented + /// by `src`. + #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] + pub fn from_str_radix(src: &str, radix: u32) -> Result<$T, ParseIntError> { + <Self as FromStrRadix>::from_str_radix(src, radix) + } /// Returns the number of ones in the binary representation of `self`. /// @@ -1298,8 +1362,7 @@ macro_rules! uint_impl { /// /// assert_eq!(n.count_ones(), 3); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn count_ones(self) -> u32 { unsafe { $ctpop(self as $ActualT) as u32 } @@ -1317,8 +1380,7 @@ macro_rules! uint_impl { /// /// assert_eq!(n.count_zeros(), 5); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn count_zeros(self) -> u32 { (!self).count_ones() @@ -1337,8 +1399,7 @@ macro_rules! uint_impl { /// /// assert_eq!(n.leading_zeros(), 10); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn leading_zeros(self) -> u32 { unsafe { $ctlz(self as $ActualT) as u32 } @@ -1357,8 +1418,7 @@ macro_rules! uint_impl { /// /// assert_eq!(n.trailing_zeros(), 3); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn trailing_zeros(self) -> u32 { unsafe { $cttz(self as $ActualT) as u32 } @@ -1378,8 +1438,7 @@ macro_rules! uint_impl { /// /// assert_eq!(n.rotate_left(12), m); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn rotate_left(self, n: u32) -> $T { // Protect against undefined behaviour for over-long bit shifts @@ -1402,8 +1461,7 @@ macro_rules! uint_impl { /// /// assert_eq!(n.rotate_right(12), m); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn rotate_right(self, n: u32) -> $T { // Protect against undefined behaviour for over-long bit shifts @@ -1604,8 +1662,8 @@ macro_rules! uint_impl { pub fn saturating_add(self, other: $T) -> $T { match self.checked_add(other) { Some(x) => x, - None if other >= <$T>::zero() => <$T>::max_value(), - None => <$T>::min_value(), + None if other >= <$T as Zero>::zero() => <$T>::max_value(), + None => <$T>::min_value(), } } @@ -1616,8 +1674,38 @@ macro_rules! uint_impl { pub fn saturating_sub(self, other: $T) -> $T { match self.checked_sub(other) { Some(x) => x, - None if other >= <$T>::zero() => <$T>::min_value(), - None => <$T>::max_value(), + None if other >= <$T as Zero>::zero() => <$T>::min_value(), + None => <$T>::max_value(), + } + } + + /// Wrapping (modular) addition. Computes `self + other`, + /// wrapping around at the boundary of the type. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn wrapping_add(self, rhs: $T) -> $T { + unsafe { + intrinsics::overflowing_add(self, rhs) + } + } + + /// Wrapping (modular) subtraction. Computes `self - other`, + /// wrapping around at the boundary of the type. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn wrapping_sub(self, rhs: $T) -> $T { + unsafe { + intrinsics::overflowing_sub(self, rhs) + } + } + + /// Wrapping (modular) multiplication. Computes `self * + /// other`, wrapping around at the boundary of the type. + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + pub fn wrapping_mul(self, rhs: $T) -> $T { + unsafe { + intrinsics::overflowing_mul(self, rhs) } } @@ -1631,12 +1719,11 @@ macro_rules! uint_impl { /// /// assert_eq!(2.pow(4), 16); /// ``` - #[unstable(feature = "core", - reason = "pending integer conventions")] + #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn pow(self, mut exp: u32) -> $T { let mut base = self; - let mut acc = <$T>::one(); + let mut acc = <$T as One>::one(); let mut prev_base = self; let mut base_oflo = false; @@ -1664,8 +1751,8 @@ macro_rules! uint_impl { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn is_power_of_two(self) -> bool { - (self.wrapping_sub(<$T>::one())) & self == <$T>::zero() && - !(self == <$T>::zero()) + (self.wrapping_sub(<$T as One>::one())) & self == <$T as Zero>::zero() && + !(self == <$T as Zero>::zero()) } /// Returns the smallest power of two greater than or equal to `self`. @@ -1674,7 +1761,7 @@ macro_rules! uint_impl { #[inline] pub fn next_power_of_two(self) -> $T { let bits = size_of::<$T>() * 8; - let one: $T = <$T>::one(); + let one: $T = <$T as One>::one(); one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits) } @@ -2339,17 +2426,26 @@ impl_num_cast! { f64, to_f64 } /// Used for representing the classification of floating point numbers #[derive(Copy, PartialEq, Debug)] -#[unstable(feature = "core", reason = "may be renamed")] +#[stable(feature = "rust1", since = "1.0.0")] pub enum FpCategory { /// "Not a Number", often obtained by dividing by zero + #[stable(feature = "rust1", since = "1.0.0")] Nan, + /// Positive or negative infinity + #[stable(feature = "rust1", since = "1.0.0")] Infinite , + /// Positive or negative zero + #[stable(feature = "rust1", since = "1.0.0")] Zero, + /// De-normalized floating point representation (less precise than `Normal`) + #[stable(feature = "rust1", since = "1.0.0")] Subnormal, + /// A regular floating point number + #[stable(feature = "rust1", since = "1.0.0")] Normal, } @@ -2526,13 +2622,24 @@ pub trait Float /// A generic trait for converting a string with a radix (base) to a value #[unstable(feature = "core", reason = "needs reevaluation")] +#[deprecated(since = "1.0.0", + reason = "moved to inherent methods; use e.g. i32::from_str_radix")] pub trait FromStrRadix { + #[unstable(feature = "core", reason = "needs reevaluation")] + #[deprecated(since = "1.0.0", reason = "moved to inherent methods")] type Err; + + #[unstable(feature = "core", reason = "needs reevaluation")] + #[deprecated(since = "1.0.0", + reason = "moved to inherent methods; use e.g. i32::from_str_radix")] + #[allow(deprecated)] fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::Err>; } /// A utility function that just calls `FromStrRadix::from_str_radix`. #[unstable(feature = "core", reason = "needs reevaluation")] +#[deprecated(since = "1.0.0", reason = "use e.g. i32::from_str_radix")] +#[allow(deprecated)] pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: u32) -> Result<T, T::Err> { FromStrRadix::from_str_radix(str, radix) @@ -2570,12 +2677,14 @@ macro_rules! from_str_radix_float_impl { /// `None` if the string did not represent a valid number. Otherwise, /// `Some(n)` where `n` is the floating-point number represented by `src`. #[inline] + #[allow(deprecated)] fn from_str(src: &str) -> Result<$T, ParseFloatError> { from_str_radix(src, 10) } } #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] impl FromStrRadix for $T { type Err = ParseFloatError; @@ -2746,6 +2855,7 @@ from_str_radix_float_impl! { f64 } macro_rules! from_str_radix_int_impl { ($T:ty) => { #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] impl FromStr for $T { type Err = ParseIntError; #[inline] @@ -2755,6 +2865,7 @@ macro_rules! from_str_radix_int_impl { } #[stable(feature = "rust1", since = "1.0.0")] + #[allow(deprecated)] impl FromStrRadix for $T { type Err = ParseIntError; fn from_str_radix(src: &str, radix: u32) diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs index 9ecc063403c..bda05a3d9e8 100644 --- a/src/libcore/num/wrapping.rs +++ b/src/libcore/num/wrapping.rs @@ -7,7 +7,11 @@ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. + #![allow(missing_docs)] +#![allow(deprecated)] + +use super::Wrapping; use ops::*; @@ -26,6 +30,8 @@ use intrinsics::{i16_mul_with_overflow, u16_mul_with_overflow}; use intrinsics::{i32_mul_with_overflow, u32_mul_with_overflow}; use intrinsics::{i64_mul_with_overflow, u64_mul_with_overflow}; +#[unstable(feature = "core", reason = "may be removed, renamed, or relocated")] +#[deprecated(since = "1.0.0", reason = "moved to inherent methods")] pub trait WrappingOps { fn wrapping_add(self, rhs: Self) -> Self; fn wrapping_sub(self, rhs: Self) -> Self; @@ -39,6 +45,49 @@ pub trait OverflowingOps { fn overflowing_mul(self, rhs: Self) -> (Self, bool); } +macro_rules! sh_impl { + ($t:ty, $f:ty) => ( + #[stable(feature = "rust1", since = "1.0.0")] + impl Shl<$f> for Wrapping<$t> { + type Output = Wrapping<$t>; + + #[inline(always)] + fn shl(self, other: $f) -> Wrapping<$t> { + Wrapping(self.0 << other) + } + } + + #[stable(feature = "rust1", since = "1.0.0")] + impl Shr<$f> for Wrapping<$t> { + type Output = Wrapping<$t>; + + #[inline(always)] + fn shr(self, other: $f) -> Wrapping<$t> { + Wrapping(self.0 >> other) + } + } + ) +} + +// FIXME (#23545): uncomment the remaining impls +macro_rules! sh_impl_all { + ($($t:ty)*) => ($( + // sh_impl! { $t, u8 } + // sh_impl! { $t, u16 } + // sh_impl! { $t, u32 } + // sh_impl! { $t, u64 } + sh_impl! { $t, usize } + + // sh_impl! { $t, i8 } + // sh_impl! { $t, i16 } + // sh_impl! { $t, i32 } + // sh_impl! { $t, i64 } + // sh_impl! { $t, isize } + )*) +} + +sh_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize } + macro_rules! wrapping_impl { ($($t:ty)*) => ($( impl WrappingOps for $t { @@ -61,94 +110,79 @@ macro_rules! wrapping_impl { } } } - )*) -} - -wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 } - -#[unstable(feature = "core", reason = "may be removed, renamed, or relocated")] -#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)] -pub struct Wrapping<T>(pub T); - -impl<T:WrappingOps> Add for Wrapping<T> { - type Output = Wrapping<T>; - #[inline(always)] - fn add(self, other: Wrapping<T>) -> Wrapping<T> { - Wrapping(self.0.wrapping_add(other.0)) - } -} + #[stable(feature = "rust1", since = "1.0.0")] + impl Add for Wrapping<$t> { + type Output = Wrapping<$t>; -impl<T:WrappingOps> Sub for Wrapping<T> { - type Output = Wrapping<T>; - - #[inline(always)] - fn sub(self, other: Wrapping<T>) -> Wrapping<T> { - Wrapping(self.0.wrapping_sub(other.0)) - } -} + #[inline(always)] + fn add(self, other: Wrapping<$t>) -> Wrapping<$t> { + Wrapping(self.0.wrapping_add(other.0)) + } + } -impl<T:WrappingOps> Mul for Wrapping<T> { - type Output = Wrapping<T>; + #[stable(feature = "rust1", since = "1.0.0")] + impl Sub for Wrapping<$t> { + type Output = Wrapping<$t>; - #[inline(always)] - fn mul(self, other: Wrapping<T>) -> Wrapping<T> { - Wrapping(self.0.wrapping_mul(other.0)) - } -} + #[inline(always)] + fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> { + Wrapping(self.0.wrapping_sub(other.0)) + } + } -impl<T:WrappingOps+Not<Output=T>> Not for Wrapping<T> { - type Output = Wrapping<T>; + #[stable(feature = "rust1", since = "1.0.0")] + impl Mul for Wrapping<$t> { + type Output = Wrapping<$t>; - fn not(self) -> Wrapping<T> { - Wrapping(!self.0) - } -} + #[inline(always)] + fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> { + Wrapping(self.0.wrapping_mul(other.0)) + } + } -impl<T:WrappingOps+BitXor<Output=T>> BitXor for Wrapping<T> { - type Output = Wrapping<T>; + #[stable(feature = "rust1", since = "1.0.0")] + impl Not for Wrapping<$t> { + type Output = Wrapping<$t>; - #[inline(always)] - fn bitxor(self, other: Wrapping<T>) -> Wrapping<T> { - Wrapping(self.0 ^ other.0) - } -} + fn not(self) -> Wrapping<$t> { + Wrapping(!self.0) + } + } -impl<T:WrappingOps+BitOr<Output=T>> BitOr for Wrapping<T> { - type Output = Wrapping<T>; + #[stable(feature = "rust1", since = "1.0.0")] + impl BitXor for Wrapping<$t> { + type Output = Wrapping<$t>; - #[inline(always)] - fn bitor(self, other: Wrapping<T>) -> Wrapping<T> { - Wrapping(self.0 | other.0) - } -} + #[inline(always)] + fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> { + Wrapping(self.0 ^ other.0) + } + } -impl<T:WrappingOps+BitAnd<Output=T>> BitAnd for Wrapping<T> { - type Output = Wrapping<T>; + #[stable(feature = "rust1", since = "1.0.0")] + impl BitOr for Wrapping<$t> { + type Output = Wrapping<$t>; - #[inline(always)] - fn bitand(self, other: Wrapping<T>) -> Wrapping<T> { - Wrapping(self.0 & other.0) - } -} + #[inline(always)] + fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> { + Wrapping(self.0 | other.0) + } + } -impl<T:WrappingOps+Shl<usize,Output=T>> Shl<usize> for Wrapping<T> { - type Output = Wrapping<T>; + #[stable(feature = "rust1", since = "1.0.0")] + impl BitAnd for Wrapping<$t> { + type Output = Wrapping<$t>; - #[inline(always)] - fn shl(self, other: usize) -> Wrapping<T> { - Wrapping(self.0 << other) - } + #[inline(always)] + fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> { + Wrapping(self.0 & other.0) + } + } + )*) } -impl<T:WrappingOps+Shr<usize,Output=T>> Shr<usize> for Wrapping<T> { - type Output = Wrapping<T>; - - #[inline(always)] - fn shr(self, other: usize) -> Wrapping<T> { - Wrapping(self.0 >> other) - } -} +wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 } macro_rules! overflowing_impl { ($($t:ident)*) => ($( diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 189cf3d3498..f9e2b47d9b6 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -945,9 +945,9 @@ impl TwoWaySearcher { // critical factorization (u, v) and p = period(v) #[inline] #[allow(dead_code)] + #[allow(deprecated)] fn maximal_suffix(arr: &[u8], reversed: bool) -> (usize, usize) { - use num::wrapping::WrappingOps; - let mut left = -1; // Corresponds to i in the paper + let mut left: usize = -1; // Corresponds to i in the paper let mut right = 0; // Corresponds to j in the paper let mut offset = 1; // Corresponds to k in the paper let mut period = 1; // Corresponds to p in the paper |
