1use crate::convert::Infallible;
4use crate::error::Error;
5use crate::fmt;
6
7#[stable(feature = "try_from", since = "1.34.0")]
9#[derive(Debug, Copy, Clone, PartialEq, Eq)]
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 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self.0 {
25 IntErrorKind::Empty | IntErrorKind::InvalidDigit => unreachable!(),
26 IntErrorKind::PosOverflow => "number too large to fit in target type",
27 IntErrorKind::NegOverflow => "number too small to fit in target type",
28 IntErrorKind::Zero => "number would be zero for non-zero type",
29 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
30 }
31 .fmt(f)
32 }
33}
34
35#[stable(feature = "try_from", since = "1.34.0")]
36impl Error for TryFromIntError {}
37
38#[stable(feature = "try_from", since = "1.34.0")]
39#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
40const impl From<Infallible> for TryFromIntError {
41 fn from(x: Infallible) -> TryFromIntError {
42 match x {}
43 }
44}
45
46#[unstable(feature = "never_type", issue = "35121")]
47#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
48const impl From<!> for TryFromIntError {
49 #[inline]
50 fn from(never: !) -> TryFromIntError {
51 match never {}
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
80#[stable(feature = "rust1", since = "1.0.0")]
81pub struct ParseIntError {
82 pub(super) kind: IntErrorKind,
83}
84
85#[stable(feature = "int_error_matching", since = "1.55.0")]
98#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
99#[non_exhaustive]
100pub enum IntErrorKind {
101 #[stable(feature = "int_error_matching", since = "1.55.0")]
105 Empty,
106 #[stable(feature = "int_error_matching", since = "1.55.0")]
114 InvalidDigit,
115 #[stable(feature = "int_error_matching", since = "1.55.0")]
117 PosOverflow,
118 #[stable(feature = "int_error_matching", since = "1.55.0")]
120 NegOverflow,
121 #[stable(feature = "int_error_matching", since = "1.55.0")]
126 Zero,
127 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
134 NotAPowerOfTwo,
136}
137
138impl ParseIntError {
139 #[must_use]
141 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
142 #[stable(feature = "int_error_matching", since = "1.55.0")]
143 pub const fn kind(&self) -> &IntErrorKind {
144 &self.kind
145 }
146}
147
148#[stable(feature = "rust1", since = "1.0.0")]
149impl fmt::Display for ParseIntError {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 match self.kind {
152 IntErrorKind::Empty => "cannot parse integer from empty string",
153 IntErrorKind::InvalidDigit => "invalid digit found in string",
154 IntErrorKind::PosOverflow => "number too large to fit in target type",
155 IntErrorKind::NegOverflow => "number too small to fit in target type",
156 IntErrorKind::Zero => "number would be zero for non-zero type",
157 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
158 }
159 .fmt(f)
160 }
161}
162
163#[stable(feature = "rust1", since = "1.0.0")]
164impl Error for ParseIntError {}