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