Skip to content

Split core/num/mod.rs to smaller mods #76327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,34 +169,34 @@ mod macros;
#[macro_use]
mod internal_macros;

#[path = "num/int_macros.rs"]
#[path = "num/shells/int_macros.rs"]
#[macro_use]
mod int_macros;

#[path = "num/i128.rs"]
#[path = "num/shells/i128.rs"]
pub mod i128;
#[path = "num/i16.rs"]
#[path = "num/shells/i16.rs"]
pub mod i16;
#[path = "num/i32.rs"]
#[path = "num/shells/i32.rs"]
pub mod i32;
#[path = "num/i64.rs"]
#[path = "num/shells/i64.rs"]
pub mod i64;
#[path = "num/i8.rs"]
#[path = "num/shells/i8.rs"]
pub mod i8;
#[path = "num/isize.rs"]
#[path = "num/shells/isize.rs"]
pub mod isize;

#[path = "num/u128.rs"]
#[path = "num/shells/u128.rs"]
pub mod u128;
#[path = "num/u16.rs"]
#[path = "num/shells/u16.rs"]
pub mod u16;
#[path = "num/u32.rs"]
#[path = "num/shells/u32.rs"]
pub mod u32;
#[path = "num/u64.rs"]
#[path = "num/shells/u64.rs"]
pub mod u64;
#[path = "num/u8.rs"]
#[path = "num/shells/u8.rs"]
pub mod u8;
#[path = "num/usize.rs"]
#[path = "num/shells/usize.rs"]
pub mod usize;

#[path = "num/f32.rs"]
Expand Down
151 changes: 151 additions & 0 deletions library/core/src/num/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//! Error types for conversion to integral types.

use crate::convert::Infallible;
use crate::fmt;

/// The error type returned when a checked integral type conversion fails.
#[stable(feature = "try_from", since = "1.34.0")]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TryFromIntError(pub(crate) ());

impl TryFromIntError {
#[unstable(
feature = "int_error_internals",
reason = "available through Error trait and this method should \
not be exposed publicly",
issue = "none"
)]
#[doc(hidden)]
pub fn __description(&self) -> &str {
"out of range integral type conversion attempted"
}
}

#[stable(feature = "try_from", since = "1.34.0")]
impl fmt::Display for TryFromIntError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
self.__description().fmt(fmt)
}
}

#[stable(feature = "try_from", since = "1.34.0")]
impl From<Infallible> for TryFromIntError {
fn from(x: Infallible) -> TryFromIntError {
match x {}
}
}

#[unstable(feature = "never_type", issue = "35121")]
impl From<!> for TryFromIntError {
fn from(never: !) -> TryFromIntError {
// Match rather than coerce to make sure that code like
// `From<Infallible> for TryFromIntError` above will keep working
// when `Infallible` becomes an alias to `!`.
match never {}
}
}

/// An error which can be returned when parsing an integer.
///
/// This error is used as the error type for the `from_str_radix()` functions
/// on the primitive integer types, such as [`i8::from_str_radix`].
///
/// # Potential causes
///
/// Among other causes, `ParseIntError` can be thrown because of leading or trailing whitespace
/// in the string e.g., when it is obtained from the standard input.
/// Using the [`str.trim()`] method ensures that no whitespace remains before parsing.
///
/// [`str.trim()`]: ../../std/primitive.str.html#method.trim
/// [`i8::from_str_radix`]: ../../std/primitive.i8.html#method.from_str_radix
///
/// # Example
///
/// ```
/// if let Err(e) = i32::from_str_radix("a12", 10) {
/// println!("Failed conversion to i32: {}", e);
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct ParseIntError {
pub(super) kind: IntErrorKind,
}

/// Enum to store the various types of errors that can cause parsing an integer to fail.
///
/// # Example
///
/// ```
/// #![feature(int_error_matching)]
///
/// # fn main() {
/// if let Err(e) = i32::from_str_radix("a12", 10) {
/// println!("Failed conversion to i32: {:?}", e.kind());
/// }
/// # }
/// ```
#[unstable(
feature = "int_error_matching",
reason = "it can be useful to match errors when making error messages \
for integer parsing",
issue = "22639"
)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum IntErrorKind {
/// Value being parsed is empty.
///
/// Among other causes, this variant will be constructed when parsing an empty string.
Empty,
/// Contains an invalid digit.
///
/// Among other causes, this variant will be constructed when parsing a string that
/// contains a letter.
InvalidDigit,
/// Integer is too large to store in target integer type.
Overflow,
/// Integer is too small to store in target integer type.
Underflow,
/// Value was Zero
///
/// This variant will be emitted when the parsing string has a value of zero, which
/// would be illegal for non-zero types.
Zero,
}

impl ParseIntError {
/// Outputs the detailed cause of parsing an integer failing.
#[unstable(
feature = "int_error_matching",
reason = "it can be useful to match errors when making error messages \
for integer parsing",
issue = "22639"
)]
pub fn kind(&self) -> &IntErrorKind {
&self.kind
}
#[unstable(
feature = "int_error_internals",
reason = "available through Error trait and this method should \
not be exposed publicly",
issue = "none"
)]
#[doc(hidden)]
pub fn __description(&self) -> &str {
match self.kind {
IntErrorKind::Empty => "cannot parse integer from empty string",
IntErrorKind::InvalidDigit => "invalid digit found in string",
IntErrorKind::Overflow => "number too large to fit in target type",
IntErrorKind::Underflow => "number too small to fit in target type",
IntErrorKind::Zero => "number would be zero for non-zero type",
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseIntError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.__description().fmt(f)
}
}
Loading