diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-04-21 15:28:53 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-04-21 15:28:53 -0700 |
| commit | a1dd5ac78745a9f266573d539ba34bbd75b50277 (patch) | |
| tree | 584e29815ca61d4045fa6bfa048d3804c7ce529a /src/libcore/fmt | |
| parent | 98e9765d973d46faa5c80fb37a48040ca9e87b28 (diff) | |
| parent | a568a7f9f2eb3fa3f3e049df288ef0ad32cc7881 (diff) | |
| download | rust-a1dd5ac78745a9f266573d539ba34bbd75b50277.tar.gz rust-a1dd5ac78745a9f266573d539ba34bbd75b50277.zip | |
rollup merge of #24636: alexcrichton/remove-deprecated
Conflicts: src/libcore/result.rs
Diffstat (limited to 'src/libcore/fmt')
| -rw-r--r-- | src/libcore/fmt/float.rs | 43 | ||||
| -rw-r--r-- | src/libcore/fmt/mod.rs | 24 | ||||
| -rw-r--r-- | src/libcore/fmt/num.rs | 43 |
3 files changed, 66 insertions, 44 deletions
diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 72c25c68040..4b75bd5f67e 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -11,15 +11,15 @@ pub use self::ExponentFormat::*; pub use self::SignificantDigits::*; -use char::{self, CharExt}; +use prelude::*; + +use char; use fmt; -use iter::Iterator; -use num::{cast, Float, ToPrimitive}; +use num::Float; use num::FpCategory as Fp; -use ops::FnOnce; -use result::Result::Ok; -use slice::{self, SliceExt}; -use str::{self, StrExt}; +use ops::{Div, Rem, Mul}; +use slice; +use str; /// A flag that specifies whether to use exponential (scientific) notation. pub enum ExponentFormat { @@ -42,6 +42,21 @@ pub enum SignificantDigits { DigExact(usize) } +#[doc(hidden)] +pub trait MyFloat: Float + PartialEq + PartialOrd + Div<Output=Self> + + Mul<Output=Self> + Rem<Output=Self> + Copy { + fn from_u32(u: u32) -> Self; + fn to_i32(&self) -> i32; +} + +macro_rules! doit { + ($($t:ident)*) => ($(impl MyFloat for $t { + fn from_u32(u: u32) -> $t { u as $t } + fn to_i32(&self) -> i32 { *self as i32 } + })*) +} +doit! { f32 f64 } + /// Converts a float number to its string representation. /// This is meant to be a common base implementation for various formatting styles. /// The number is assumed to be non-negative, callers use `Formatter::pad_integral` @@ -63,7 +78,7 @@ pub enum SignificantDigits { /// # Panics /// /// - Panics if `num` is negative. -pub fn float_to_str_bytes_common<T: Float, U, F>( +pub fn float_to_str_bytes_common<T: MyFloat, U, F>( num: T, digits: SignificantDigits, exp_format: ExponentFormat, @@ -72,10 +87,10 @@ pub fn float_to_str_bytes_common<T: Float, U, F>( ) -> U where F: FnOnce(&str) -> U, { - let _0: T = Float::zero(); - let _1: T = Float::one(); + let _0: T = T::zero(); + let _1: T = T::one(); let radix: u32 = 10; - let radix_f: T = cast(radix).unwrap(); + let radix_f = T::from_u32(radix); assert!(num.is_nan() || num >= _0, "float_to_str_bytes_common: number is negative"); @@ -99,7 +114,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>( let (num, exp) = match exp_format { ExpDec if num != _0 => { let exp = num.log10().floor(); - (num / radix_f.powf(exp), cast::<T, i32>(exp).unwrap()) + (num / radix_f.powf(exp), exp.to_i32()) } _ => (num, 0) }; @@ -114,7 +129,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>( deccum = deccum / radix_f; deccum = deccum.trunc(); - let c = char::from_digit(current_digit.to_isize().unwrap() as u32, radix); + let c = char::from_digit(current_digit.to_i32() as u32, radix); buf[end] = c.unwrap() as u8; end += 1; @@ -158,7 +173,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>( let current_digit = deccum.trunc(); - let c = char::from_digit(current_digit.to_isize().unwrap() as u32, radix); + let c = char::from_digit(current_digit.to_i32() as u32, radix); buf[end] = c.unwrap() as u8; end += 1; diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index f6b3bdcecb9..0178b321e88 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -12,21 +12,16 @@ #![stable(feature = "rust1", since = "1.0.0")] +use prelude::*; + use cell::{Cell, RefCell, Ref, RefMut, BorrowState}; -use char::CharExt; -use clone::Clone; -use iter::Iterator; -use marker::{Copy, PhantomData, Sized}; +use marker::PhantomData; use mem; -use num::Float; -use option::Option; -use option::Option::{Some, None}; -use result::Result::Ok; -use ops::{Deref, FnOnce}; +use ops::Deref; use result; -use slice::SliceExt; +use num::Float; use slice; -use str::{self, StrExt}; +use str; use self::rt::v1::Alignment; pub use self::num::radix; @@ -929,7 +924,8 @@ impl<'a, T> Pointer for &'a mut T { } // Common code of floating point Debug and Display. -fn float_to_str_common<T: Float, F>(num: &T, precision: Option<usize>, post: F) -> Result +fn float_to_str_common<T: float::MyFloat, F>(num: &T, precision: Option<usize>, + post: F) -> Result where F : FnOnce(&str) -> Result { let digits = match precision { Some(i) => float::DigExact(i), @@ -967,8 +963,6 @@ macro_rules! floating { ($ty:ident) => { #[stable(feature = "rust1", since = "1.0.0")] impl LowerExp for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { - use num::Float; - let digits = match fmt.precision { Some(i) => float::DigExact(i), None => float::DigMax(6), @@ -986,8 +980,6 @@ macro_rules! floating { ($ty:ident) => { #[stable(feature = "rust1", since = "1.0.0")] impl UpperExp for $ty { fn fmt(&self, fmt: &mut Formatter) -> Result { - use num::Float; - let digits = match fmt.precision { Some(i) => float::DigExact(i), None => float::DigMax(6), diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index 76c975902aa..122fffc5959 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -14,12 +14,28 @@ #![allow(unsigned_negation)] +use prelude::*; + use fmt; -use iter::Iterator; -use num::{Int, cast}; -use slice::SliceExt; +use num::Zero; +use ops::{Div, Rem, Sub}; use str; +#[doc(hidden)] +trait Int: Zero + PartialEq + PartialOrd + Div<Output=Self> + Rem<Output=Self> + + Sub<Output=Self> + Copy { + fn from_u8(u: u8) -> Self; + fn to_u8(&self) -> u8; +} + +macro_rules! doit { + ($($t:ident)*) => ($(impl Int for $t { + fn from_u8(u: u8) -> $t { u as $t } + fn to_u8(&self) -> u8 { *self as u8 } + })*) +} +doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize } + /// A type that represents a specific radix #[doc(hidden)] trait GenericRadix { @@ -33,33 +49,32 @@ 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. - let zero = Int::zero(); + let zero = T::zero(); let is_positive = x >= zero; let mut buf = [0; 64]; let mut curr = buf.len(); - let base = cast(self.base()).unwrap(); + let base = T::from_u8(self.base()); if is_positive { // Accumulate each digit of the number from the least significant // to the most significant figure. for byte in buf.iter_mut().rev() { - let n = x % base; // Get the current place value. - x = x / base; // Deaccumulate the number. - *byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer. + let n = x % base; // Get the current place value. + x = x / base; // Deaccumulate the number. + *byte = self.digit(n.to_u8()); // Store the digit in the buffer. curr -= 1; - if x == zero { break }; // No more digits left to accumulate. + if x == zero { break }; // No more digits left to accumulate. } } else { // Do the same as above, but accounting for two's complement. for byte in buf.iter_mut().rev() { - let n = zero - (x % base); // Get the current place value. - x = x / base; // Deaccumulate the number. - *byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer. + let n = zero - (x % base); // Get the current place value. + x = x / base; // Deaccumulate the number. + *byte = self.digit(n.to_u8()); // Store the digit in the buffer. curr -= 1; - if x == zero { break }; // No more digits left to accumulate. + if x == zero { break }; // No more digits left to accumulate. } } let buf = unsafe { str::from_utf8_unchecked(&buf[curr..]) }; |
