Skip to content

Commit a2b932c

Browse files
committed
core: Shuffle around float parsing
Stop using stability to hide the implementation details of ParseFloatError and instead move the error type into the `dec2flt` module. Also move the implementation blocks of `FromStr for f{32,64}` into `dec2flt` directly.
1 parent 5990249 commit a2b932c

File tree

9 files changed

+133
-138
lines changed

9 files changed

+133
-138
lines changed

src/libcore/num/dec2flt/mod.rs

+87-13
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@
9696
issue = "0")]
9797

9898
use prelude::v1::*;
99-
use num::ParseFloatError as PFE;
100-
use num::FloatErrorKind;
99+
use fmt;
100+
use str::FromStr;
101+
101102
use self::parse::{parse_decimal, Decimal, Sign};
102103
use self::parse::ParseResult::{self, Valid, ShortcutToInf, ShortcutToZero};
103104
use self::num::digits_to_big;
@@ -110,14 +111,87 @@ mod num;
110111
pub mod rawfp;
111112
pub mod parse;
112113

113-
/// Entry point for decimal-to-f32 conversion.
114-
pub fn to_f32(s: &str) -> Result<f32, PFE> {
115-
dec2flt(s)
114+
macro_rules! from_str_float_impl {
115+
($t:ty, $func:ident) => {
116+
#[stable(feature = "rust1", since = "1.0.0")]
117+
impl FromStr for $t {
118+
type Err = ParseFloatError;
119+
120+
/// Converts a string in base 10 to a float.
121+
/// Accepts an optional decimal exponent.
122+
///
123+
/// This function accepts strings such as
124+
///
125+
/// * '3.14'
126+
/// * '-3.14'
127+
/// * '2.5E10', or equivalently, '2.5e10'
128+
/// * '2.5E-10'
129+
/// * '.' (understood as 0)
130+
/// * '5.'
131+
/// * '.5', or, equivalently, '0.5'
132+
/// * 'inf', '-inf', 'NaN'
133+
///
134+
/// Leading and trailing whitespace represent an error.
135+
///
136+
/// # Arguments
137+
///
138+
/// * src - A string
139+
///
140+
/// # Return value
141+
///
142+
/// `Err(ParseFloatError)` if the string did not represent a valid
143+
/// number. Otherwise, `Ok(n)` where `n` is the floating-point
144+
/// number represented by `src`.
145+
#[inline]
146+
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
147+
dec2flt(src)
148+
}
149+
}
150+
}
151+
}
152+
from_str_float_impl!(f32, to_f32);
153+
from_str_float_impl!(f64, to_f64);
154+
155+
/// An error which can be returned when parsing a float.
156+
#[derive(Debug, Clone, PartialEq)]
157+
#[stable(feature = "rust1", since = "1.0.0")]
158+
pub struct ParseFloatError {
159+
kind: FloatErrorKind
160+
}
161+
162+
#[derive(Debug, Clone, PartialEq)]
163+
enum FloatErrorKind {
164+
Empty,
165+
Invalid,
166+
}
167+
168+
impl ParseFloatError {
169+
#[unstable(feature = "int_error_internals",
170+
reason = "available through Error trait and this method should \
171+
not be exposed publicly",
172+
issue = "0")]
173+
#[doc(hidden)]
174+
pub fn __description(&self) -> &str {
175+
match self.kind {
176+
FloatErrorKind::Empty => "cannot parse float from empty string",
177+
FloatErrorKind::Invalid => "invalid float literal",
178+
}
179+
}
180+
}
181+
182+
#[stable(feature = "rust1", since = "1.0.0")]
183+
impl fmt::Display for ParseFloatError {
184+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185+
self.__description().fmt(f)
186+
}
187+
}
188+
189+
pub fn pfe_empty() -> ParseFloatError {
190+
ParseFloatError { kind: FloatErrorKind::Empty }
116191
}
117192

118-
/// Entry point for decimal-to-f64 conversion.
119-
pub fn to_f64(s: &str) -> Result<f64, PFE> {
120-
dec2flt(s)
193+
pub fn pfe_invalid() -> ParseFloatError {
194+
ParseFloatError { kind: FloatErrorKind::Invalid }
121195
}
122196

123197
/// 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) {
131205
}
132206

133207
/// Convert a decimal string into a floating point number.
134-
fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> {
208+
fn dec2flt<T: RawFloat>(s: &str) -> Result<T, ParseFloatError> {
135209
if s.is_empty() {
136-
return Err(PFE { __kind: FloatErrorKind::Empty });
210+
return Err(pfe_empty())
137211
}
138212
let (sign, s) = extract_sign(s);
139213
let flt = match parse_decimal(s) {
@@ -143,7 +217,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> {
143217
ParseResult::Invalid => match s {
144218
"inf" => T::infinity(),
145219
"NaN" => T::nan(),
146-
_ => { return Err(PFE { __kind: FloatErrorKind::Invalid }); }
220+
_ => { return Err(pfe_invalid()); }
147221
}
148222
};
149223

@@ -155,7 +229,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> {
155229

156230
/// The main workhorse for the decimal-to-float conversion: Orchestrate all the preprocessing
157231
/// and figure out which algorithm should do the actual conversion.
158-
fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, PFE> {
232+
fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, ParseFloatError> {
159233
simplify(&mut decimal);
160234
if let Some(x) = trivial_cases(&decimal) {
161235
return Ok(x);
@@ -172,7 +246,7 @@ fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, PFE> {
172246
// If we exceed this, perhaps while calculating `f * 10^e` in Algorithm R or Algorithm M,
173247
// we'll crash. So we error out before getting too close, with a generous safety margin.
174248
if max_digits > 375 {
175-
return Err(PFE { __kind: FloatErrorKind::Invalid });
249+
return Err(pfe_invalid());
176250
}
177251
let f = digits_to_big(decimal.integral, decimal.fractional);
178252

src/libcore/num/float_macros.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ macro_rules! from_str_radix_float_impl {
2323
($T:ty) => {
2424
fn from_str_radix(src: &str, radix: u32)
2525
-> Result<$T, ParseFloatError> {
26-
use num::FloatErrorKind::*;
27-
use num::ParseFloatError as PFE;
26+
use num::dec2flt::{pfe_empty, pfe_invalid};
2827

2928
// Special values
3029
match src {
@@ -35,8 +34,8 @@ macro_rules! from_str_radix_float_impl {
3534
}
3635

3736
let (is_positive, src) = match src.slice_shift_char() {
38-
None => return Err(PFE { __kind: Empty }),
39-
Some(('-', "")) => return Err(PFE { __kind: Empty }),
37+
None => return Err(pfe_empty()),
38+
Some(('-', "")) => return Err(pfe_empty()),
4039
Some(('-', src)) => (false, src),
4140
Some((_, _)) => (true, src),
4241
};
@@ -88,7 +87,7 @@ macro_rules! from_str_radix_float_impl {
8887
break; // start of fractional part
8988
},
9089
_ => {
91-
return Err(PFE { __kind: Invalid });
90+
return Err(pfe_invalid())
9291
},
9392
},
9493
}
@@ -122,7 +121,7 @@ macro_rules! from_str_radix_float_impl {
122121
break; // start of exponent
123122
},
124123
_ => {
125-
return Err(PFE { __kind: Invalid });
124+
return Err(pfe_invalid())
126125
},
127126
},
128127
}
@@ -135,7 +134,7 @@ macro_rules! from_str_radix_float_impl {
135134
let base = match c {
136135
'E' | 'e' if radix == 10 => 10.0,
137136
'P' | 'p' if radix == 16 => 2.0,
138-
_ => return Err(PFE { __kind: Invalid }),
137+
_ => return Err(pfe_invalid()),
139138
};
140139

141140
// Parse the exponent as decimal integer
@@ -144,13 +143,13 @@ macro_rules! from_str_radix_float_impl {
144143
Some(('-', src)) => (false, src.parse::<usize>()),
145144
Some(('+', src)) => (true, src.parse::<usize>()),
146145
Some((_, _)) => (true, src.parse::<usize>()),
147-
None => return Err(PFE { __kind: Invalid }),
146+
None => return Err(pfe_invalid()),
148147
};
149148

150149
match (is_positive, exp) {
151150
(true, Ok(exp)) => base.powi(exp as i32),
152151
(false, Ok(exp)) => 1.0 / base.powi(exp as i32),
153-
(_, Err(_)) => return Err(PFE { __kind: Invalid }),
152+
(_, Err(_)) => return Err(pfe_invalid()),
154153
}
155154
},
156155
None => 1.0, // no exponent

src/libcore/num/mod.rs

+1-78
Original file line numberDiff line numberDiff line change
@@ -1365,47 +1365,6 @@ pub trait Float: Sized {
13651365
fn to_radians(self) -> Self;
13661366
}
13671367

1368-
macro_rules! from_str_float_impl {
1369-
($t:ty, $func:ident) => {
1370-
#[stable(feature = "rust1", since = "1.0.0")]
1371-
impl FromStr for $t {
1372-
type Err = ParseFloatError;
1373-
1374-
/// Converts a string in base 10 to a float.
1375-
/// Accepts an optional decimal exponent.
1376-
///
1377-
/// This function accepts strings such as
1378-
///
1379-
/// * '3.14'
1380-
/// * '-3.14'
1381-
/// * '2.5E10', or equivalently, '2.5e10'
1382-
/// * '2.5E-10'
1383-
/// * '.' (understood as 0)
1384-
/// * '5.'
1385-
/// * '.5', or, equivalently, '0.5'
1386-
/// * 'inf', '-inf', 'NaN'
1387-
///
1388-
/// Leading and trailing whitespace represent an error.
1389-
///
1390-
/// # Arguments
1391-
///
1392-
/// * src - A string
1393-
///
1394-
/// # Return value
1395-
///
1396-
/// `Err(ParseFloatError)` if the string did not represent a valid
1397-
/// number. Otherwise, `Ok(n)` where `n` is the floating-point
1398-
/// number represented by `src`.
1399-
#[inline]
1400-
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
1401-
dec2flt::$func(src)
1402-
}
1403-
}
1404-
}
1405-
}
1406-
from_str_float_impl!(f32, to_f32);
1407-
from_str_float_impl!(f64, to_f64);
1408-
14091368
macro_rules! from_str_radix_int_impl {
14101369
($($t:ty)*) => {$(
14111370
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1548,40 +1507,4 @@ impl fmt::Display for ParseIntError {
15481507
}
15491508
}
15501509

1551-
/// An error which can be returned when parsing a float.
1552-
#[derive(Debug, Clone, PartialEq)]
1553-
#[stable(feature = "rust1", since = "1.0.0")]
1554-
pub struct ParseFloatError {
1555-
#[doc(hidden)]
1556-
#[unstable(feature = "float_error_internals",
1557-
reason = "should not be exposed publicly",
1558-
issue = "0")]
1559-
pub __kind: FloatErrorKind
1560-
}
1561-
1562-
#[derive(Debug, Clone, PartialEq)]
1563-
#[unstable(feature = "float_error_internals",
1564-
reason = "should not be exposed publicly",
1565-
issue = "0")]
1566-
#[doc(hidden)]
1567-
pub enum FloatErrorKind {
1568-
Empty,
1569-
Invalid,
1570-
}
1571-
1572-
impl ParseFloatError {
1573-
#[doc(hidden)]
1574-
pub fn __description(&self) -> &str {
1575-
match self.__kind {
1576-
FloatErrorKind::Empty => "cannot parse float from empty string",
1577-
FloatErrorKind::Invalid => "invalid float literal",
1578-
}
1579-
}
1580-
}
1581-
1582-
#[stable(feature = "rust1", since = "1.0.0")]
1583-
impl fmt::Display for ParseFloatError {
1584-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1585-
self.__description().fmt(f)
1586-
}
1587-
}
1510+
pub use num::dec2flt::ParseFloatError;

src/libcoretest/atomic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use core::atomic::*;
12-
use core::atomic::Ordering::SeqCst;
11+
use core::sync::atomic::*;
12+
use core::sync::atomic::Ordering::SeqCst;
1313

1414
#[test]
1515
fn bool_() {

0 commit comments

Comments
 (0)