Skip to content

Commit 94ee3b5

Browse files
committed
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
2 parents e9b74a9 + a2b932c commit 94ee3b5

40 files changed

+244
-461
lines changed

src/liballoc/arc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
7272
use boxed::Box;
7373

74-
use core::atomic;
75-
use core::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
74+
use core::sync::atomic;
75+
use core::sync::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
7676
use core::fmt;
7777
use core::cmp::Ordering;
7878
use core::mem::{align_of_val, size_of_val};

src/libcore/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub mod convert;
144144

145145
pub mod any;
146146
pub mod array;
147-
pub mod atomic;
147+
pub mod sync;
148148
pub mod cell;
149149
pub mod char;
150150
pub mod panicking;

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/i16.rs

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

11-
//! Operations and constants for signed 16-bits integers (`i16` type)
11+
//! The 16-bit signed integer type.
12+
//!
13+
//! *[See also the `i16` primitive type](../primitive.i16.html).*
1214
1315
#![stable(feature = "rust1", since = "1.0.0")]
1416

src/libcore/num/i32.rs

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

11-
//! Operations and constants for signed 32-bits integers (`i32` type)
11+
//! The 32-bit signed integer type.
12+
//!
13+
//! *[See also the `i32` primitive type](../primitive.i32.html).*
1214
1315
#![stable(feature = "rust1", since = "1.0.0")]
1416

src/libcore/num/i64.rs

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

11-
//! Operations and constants for signed 64-bits integers (`i64` type)
11+
//! The 64-bit signed integer type.
12+
//!
13+
//! *[See also the `i64` primitive type](../primitive.i64.html).*
1214
1315
#![stable(feature = "rust1", since = "1.0.0")]
1416

src/libcore/num/i8.rs

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

11-
//! Operations and constants for signed 8-bits integers (`i8` type)
11+
//! The 8-bit signed integer type.
12+
//!
13+
//! *[See also the `i8` primitive type](../primitive.i8.html).*
1214
1315
#![stable(feature = "rust1", since = "1.0.0")]
1416

src/libcore/num/isize.rs

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

11-
//! Operations and constants for pointer-sized signed integers (`isize` type)
11+
//! The pointer-sized signed integer type.
12+
//!
13+
//! *[See also the `isize` primitive type](../primitive.isize.html).*
1214
1315
#![stable(feature = "rust1", since = "1.0.0")]
1416

src/libcore/num/mod.rs

+1-78
Original file line numberDiff line numberDiff line change
@@ -1327,47 +1327,6 @@ pub trait Float: Sized {
13271327
fn to_radians(self) -> Self;
13281328
}
13291329

1330-
macro_rules! from_str_float_impl {
1331-
($t:ty, $func:ident) => {
1332-
#[stable(feature = "rust1", since = "1.0.0")]
1333-
impl FromStr for $t {
1334-
type Err = ParseFloatError;
1335-
1336-
/// Converts a string in base 10 to a float.
1337-
/// Accepts an optional decimal exponent.
1338-
///
1339-
/// This function accepts strings such as
1340-
///
1341-
/// * '3.14'
1342-
/// * '-3.14'
1343-
/// * '2.5E10', or equivalently, '2.5e10'
1344-
/// * '2.5E-10'
1345-
/// * '.' (understood as 0)
1346-
/// * '5.'
1347-
/// * '.5', or, equivalently, '0.5'
1348-
/// * 'inf', '-inf', 'NaN'
1349-
///
1350-
/// Leading and trailing whitespace represent an error.
1351-
///
1352-
/// # Arguments
1353-
///
1354-
/// * src - A string
1355-
///
1356-
/// # Return value
1357-
///
1358-
/// `Err(ParseFloatError)` if the string did not represent a valid
1359-
/// number. Otherwise, `Ok(n)` where `n` is the floating-point
1360-
/// number represented by `src`.
1361-
#[inline]
1362-
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
1363-
dec2flt::$func(src)
1364-
}
1365-
}
1366-
}
1367-
}
1368-
from_str_float_impl!(f32, to_f32);
1369-
from_str_float_impl!(f64, to_f64);
1370-
13711330
macro_rules! from_str_radix_int_impl {
13721331
($($t:ty)*) => {$(
13731332
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1510,40 +1469,4 @@ impl fmt::Display for ParseIntError {
15101469
}
15111470
}
15121471

1513-
/// An error which can be returned when parsing a float.
1514-
#[derive(Debug, Clone, PartialEq)]
1515-
#[stable(feature = "rust1", since = "1.0.0")]
1516-
pub struct ParseFloatError {
1517-
#[doc(hidden)]
1518-
#[unstable(feature = "float_error_internals",
1519-
reason = "should not be exposed publicly",
1520-
issue = "0")]
1521-
pub __kind: FloatErrorKind
1522-
}
1523-
1524-
#[derive(Debug, Clone, PartialEq)]
1525-
#[unstable(feature = "float_error_internals",
1526-
reason = "should not be exposed publicly",
1527-
issue = "0")]
1528-
#[doc(hidden)]
1529-
pub enum FloatErrorKind {
1530-
Empty,
1531-
Invalid,
1532-
}
1533-
1534-
impl ParseFloatError {
1535-
#[doc(hidden)]
1536-
pub fn __description(&self) -> &str {
1537-
match self.__kind {
1538-
FloatErrorKind::Empty => "cannot parse float from empty string",
1539-
FloatErrorKind::Invalid => "invalid float literal",
1540-
}
1541-
}
1542-
}
1543-
1544-
#[stable(feature = "rust1", since = "1.0.0")]
1545-
impl fmt::Display for ParseFloatError {
1546-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1547-
self.__description().fmt(f)
1548-
}
1549-
}
1472+
pub use num::dec2flt::ParseFloatError;

src/libcore/num/u16.rs

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

11-
//! Operations and constants for unsigned 16-bits integers (`u16` type)
11+
//! The 16-bit unsigned integer type.
12+
//!
13+
//! *[See also the `u16` primitive type](../primitive.u16.html).*
1214
1315
#![stable(feature = "rust1", since = "1.0.0")]
1416

src/libcore/num/u32.rs

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

11-
//! Operations and constants for unsigned 32-bits integers (`u32` type)
11+
//! The 32-bit unsigned integer type.
12+
//!
13+
//! *[See also the `u32` primitive type](../primitive.u32.html).*
1214
1315
#![stable(feature = "rust1", since = "1.0.0")]
1416

0 commit comments

Comments
 (0)