1use crate::error::Error;
4use crate::fmt;
5
6#[stable(feature = "try_from", since = "1.34.0")]
8#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9pub struct TryFromIntError(pub(crate) IntErrorKind);
10
11impl TryFromIntError {
12 #[must_use]
14 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
15 pub const fn kind(&self) -> &IntErrorKind {
16 &self.0
17 }
18}
19
20#[stable(feature = "try_from", since = "1.34.0")]
21impl fmt::Display for TryFromIntError {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 match self.0 {
24 IntErrorKind::Empty | IntErrorKind::InvalidDigit => unreachable!(),
25 IntErrorKind::PosOverflow => "number too large to fit in target type",
26 IntErrorKind::NegOverflow => "number too small to fit in target type",
27 IntErrorKind::Zero => "number would be zero for non-zero type",
28 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
29 }
30 .fmt(f)
31 }
32}
33
34#[stable(feature = "try_from", since = "1.34.0")]
35impl Error for TryFromIntError {}
36
37#[stable(feature = "never_type", since = "CURRENT_RUSTC_VERSION")]
38#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
39const impl From<!> for TryFromIntError {
40 #[inline]
41 fn from(never: !) -> TryFromIntError {
42 never
43 }
44}
45
46#[derive(Debug, Clone, PartialEq, Eq)]
68#[stable(feature = "rust1", since = "1.0.0")]
69pub struct ParseIntError {
70 pub(super) kind: IntErrorKind,
71}
72
73#[stable(feature = "int_error_matching", since = "1.55.0")]
86#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
87#[non_exhaustive]
88pub enum IntErrorKind {
89 #[stable(feature = "int_error_matching", since = "1.55.0")]
93 Empty,
94 #[stable(feature = "int_error_matching", since = "1.55.0")]
102 InvalidDigit,
103 #[stable(feature = "int_error_matching", since = "1.55.0")]
105 PosOverflow,
106 #[stable(feature = "int_error_matching", since = "1.55.0")]
108 NegOverflow,
109 #[stable(feature = "int_error_matching", since = "1.55.0")]
114 Zero,
115 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
122 NotAPowerOfTwo,
124}
125
126impl ParseIntError {
127 #[must_use]
129 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
130 #[stable(feature = "int_error_matching", since = "1.55.0")]
131 pub const fn kind(&self) -> &IntErrorKind {
132 &self.kind
133 }
134}
135
136#[stable(feature = "rust1", since = "1.0.0")]
137impl fmt::Display for ParseIntError {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 match self.kind {
140 IntErrorKind::Empty => "cannot parse integer from empty string",
141 IntErrorKind::InvalidDigit => "invalid digit found in string",
142 IntErrorKind::PosOverflow => "number too large to fit in target type",
143 IntErrorKind::NegOverflow => "number too small to fit in target type",
144 IntErrorKind::Zero => "number would be zero for non-zero type",
145 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
146 }
147 .fmt(f)
148 }
149}
150
151#[stable(feature = "rust1", since = "1.0.0")]
152impl Error for ParseIntError {}