diff options
| author | bors <bors@rust-lang.org> | 2015-08-22 09:59:07 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-08-22 09:59:07 +0000 |
| commit | 94ee3b5a54a9f4965b82f5e4eda512966e96ac63 (patch) | |
| tree | d404e61ade0e002ee43c690d64e4fb2e2900c694 /src/libcore/num | |
| parent | e9b74a90172e99e361ff46277f52bfc03d0a7ba4 (diff) | |
| parent | a2b932c0b614aa5a68f7d24977fab241998a89ef (diff) | |
| download | rust-94ee3b5a54a9f4965b82f5e4eda512966e96ac63.tar.gz rust-94ee3b5a54a9f4965b82f5e4eda512966e96ac63.zip | |
Auto merge of #27871 - alexcrichton:stabilize-libcore, r=aturon
These commits move libcore into a state so that it's ready for stabilization, performing some minor cleanup: * The primitive modules for integers in the standard library were all removed from the source tree as they were just straight reexports of the libcore variants. * The `core::atomic` module now lives in `core::sync::atomic`. The `core::sync` module is otherwise empty, but ripe for expansion! * The `core::prelude::v1` module was stabilized after auditing that it is a subset of the standard library's prelude plus some primitive extension traits (char, str, and slice) * Some unstable-hacks for float parsing errors were shifted around to not use the same unstable hacks (e.g. the `flt2dec` module is now used for "privacy"). After this commit, the remaining large unstable functionality specific to libcore is: * `raw`, `intrinsics`, `nonzero`, `array`, `panicking`, `simd` -- these modules are all unstable or not reexported in the standard library, so they're just remaining in the same status quo as before * `num::Float` - this extension trait for floats needs to be audited for functionality (much of that is happening in #27823) and may also want to be renamed to `FloatExt` or `F32Ext`/`F64Ext`. * Should the extension traits for primitives be stabilized in libcore? I believe other unstable pieces are not isolated to just libcore but also affect the standard library. cc #27701
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/dec2flt/mod.rs | 100 | ||||
| -rw-r--r-- | src/libcore/num/float_macros.rs | 17 | ||||
| -rw-r--r-- | src/libcore/num/i16.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/i32.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/i64.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/i8.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/isize.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 79 | ||||
| -rw-r--r-- | src/libcore/num/u16.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/u32.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/u64.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/u8.rs | 4 | ||||
| -rw-r--r-- | src/libcore/num/usize.rs | 4 |
13 files changed, 126 insertions, 110 deletions
diff --git a/src/libcore/num/dec2flt/mod.rs b/src/libcore/num/dec2flt/mod.rs index 7c3c384ea93..4e6b6f04e9e 100644 --- a/src/libcore/num/dec2flt/mod.rs +++ b/src/libcore/num/dec2flt/mod.rs @@ -96,8 +96,9 @@ issue = "0")] use prelude::v1::*; -use num::ParseFloatError as PFE; -use num::FloatErrorKind; +use fmt; +use str::FromStr; + use self::parse::{parse_decimal, Decimal, Sign}; use self::parse::ParseResult::{self, Valid, ShortcutToInf, ShortcutToZero}; use self::num::digits_to_big; @@ -110,14 +111,87 @@ mod num; pub mod rawfp; pub mod parse; -/// Entry point for decimal-to-f32 conversion. -pub fn to_f32(s: &str) -> Result<f32, PFE> { - dec2flt(s) +macro_rules! from_str_float_impl { + ($t:ty, $func:ident) => { + #[stable(feature = "rust1", since = "1.0.0")] + impl FromStr for $t { + type Err = ParseFloatError; + + /// Converts a string in base 10 to a float. + /// Accepts an optional decimal exponent. + /// + /// This function accepts strings such as + /// + /// * '3.14' + /// * '-3.14' + /// * '2.5E10', or equivalently, '2.5e10' + /// * '2.5E-10' + /// * '.' (understood as 0) + /// * '5.' + /// * '.5', or, equivalently, '0.5' + /// * 'inf', '-inf', 'NaN' + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * src - A string + /// + /// # Return value + /// + /// `Err(ParseFloatError)` if the string did not represent a valid + /// number. Otherwise, `Ok(n)` where `n` is the floating-point + /// number represented by `src`. + #[inline] + fn from_str(src: &str) -> Result<Self, ParseFloatError> { + dec2flt(src) + } + } + } +} +from_str_float_impl!(f32, to_f32); +from_str_float_impl!(f64, to_f64); + +/// An error which can be returned when parsing a float. +#[derive(Debug, Clone, PartialEq)] +#[stable(feature = "rust1", since = "1.0.0")] +pub struct ParseFloatError { + kind: FloatErrorKind +} + +#[derive(Debug, Clone, PartialEq)] +enum FloatErrorKind { + Empty, + Invalid, +} + +impl ParseFloatError { + #[unstable(feature = "int_error_internals", + reason = "available through Error trait and this method should \ + not be exposed publicly", + issue = "0")] + #[doc(hidden)] + pub fn __description(&self) -> &str { + match self.kind { + FloatErrorKind::Empty => "cannot parse float from empty string", + FloatErrorKind::Invalid => "invalid float literal", + } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Display for ParseFloatError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.__description().fmt(f) + } +} + +pub fn pfe_empty() -> ParseFloatError { + ParseFloatError { kind: FloatErrorKind::Empty } } -/// Entry point for decimal-to-f64 conversion. -pub fn to_f64(s: &str) -> Result<f64, PFE> { - dec2flt(s) +pub fn pfe_invalid() -> ParseFloatError { + ParseFloatError { kind: FloatErrorKind::Invalid } } /// Split decimal string into sign and the rest, without inspecting or validating the rest. @@ -131,9 +205,9 @@ fn extract_sign(s: &str) -> (Sign, &str) { } /// Convert a decimal string into a floating point number. -fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> { +fn dec2flt<T: RawFloat>(s: &str) -> Result<T, ParseFloatError> { if s.is_empty() { - return Err(PFE { __kind: FloatErrorKind::Empty }); + return Err(pfe_empty()) } let (sign, s) = extract_sign(s); let flt = match parse_decimal(s) { @@ -143,7 +217,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> { ParseResult::Invalid => match s { "inf" => T::infinity(), "NaN" => T::nan(), - _ => { return Err(PFE { __kind: FloatErrorKind::Invalid }); } + _ => { return Err(pfe_invalid()); } } }; @@ -155,7 +229,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> { /// The main workhorse for the decimal-to-float conversion: Orchestrate all the preprocessing /// and figure out which algorithm should do the actual conversion. -fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, PFE> { +fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, ParseFloatError> { simplify(&mut decimal); if let Some(x) = trivial_cases(&decimal) { return Ok(x); @@ -172,7 +246,7 @@ fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, PFE> { // If we exceed this, perhaps while calculating `f * 10^e` in Algorithm R or Algorithm M, // we'll crash. So we error out before getting too close, with a generous safety margin. if max_digits > 375 { - return Err(PFE { __kind: FloatErrorKind::Invalid }); + return Err(pfe_invalid()); } let f = digits_to_big(decimal.integral, decimal.fractional); diff --git a/src/libcore/num/float_macros.rs b/src/libcore/num/float_macros.rs index e3fa7047ec8..88c3b756793 100644 --- a/src/libcore/num/float_macros.rs +++ b/src/libcore/num/float_macros.rs @@ -23,8 +23,7 @@ macro_rules! from_str_radix_float_impl { ($T:ty) => { fn from_str_radix(src: &str, radix: u32) -> Result<$T, ParseFloatError> { - use num::FloatErrorKind::*; - use num::ParseFloatError as PFE; + use num::dec2flt::{pfe_empty, pfe_invalid}; // Special values match src { @@ -35,8 +34,8 @@ macro_rules! from_str_radix_float_impl { } let (is_positive, src) = match src.slice_shift_char() { - None => return Err(PFE { __kind: Empty }), - Some(('-', "")) => return Err(PFE { __kind: Empty }), + None => return Err(pfe_empty()), + Some(('-', "")) => return Err(pfe_empty()), Some(('-', src)) => (false, src), Some((_, _)) => (true, src), }; @@ -88,7 +87,7 @@ macro_rules! from_str_radix_float_impl { break; // start of fractional part }, _ => { - return Err(PFE { __kind: Invalid }); + return Err(pfe_invalid()) }, }, } @@ -122,7 +121,7 @@ macro_rules! from_str_radix_float_impl { break; // start of exponent }, _ => { - return Err(PFE { __kind: Invalid }); + return Err(pfe_invalid()) }, }, } @@ -135,7 +134,7 @@ macro_rules! from_str_radix_float_impl { let base = match c { 'E' | 'e' if radix == 10 => 10.0, 'P' | 'p' if radix == 16 => 2.0, - _ => return Err(PFE { __kind: Invalid }), + _ => return Err(pfe_invalid()), }; // Parse the exponent as decimal integer @@ -144,13 +143,13 @@ macro_rules! from_str_radix_float_impl { Some(('-', src)) => (false, src.parse::<usize>()), Some(('+', src)) => (true, src.parse::<usize>()), Some((_, _)) => (true, src.parse::<usize>()), - None => return Err(PFE { __kind: Invalid }), + None => return Err(pfe_invalid()), }; match (is_positive, exp) { (true, Ok(exp)) => base.powi(exp as i32), (false, Ok(exp)) => 1.0 / base.powi(exp as i32), - (_, Err(_)) => return Err(PFE { __kind: Invalid }), + (_, Err(_)) => return Err(pfe_invalid()), } }, None => 1.0, // no exponent diff --git a/src/libcore/num/i16.rs b/src/libcore/num/i16.rs index dacb4ebcdfa..40544979417 100644 --- a/src/libcore/num/i16.rs +++ b/src/libcore/num/i16.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for signed 16-bits integers (`i16` type) +//! The 16-bit signed integer type. +//! +//! *[See also the `i16` primitive type](../primitive.i16.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/i32.rs b/src/libcore/num/i32.rs index 250d66de70b..5d2ade8d8e0 100644 --- a/src/libcore/num/i32.rs +++ b/src/libcore/num/i32.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for signed 32-bits integers (`i32` type) +//! The 32-bit signed integer type. +//! +//! *[See also the `i32` primitive type](../primitive.i32.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/i64.rs b/src/libcore/num/i64.rs index 5ed21d7246c..b1d43a3b838 100644 --- a/src/libcore/num/i64.rs +++ b/src/libcore/num/i64.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for signed 64-bits integers (`i64` type) +//! The 64-bit signed integer type. +//! +//! *[See also the `i64` primitive type](../primitive.i64.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/i8.rs b/src/libcore/num/i8.rs index 0394c12d5c4..ee003d92b28 100644 --- a/src/libcore/num/i8.rs +++ b/src/libcore/num/i8.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for signed 8-bits integers (`i8` type) +//! The 8-bit signed integer type. +//! +//! *[See also the `i8` primitive type](../primitive.i8.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/isize.rs b/src/libcore/num/isize.rs index 066cb10cce2..034a5c0eb89 100644 --- a/src/libcore/num/isize.rs +++ b/src/libcore/num/isize.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for pointer-sized signed integers (`isize` type) +//! The pointer-sized signed integer type. +//! +//! *[See also the `isize` primitive type](../primitive.isize.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 23432a2044c..05c7e8b8de4 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1327,47 +1327,6 @@ pub trait Float: Sized { fn to_radians(self) -> Self; } -macro_rules! from_str_float_impl { - ($t:ty, $func:ident) => { - #[stable(feature = "rust1", since = "1.0.0")] - impl FromStr for $t { - type Err = ParseFloatError; - - /// Converts a string in base 10 to a float. - /// Accepts an optional decimal exponent. - /// - /// This function accepts strings such as - /// - /// * '3.14' - /// * '-3.14' - /// * '2.5E10', or equivalently, '2.5e10' - /// * '2.5E-10' - /// * '.' (understood as 0) - /// * '5.' - /// * '.5', or, equivalently, '0.5' - /// * 'inf', '-inf', 'NaN' - /// - /// Leading and trailing whitespace represent an error. - /// - /// # Arguments - /// - /// * src - A string - /// - /// # Return value - /// - /// `Err(ParseFloatError)` if the string did not represent a valid - /// number. Otherwise, `Ok(n)` where `n` is the floating-point - /// number represented by `src`. - #[inline] - fn from_str(src: &str) -> Result<Self, ParseFloatError> { - dec2flt::$func(src) - } - } - } -} -from_str_float_impl!(f32, to_f32); -from_str_float_impl!(f64, to_f64); - macro_rules! from_str_radix_int_impl { ($($t:ty)*) => {$( #[stable(feature = "rust1", since = "1.0.0")] @@ -1510,40 +1469,4 @@ impl fmt::Display for ParseIntError { } } -/// An error which can be returned when parsing a float. -#[derive(Debug, Clone, PartialEq)] -#[stable(feature = "rust1", since = "1.0.0")] -pub struct ParseFloatError { - #[doc(hidden)] - #[unstable(feature = "float_error_internals", - reason = "should not be exposed publicly", - issue = "0")] - pub __kind: FloatErrorKind -} - -#[derive(Debug, Clone, PartialEq)] -#[unstable(feature = "float_error_internals", - reason = "should not be exposed publicly", - issue = "0")] -#[doc(hidden)] -pub enum FloatErrorKind { - Empty, - Invalid, -} - -impl ParseFloatError { - #[doc(hidden)] - pub fn __description(&self) -> &str { - match self.__kind { - FloatErrorKind::Empty => "cannot parse float from empty string", - FloatErrorKind::Invalid => "invalid float literal", - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Display for ParseFloatError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.__description().fmt(f) - } -} +pub use num::dec2flt::ParseFloatError; diff --git a/src/libcore/num/u16.rs b/src/libcore/num/u16.rs index ecf79944848..68e50e8a400 100644 --- a/src/libcore/num/u16.rs +++ b/src/libcore/num/u16.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for unsigned 16-bits integers (`u16` type) +//! The 16-bit unsigned integer type. +//! +//! *[See also the `u16` primitive type](../primitive.u16.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/u32.rs b/src/libcore/num/u32.rs index b0682b55ac0..c1ee96b363c 100644 --- a/src/libcore/num/u32.rs +++ b/src/libcore/num/u32.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for unsigned 32-bits integers (`u32` type) +//! The 32-bit unsigned integer type. +//! +//! *[See also the `u32` primitive type](../primitive.u32.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/u64.rs b/src/libcore/num/u64.rs index dbc6a64a905..c0d18d850a7 100644 --- a/src/libcore/num/u64.rs +++ b/src/libcore/num/u64.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for unsigned 64-bits integer (`u64` type) +//! The 64-bit unsigned integer type. +//! +//! *[See also the `u64` primitive type](../primitive.u64.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/u8.rs b/src/libcore/num/u8.rs index bf9347ca62c..a60c480d810 100644 --- a/src/libcore/num/u8.rs +++ b/src/libcore/num/u8.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for unsigned 8-bits integers (`u8` type) +//! The 8-bit unsigned integer type. +//! +//! *[See also the `u8` primitive type](../primitive.u8.html).* #![stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/num/usize.rs b/src/libcore/num/usize.rs index 67e3c954ab6..70e790106e1 100644 --- a/src/libcore/num/usize.rs +++ b/src/libcore/num/usize.rs @@ -8,7 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Operations and constants for pointer-sized unsigned integers (`usize` type) +//! The pointer-sized unsigned integer type. +//! +//! *[See also the `usize` primitive type](../primitive.usize.html).* #![stable(feature = "rust1", since = "1.0.0")] |
