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 = "1.100.0")]
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,
123}
124
125impl ParseIntError {
126 #[must_use]
128 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
129 #[stable(feature = "int_error_matching", since = "1.55.0")]
130 pub const fn kind(&self) -> &IntErrorKind {
131 &self.kind
132 }
133}
134
135#[stable(feature = "rust1", since = "1.0.0")]
136impl fmt::Display for ParseIntError {
137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 match self.kind {
139 IntErrorKind::Empty => "cannot parse integer from empty string",
140 IntErrorKind::InvalidDigit => "invalid digit found in string",
141 IntErrorKind::PosOverflow => "number too large to fit in target type",
142 IntErrorKind::NegOverflow => "number too small to fit in target type",
143 IntErrorKind::Zero => "number would be zero for non-zero type",
144 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
145 }
146 .fmt(f)
147 }
148}
149
150#[stable(feature = "rust1", since = "1.0.0")]
151impl Error for ParseIntError {}