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
This commit is contained in:
@@ -144,7 +144,7 @@ pub mod convert;
|
||||
|
||||
pub mod any;
|
||||
pub mod array;
|
||||
pub mod atomic;
|
||||
pub mod sync;
|
||||
pub mod cell;
|
||||
pub mod char;
|
||||
pub mod panicking;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
/// Entry point for decimal-to-f64 conversion.
|
||||
pub fn to_f64(s: &str) -> Result<f64, PFE> {
|
||||
dec2flt(s)
|
||||
#[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 }
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
|
||||
@@ -10,4 +10,6 @@
|
||||
|
||||
//! The libcore prelude
|
||||
|
||||
#![stable(feature = "core_prelude", since = "1.4.0")]
|
||||
|
||||
pub mod v1;
|
||||
|
||||
@@ -14,27 +14,26 @@
|
||||
//! well. This module is imported by default when `#![no_std]` is used in the
|
||||
//! same manner as the standard library's prelude.
|
||||
|
||||
#![unstable(feature = "core_prelude",
|
||||
reason = "the libcore prelude has not been scrutinized and \
|
||||
stabilized yet",
|
||||
issue = "27701")]
|
||||
#![stable(feature = "core_prelude", since = "1.4.0")]
|
||||
|
||||
// Reexported core operators
|
||||
pub use marker::{Copy, Send, Sized, Sync};
|
||||
pub use ops::{Drop, Fn, FnMut, FnOnce};
|
||||
#[doc(no_inline)] pub use marker::{Copy, Send, Sized, Sync};
|
||||
#[doc(no_inline)] pub use ops::{Drop, Fn, FnMut, FnOnce};
|
||||
|
||||
// Reexported functions
|
||||
pub use mem::drop;
|
||||
#[doc(no_inline)] pub use mem::drop;
|
||||
|
||||
// Reexported types and traits
|
||||
pub use char::CharExt;
|
||||
pub use clone::Clone;
|
||||
pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
|
||||
pub use convert::{AsRef, AsMut, Into, From};
|
||||
pub use default::Default;
|
||||
pub use iter::IntoIterator;
|
||||
pub use iter::{Iterator, DoubleEndedIterator, Extend, ExactSizeIterator};
|
||||
pub use option::Option::{self, Some, None};
|
||||
pub use result::Result::{self, Ok, Err};
|
||||
pub use slice::SliceExt;
|
||||
pub use str::StrExt;
|
||||
#[doc(no_inline)] pub use clone::Clone;
|
||||
#[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
|
||||
#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From};
|
||||
#[doc(no_inline)] pub use default::Default;
|
||||
#[doc(no_inline)] pub use iter::{Iterator, Extend, IntoIterator};
|
||||
#[doc(no_inline)] pub use iter::{DoubleEndedIterator, ExactSizeIterator};
|
||||
#[doc(no_inline)] pub use option::Option::{self, Some, None};
|
||||
#[doc(no_inline)] pub use result::Result::{self, Ok, Err};
|
||||
|
||||
// Reexported extension traits for primitive types
|
||||
#[doc(no_inline)] pub use slice::SliceExt;
|
||||
#[doc(no_inline)] pub use str::StrExt;
|
||||
#[doc(no_inline)] pub use char::CharExt;
|
||||
|
||||
15
src/libcore/sync/mod.rs
Normal file
15
src/libcore/sync/mod.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! Synchronization primitives
|
||||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
pub mod atomic;
|
||||
Reference in New Issue
Block a user