Skip to content

Commit 59fb88d

Browse files
committed
Auto merge of #76327 - lzutao:split-core-num, r=SimonSapin
Split `core/num/mod.rs` to smaller mods Note for reviewer: * I split to multiple commits for easier reviewing, but I could git squash them all to one if requested. * Recommend pulling this change locally and using advanced git diff viewer or this command: ``` git show --reverse --color-moved=dimmed-zebra master.. ``` --- I split `core/num/mod.rs` to these modules: * `error`: For error structs like `ParseIntError`. * blanket `shells` dir: For dummy number type modules: std::i32, std::f32, and the likes. Why? See below. * `int_macros` and `uint_macros`: Real implementation of all integer types via `int_impl` and `uint_impl` * `nonzero`: For `NonZero*` types and their implementations. * `wrapping`: For `Wrapping<T>` types.
2 parents b3aae05 + a50ec5f commit 59fb88d

20 files changed

+4748
-4707
lines changed

Diff for: library/core/src/lib.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -169,34 +169,34 @@ mod macros;
169169
#[macro_use]
170170
mod internal_macros;
171171

172-
#[path = "num/int_macros.rs"]
172+
#[path = "num/shells/int_macros.rs"]
173173
#[macro_use]
174174
mod int_macros;
175175

176-
#[path = "num/i128.rs"]
176+
#[path = "num/shells/i128.rs"]
177177
pub mod i128;
178-
#[path = "num/i16.rs"]
178+
#[path = "num/shells/i16.rs"]
179179
pub mod i16;
180-
#[path = "num/i32.rs"]
180+
#[path = "num/shells/i32.rs"]
181181
pub mod i32;
182-
#[path = "num/i64.rs"]
182+
#[path = "num/shells/i64.rs"]
183183
pub mod i64;
184-
#[path = "num/i8.rs"]
184+
#[path = "num/shells/i8.rs"]
185185
pub mod i8;
186-
#[path = "num/isize.rs"]
186+
#[path = "num/shells/isize.rs"]
187187
pub mod isize;
188188

189-
#[path = "num/u128.rs"]
189+
#[path = "num/shells/u128.rs"]
190190
pub mod u128;
191-
#[path = "num/u16.rs"]
191+
#[path = "num/shells/u16.rs"]
192192
pub mod u16;
193-
#[path = "num/u32.rs"]
193+
#[path = "num/shells/u32.rs"]
194194
pub mod u32;
195-
#[path = "num/u64.rs"]
195+
#[path = "num/shells/u64.rs"]
196196
pub mod u64;
197-
#[path = "num/u8.rs"]
197+
#[path = "num/shells/u8.rs"]
198198
pub mod u8;
199-
#[path = "num/usize.rs"]
199+
#[path = "num/shells/usize.rs"]
200200
pub mod usize;
201201

202202
#[path = "num/f32.rs"]

Diff for: library/core/src/num/error.rs

+151
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+
}

0 commit comments

Comments
 (0)