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")]
29impl From<Infallible> for TryFromIntError {
30 fn from(x: Infallible) -> TryFromIntError {
31 match x {}
32 }
33}
34
35#[unstable(feature = "never_type", issue = "35121")]
36impl From<!> for TryFromIntError {
37 #[inline]
38 fn from(never: !) -> TryFromIntError {
39 match 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")]
85#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
86#[non_exhaustive]
87pub enum IntErrorKind {
88 #[stable(feature = "int_error_matching", since = "1.55.0")]
92 Empty,
93 #[stable(feature = "int_error_matching", since = "1.55.0")]
101 InvalidDigit,
102 #[stable(feature = "int_error_matching", since = "1.55.0")]
104 PosOverflow,
105 #[stable(feature = "int_error_matching", since = "1.55.0")]
107 NegOverflow,
108 #[stable(feature = "int_error_matching", since = "1.55.0")]
113 Zero,
114}
115
116impl ParseIntError {
117 #[must_use]
119 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
120 #[stable(feature = "int_error_matching", since = "1.55.0")]
121 pub const fn kind(&self) -> &IntErrorKind {
122 &self.kind
123 }
124}
125
126#[stable(feature = "rust1", since = "1.0.0")]
127impl fmt::Display for ParseIntError {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 #[allow(deprecated)]
130 self.description().fmt(f)
131 }
132}
133
134#[stable(feature = "rust1", since = "1.0.0")]
135impl Error for ParseIntError {
136 #[allow(deprecated)]
137 fn description(&self) -> &str {
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 }
145 }
146}