1use crate::error::Error;
4use crate::fmt;
5
6#[stable(feature = "try_from", since = "1.34.0")]
8#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9#[ferrocene::prevalidated]
10pub struct TryFromIntError(pub(crate) IntErrorKind);
11
12impl TryFromIntError {
13 #[must_use]
15 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
16 pub const fn kind(&self) -> &IntErrorKind {
17 &self.0
18 }
19}
20
21#[stable(feature = "try_from", since = "1.34.0")]
22impl fmt::Display for TryFromIntError {
23 #[ferrocene::prevalidated]
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 match self.0 {
26 IntErrorKind::Empty | IntErrorKind::InvalidDigit => unreachable!(),
27 IntErrorKind::PosOverflow => "number too large to fit in target type",
28 IntErrorKind::NegOverflow => "number too small to fit in target type",
29 IntErrorKind::Zero => "number would be zero for non-zero type",
30 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
31 }
32 .fmt(f)
33 }
34}
35
36#[stable(feature = "try_from", since = "1.34.0")]
37impl Error for TryFromIntError {}
38
39#[stable(feature = "never_type", since = "CURRENT_RUSTC_VERSION")]
40#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
41const impl From<!> for TryFromIntError {
42 #[inline]
43 #[ferrocene::prevalidated]
44 fn from(never: !) -> TryFromIntError {
45 never
46 }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
71#[stable(feature = "rust1", since = "1.0.0")]
72#[ferrocene::prevalidated]
73pub struct ParseIntError {
74 pub(super) kind: IntErrorKind,
75}
76
77#[stable(feature = "int_error_matching", since = "1.55.0")]
90#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
91#[non_exhaustive]
92#[ferrocene::prevalidated]
93pub enum IntErrorKind {
94 #[stable(feature = "int_error_matching", since = "1.55.0")]
98 Empty,
99 #[stable(feature = "int_error_matching", since = "1.55.0")]
107 InvalidDigit,
108 #[stable(feature = "int_error_matching", since = "1.55.0")]
110 PosOverflow,
111 #[stable(feature = "int_error_matching", since = "1.55.0")]
113 NegOverflow,
114 #[stable(feature = "int_error_matching", since = "1.55.0")]
119 Zero,
120 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
127 NotAPowerOfTwo,
129}
130
131impl ParseIntError {
132 #[must_use]
134 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
135 #[stable(feature = "int_error_matching", since = "1.55.0")]
136 #[ferrocene::prevalidated]
137 pub const fn kind(&self) -> &IntErrorKind {
138 &self.kind
139 }
140}
141
142#[stable(feature = "rust1", since = "1.0.0")]
143impl fmt::Display for ParseIntError {
144 #[ferrocene::prevalidated]
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 match self.kind {
147 IntErrorKind::Empty => "cannot parse integer from empty string",
148 IntErrorKind::InvalidDigit => "invalid digit found in string",
149 IntErrorKind::PosOverflow => "number too large to fit in target type",
150 IntErrorKind::NegOverflow => "number too small to fit in target type",
151 IntErrorKind::Zero => "number would be zero for non-zero type",
152 IntErrorKind::NotAPowerOfTwo => {
153 #[cfg(feature = "ferrocene_test")]
154 unreachable!();
155 #[ferrocene::annotation(
156 "This variant can't be constructed anywhere in the API surface. \
157 Testing would require transmuting from a fake struct, the unreachable!() \
158 above will indicate to us if this ever does become a live codepath."
159 )]
160 "number is not a power of two"
161 }
162 }
163 .fmt(f)
164 }
165}
166
167#[stable(feature = "rust1", since = "1.0.0")]
168impl Error for ParseIntError {}