Skip to content

Commit

Permalink
std: Remove deprecated/unstable num functionality
Browse files Browse the repository at this point in the history
This commit removes all the old casting/generic traits from `std::num` that are
no longer in use by the standard library. This additionally removes the old
`strconv` module which has not seen much use in quite a long time. All generic
functionality has been supplanted with traits in the `num` crate and the
`strconv` module is supplanted with the [rust-strconv crate][rust-strconv].

[rust-strconv]: https://github.com/lifthrasiir/rust-strconv

This is a breaking change due to the removal of these deprecated crates, and the
alternative crates are listed above.

[breaking-change]
  • Loading branch information
alexcrichton committed Apr 21, 2015
1 parent e091ba3 commit eeb9488
Show file tree
Hide file tree
Showing 54 changed files with 623 additions and 5,599 deletions.
8 changes: 0 additions & 8 deletions src/libcollections/string.rs
Expand Up @@ -1058,14 +1058,6 @@ impl<'a> IntoCow<'a, str> for &'a str {
}
}

#[allow(deprecated)]
impl<'a> Str for Cow<'a, str> {
#[inline]
fn as_slice<'b>(&'b self) -> &'b str {
&**self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Write for String {
#[inline]
Expand Down
43 changes: 29 additions & 14 deletions src/libcore/fmt/float.rs
Expand Up @@ -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 {
Expand All @@ -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`
Expand All @@ -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,
Expand All @@ -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");

Expand All @@ -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)
};
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
24 changes: 8 additions & 16 deletions src/libcore/fmt/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -912,7 +907,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),
Expand Down Expand Up @@ -950,8 +946,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),
Expand All @@ -969,8 +963,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),
Expand Down
43 changes: 29 additions & 14 deletions src/libcore/fmt/num.rs
Expand Up @@ -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 {
Expand All @@ -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..]) };
Expand Down
76 changes: 1 addition & 75 deletions src/libcore/iter.rs
Expand Up @@ -64,7 +64,7 @@ use cmp::{Ord, PartialOrd, PartialEq};
use default::Default;
use marker;
use mem;
use num::{Int, Zero, One};
use num::{Zero, One};
use ops::{self, Add, Sub, FnMut, Mul, RangeFrom};
use option::Option::{self, Some, None};
use marker::Sized;
Expand Down Expand Up @@ -2647,80 +2647,6 @@ impl<A: Step + Zero + Clone> Iterator for StepBy<A, ops::Range<A>> {
}
}

/// An iterator over the range [start, stop] by `step`. It handles overflow by stopping.
#[derive(Clone)]
#[unstable(feature = "core",
reason = "likely to be replaced by range notation and adapters")]
pub struct RangeStepInclusive<A> {
state: A,
stop: A,
step: A,
rev: bool,
done: bool,
}

/// Returns an iterator over the range [start, stop] by `step`.
///
/// It handles overflow by stopping.
///
/// # Examples
///
/// ```
/// # #![feature(core)]
/// use std::iter::range_step_inclusive;
///
/// for i in range_step_inclusive(0, 10, 2) {
/// println!("{}", i);
/// }
/// ```
///
/// This prints:
///
/// ```text
/// 0
/// 2
/// 4
/// 6
/// 8
/// 10
/// ```
#[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 {
state: start,
stop: stop,
step: step,
rev: rev,
done: false,
}
}

#[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;

#[inline]
fn next(&mut self) -> Option<A> {
if !self.done && ((self.rev && self.state >= self.stop) ||
(!self.rev && self.state <= self.stop)) {
let result = self.state;
match self.state.checked_add(self.step) {
Some(x) => self.state = x,
None => self.done = true
}
Some(result)
} else {
None
}
}
}

macro_rules! range_exact_iter_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit eeb9488

Please sign in to comment.