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) ());
11
12#[stable(feature = "try_from", since = "1.34.0")]
13impl 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")]
21impl 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#[rustc_const_unstable(feature = "const_try", issue = "74935")]
30impl const From<Infallible> for TryFromIntError {
31 fn from(x: Infallible) -> TryFromIntError {
32 match x {}
33 }
34}
35
36#[unstable(feature = "never_type", issue = "35121")]
37#[rustc_const_unstable(feature = "const_try", issue = "74935")]
38impl const From<!> for TryFromIntError {
39 #[inline]
40 fn from(never: !) -> TryFromIntError {
41 match never {}
45 }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
70#[stable(feature = "rust1", since = "1.0.0")]
71pub struct ParseIntError {
72 pub(super) kind: IntErrorKind,
73}
74
75#[stable(feature = "int_error_matching", since = "1.55.0")]
87#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
88#[non_exhaustive]
89pub enum IntErrorKind {
90 #[stable(feature = "int_error_matching", since = "1.55.0")]
94 Empty,
95 #[stable(feature = "int_error_matching", since = "1.55.0")]
103 InvalidDigit,
104 #[stable(feature = "int_error_matching", since = "1.55.0")]
106 PosOverflow,
107 #[stable(feature = "int_error_matching", since = "1.55.0")]
109 NegOverflow,
110 #[stable(feature = "int_error_matching", since = "1.55.0")]
115 Zero,
116}
117
118impl ParseIntError {
119 #[must_use]
121 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
122 #[stable(feature = "int_error_matching", since = "1.55.0")]
123 pub const fn kind(&self) -> &IntErrorKind {
124 &self.kind
125 }
126}
127
128#[stable(feature = "rust1", since = "1.0.0")]
129impl fmt::Display for ParseIntError {
130 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131 #[allow(deprecated)]
132 self.description().fmt(f)
133 }
134}
135
136#[stable(feature = "rust1", since = "1.0.0")]
137impl Error for ParseIntError {
138 #[allow(deprecated)]
139 fn description(&self) -> &str {
140 match self.kind {
141 IntErrorKind::Empty => "cannot parse integer from empty string",
142 IntErrorKind::InvalidDigit => "invalid digit found in string",
143 IntErrorKind::PosOverflow => "number too large to fit in target type",
144 IntErrorKind::NegOverflow => "number too small to fit in target type",
145 IntErrorKind::Zero => "number would be zero for non-zero type",
146 }
147 }
148}