Skip to content

Commit 550939f

Browse files
committed
Move error structs to new mod
1 parent 7125a48 commit 550939f

File tree

2 files changed

+174
-162
lines changed

2 files changed

+174
-162
lines changed

library/core/src/num/error.rs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
//! Error types for conversion to integral types.
2+
3+
use crate::convert::Infallible;
4+
use crate::fmt;
5+
6+
/// The error type returned when a checked integral type conversion fails.
7+
#[stable(feature = "try_from", since = "1.34.0")]
8+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9+
pub struct TryFromIntError(pub(crate) ());
10+
11+
impl TryFromIntError {
12+
#[unstable(
13+
feature = "int_error_internals",
14+
reason = "available through Error trait and this method should \
15+
not be exposed publicly",
16+
issue = "none"
17+
)]
18+
#[doc(hidden)]
19+
pub fn __description(&self) -> &str {
20+
"out of range integral type conversion attempted"
21+
}
22+
}
23+
24+
#[stable(feature = "try_from", since = "1.34.0")]
25+
impl fmt::Display for TryFromIntError {
26+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
self.__description().fmt(fmt)
28+
}
29+
}
30+
31+
#[stable(feature = "try_from", since = "1.34.0")]
32+
impl From<Infallible> for TryFromIntError {
33+
fn from(x: Infallible) -> TryFromIntError {
34+
match x {}
35+
}
36+
}
37+
38+
#[unstable(feature = "never_type", issue = "35121")]
39+
impl From<!> for TryFromIntError {
40+
fn from(never: !) -> TryFromIntError {
41+
// Match rather than coerce to make sure that code like
42+
// `From<Infallible> for TryFromIntError` above will keep working
43+
// when `Infallible` becomes an alias to `!`.
44+
match never {}
45+
}
46+
}
47+
48+
/// An error which can be returned when parsing an integer.
49+
///
50+
/// This error is used as the error type for the `from_str_radix()` functions
51+
/// on the primitive integer types, such as [`i8::from_str_radix`].
52+
///
53+
/// # Potential causes
54+
///
55+
/// Among other causes, `ParseIntError` can be thrown because of leading or trailing whitespace
56+
/// in the string e.g., when it is obtained from the standard input.
57+
/// Using the [`str.trim()`] method ensures that no whitespace remains before parsing.
58+
///
59+
/// [`str.trim()`]: ../../std/primitive.str.html#method.trim
60+
/// [`i8::from_str_radix`]: ../../std/primitive.i8.html#method.from_str_radix
61+
///
62+
/// # Example
63+
///
64+
/// ```
65+
/// if let Err(e) = i32::from_str_radix("a12", 10) {
66+
/// println!("Failed conversion to i32: {}", e);
67+
/// }
68+
/// ```
69+
#[derive(Debug, Clone, PartialEq, Eq)]
70+
#[stable(feature = "rust1", since = "1.0.0")]
71+
pub struct ParseIntError {
72+
pub(super) kind: IntErrorKind,
73+
}
74+
75+
/// Enum to store the various types of errors that can cause parsing an integer to fail.
76+
///
77+
/// # Example
78+
///
79+
/// ```
80+
/// #![feature(int_error_matching)]
81+
///
82+
/// # fn main() {
83+
/// if let Err(e) = i32::from_str_radix("a12", 10) {
84+
/// println!("Failed conversion to i32: {:?}", e.kind());
85+
/// }
86+
/// # }
87+
/// ```
88+
#[unstable(
89+
feature = "int_error_matching",
90+
reason = "it can be useful to match errors when making error messages \
91+
for integer parsing",
92+
issue = "22639"
93+
)]
94+
#[derive(Debug, Clone, PartialEq, Eq)]
95+
#[non_exhaustive]
96+
pub enum IntErrorKind {
97+
/// Value being parsed is empty.
98+
///
99+
/// Among other causes, this variant will be constructed when parsing an empty string.
100+
Empty,
101+
/// Contains an invalid digit.
102+
///
103+
/// Among other causes, this variant will be constructed when parsing a string that
104+
/// contains a letter.
105+
InvalidDigit,
106+
/// Integer is too large to store in target integer type.
107+
Overflow,
108+
/// Integer is too small to store in target integer type.
109+
Underflow,
110+
/// Value was Zero
111+
///
112+
/// This variant will be emitted when the parsing string has a value of zero, which
113+
/// would be illegal for non-zero types.
114+
Zero,
115+
}
116+
117+
impl ParseIntError {
118+
/// Outputs the detailed cause of parsing an integer failing.
119+
#[unstable(
120+
feature = "int_error_matching",
121+
reason = "it can be useful to match errors when making error messages \
122+
for integer parsing",
123+
issue = "22639"
124+
)]
125+
pub fn kind(&self) -> &IntErrorKind {
126+
&self.kind
127+
}
128+
#[unstable(
129+
feature = "int_error_internals",
130+
reason = "available through Error trait and this method should \
131+
not be exposed publicly",
132+
issue = "none"
133+
)]
134+
#[doc(hidden)]
135+
pub fn __description(&self) -> &str {
136+
match self.kind {
137+
IntErrorKind::Empty => "cannot parse integer from empty string",
138+
IntErrorKind::InvalidDigit => "invalid digit found in string",
139+
IntErrorKind::Overflow => "number too large to fit in target type",
140+
IntErrorKind::Underflow => "number too small to fit in target type",
141+
IntErrorKind::Zero => "number would be zero for non-zero type",
142+
}
143+
}
144+
}
145+
146+
#[stable(feature = "rust1", since = "1.0.0")]
147+
impl fmt::Display for ParseIntError {
148+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149+
self.__description().fmt(f)
150+
}
151+
}

library/core/src/num/mod.rs

Lines changed: 23 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
55
#![stable(feature = "rust1", since = "1.0.0")]
66

7-
use crate::convert::Infallible;
8-
use crate::fmt;
97
use crate::intrinsics;
108
use crate::mem;
119
use crate::str::FromStr;
@@ -40,18 +38,31 @@ pub mod dec2flt;
4038
pub mod diy_float;
4139
pub mod flt2dec;
4240

41+
mod error;
4342
mod nonzero;
4443
mod wrapping;
4544

4645
#[stable(feature = "rust1", since = "1.0.0")]
4746
pub use wrapping::Wrapping;
4847

48+
#[stable(feature = "rust1", since = "1.0.0")]
49+
pub use dec2flt::ParseFloatError;
50+
51+
#[stable(feature = "rust1", since = "1.0.0")]
52+
pub use error::ParseIntError;
53+
4954
#[stable(feature = "nonzero", since = "1.28.0")]
5055
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
5156

5257
#[stable(feature = "signed_nonzero", since = "1.34.0")]
5358
pub use nonzero::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
5459

60+
#[stable(feature = "try_from", since = "1.34.0")]
61+
pub use error::TryFromIntError;
62+
63+
#[unstable(feature = "int_error_matching", issue = "22639")]
64+
pub use error::IntErrorKind;
65+
5566
macro_rules! usize_isize_to_xe_bytes_doc {
5667
() => {
5768
"
@@ -4904,6 +4915,16 @@ pub enum FpCategory {
49044915
Normal,
49054916
}
49064917

4918+
#[doc(hidden)]
4919+
trait FromStrRadixHelper: PartialOrd + Copy {
4920+
fn min_value() -> Self;
4921+
fn max_value() -> Self;
4922+
fn from_u32(u: u32) -> Self;
4923+
fn checked_mul(&self, other: u32) -> Option<Self>;
4924+
fn checked_sub(&self, other: u32) -> Option<Self>;
4925+
fn checked_add(&self, other: u32) -> Option<Self>;
4926+
}
4927+
49074928
macro_rules! from_str_radix_int_impl {
49084929
($($t:ty)*) => {$(
49094930
#[stable(feature = "rust1", since = "1.0.0")]
@@ -4917,58 +4938,6 @@ macro_rules! from_str_radix_int_impl {
49174938
}
49184939
from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
49194940

4920-
/// The error type returned when a checked integral type conversion fails.
4921-
#[stable(feature = "try_from", since = "1.34.0")]
4922-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
4923-
pub struct TryFromIntError(pub(crate) ());
4924-
4925-
impl TryFromIntError {
4926-
#[unstable(
4927-
feature = "int_error_internals",
4928-
reason = "available through Error trait and this method should \
4929-
not be exposed publicly",
4930-
issue = "none"
4931-
)]
4932-
#[doc(hidden)]
4933-
pub fn __description(&self) -> &str {
4934-
"out of range integral type conversion attempted"
4935-
}
4936-
}
4937-
4938-
#[stable(feature = "try_from", since = "1.34.0")]
4939-
impl fmt::Display for TryFromIntError {
4940-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
4941-
self.__description().fmt(fmt)
4942-
}
4943-
}
4944-
4945-
#[stable(feature = "try_from", since = "1.34.0")]
4946-
impl From<Infallible> for TryFromIntError {
4947-
fn from(x: Infallible) -> TryFromIntError {
4948-
match x {}
4949-
}
4950-
}
4951-
4952-
#[unstable(feature = "never_type", issue = "35121")]
4953-
impl From<!> for TryFromIntError {
4954-
fn from(never: !) -> TryFromIntError {
4955-
// Match rather than coerce to make sure that code like
4956-
// `From<Infallible> for TryFromIntError` above will keep working
4957-
// when `Infallible` becomes an alias to `!`.
4958-
match never {}
4959-
}
4960-
}
4961-
4962-
#[doc(hidden)]
4963-
trait FromStrRadixHelper: PartialOrd + Copy {
4964-
fn min_value() -> Self;
4965-
fn max_value() -> Self;
4966-
fn from_u32(u: u32) -> Self;
4967-
fn checked_mul(&self, other: u32) -> Option<Self>;
4968-
fn checked_sub(&self, other: u32) -> Option<Self>;
4969-
fn checked_add(&self, other: u32) -> Option<Self>;
4970-
}
4971-
49724941
macro_rules! doit {
49734942
($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
49744943
#[inline]
@@ -5061,111 +5030,3 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
50615030
}
50625031
Ok(result)
50635032
}
5064-
5065-
/// An error which can be returned when parsing an integer.
5066-
///
5067-
/// This error is used as the error type for the `from_str_radix()` functions
5068-
/// on the primitive integer types, such as [`i8::from_str_radix`].
5069-
///
5070-
/// # Potential causes
5071-
///
5072-
/// Among other causes, `ParseIntError` can be thrown because of leading or trailing whitespace
5073-
/// in the string e.g., when it is obtained from the standard input.
5074-
/// Using the [`str.trim()`] method ensures that no whitespace remains before parsing.
5075-
///
5076-
/// [`str.trim()`]: ../../std/primitive.str.html#method.trim
5077-
/// [`i8::from_str_radix`]: ../../std/primitive.i8.html#method.from_str_radix
5078-
///
5079-
/// # Example
5080-
///
5081-
/// ```
5082-
/// if let Err(e) = i32::from_str_radix("a12", 10) {
5083-
/// println!("Failed conversion to i32: {}", e);
5084-
/// }
5085-
/// ```
5086-
#[derive(Debug, Clone, PartialEq, Eq)]
5087-
#[stable(feature = "rust1", since = "1.0.0")]
5088-
pub struct ParseIntError {
5089-
kind: IntErrorKind,
5090-
}
5091-
5092-
/// Enum to store the various types of errors that can cause parsing an integer to fail.
5093-
///
5094-
/// # Example
5095-
///
5096-
/// ```
5097-
/// #![feature(int_error_matching)]
5098-
///
5099-
/// # fn main() {
5100-
/// if let Err(e) = i32::from_str_radix("a12", 10) {
5101-
/// println!("Failed conversion to i32: {:?}", e.kind());
5102-
/// }
5103-
/// # }
5104-
/// ```
5105-
#[unstable(
5106-
feature = "int_error_matching",
5107-
reason = "it can be useful to match errors when making error messages \
5108-
for integer parsing",
5109-
issue = "22639"
5110-
)]
5111-
#[derive(Debug, Clone, PartialEq, Eq)]
5112-
#[non_exhaustive]
5113-
pub enum IntErrorKind {
5114-
/// Value being parsed is empty.
5115-
///
5116-
/// Among other causes, this variant will be constructed when parsing an empty string.
5117-
Empty,
5118-
/// Contains an invalid digit.
5119-
///
5120-
/// Among other causes, this variant will be constructed when parsing a string that
5121-
/// contains a letter.
5122-
InvalidDigit,
5123-
/// Integer is too large to store in target integer type.
5124-
Overflow,
5125-
/// Integer is too small to store in target integer type.
5126-
Underflow,
5127-
/// Value was Zero
5128-
///
5129-
/// This variant will be emitted when the parsing string has a value of zero, which
5130-
/// would be illegal for non-zero types.
5131-
Zero,
5132-
}
5133-
5134-
impl ParseIntError {
5135-
/// Outputs the detailed cause of parsing an integer failing.
5136-
#[unstable(
5137-
feature = "int_error_matching",
5138-
reason = "it can be useful to match errors when making error messages \
5139-
for integer parsing",
5140-
issue = "22639"
5141-
)]
5142-
pub fn kind(&self) -> &IntErrorKind {
5143-
&self.kind
5144-
}
5145-
#[unstable(
5146-
feature = "int_error_internals",
5147-
reason = "available through Error trait and this method should \
5148-
not be exposed publicly",
5149-
issue = "none"
5150-
)]
5151-
#[doc(hidden)]
5152-
pub fn __description(&self) -> &str {
5153-
match self.kind {
5154-
IntErrorKind::Empty => "cannot parse integer from empty string",
5155-
IntErrorKind::InvalidDigit => "invalid digit found in string",
5156-
IntErrorKind::Overflow => "number too large to fit in target type",
5157-
IntErrorKind::Underflow => "number too small to fit in target type",
5158-
IntErrorKind::Zero => "number would be zero for non-zero type",
5159-
}
5160-
}
5161-
}
5162-
5163-
#[stable(feature = "rust1", since = "1.0.0")]
5164-
impl fmt::Display for ParseIntError {
5165-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5166-
self.__description().fmt(f)
5167-
}
5168-
}
5169-
5170-
#[stable(feature = "rust1", since = "1.0.0")]
5171-
pub use crate::num::dec2flt::ParseFloatError;

0 commit comments

Comments
 (0)