1use crate::convert::Infallible;
4#[cfg(not(feature = "ferrocene_subset"))]
5use crate::error::Error;
6use crate::fmt;
7
8#[stable(feature = "try_from", since = "1.34.0")]
10#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11pub struct TryFromIntError(pub(crate) ());
12
13#[stable(feature = "try_from", since = "1.34.0")]
14impl fmt::Display for TryFromIntError {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 "out of range integral type conversion attempted".fmt(f)
17 }
18}
19
20#[stable(feature = "try_from", since = "1.34.0")]
21#[cfg(not(feature = "ferrocene_subset"))]
22impl Error for TryFromIntError {}
23
24#[stable(feature = "try_from", since = "1.34.0")]
25#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
26impl const From<Infallible> for TryFromIntError {
27 fn from(x: Infallible) -> TryFromIntError {
28 match x {}
29 }
30}
31
32#[unstable(feature = "never_type", issue = "35121")]
33#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
34impl const From<!> for TryFromIntError {
35 #[inline]
36 fn from(never: !) -> TryFromIntError {
37 match never {}
41 }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
66#[stable(feature = "rust1", since = "1.0.0")]
67pub struct ParseIntError {
68 pub(super) kind: IntErrorKind,
69}
70
71#[stable(feature = "int_error_matching", since = "1.55.0")]
83#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
84#[non_exhaustive]
85pub enum IntErrorKind {
86 #[stable(feature = "int_error_matching", since = "1.55.0")]
90 Empty,
91 #[stable(feature = "int_error_matching", since = "1.55.0")]
99 InvalidDigit,
100 #[stable(feature = "int_error_matching", since = "1.55.0")]
102 PosOverflow,
103 #[stable(feature = "int_error_matching", since = "1.55.0")]
105 NegOverflow,
106 #[stable(feature = "int_error_matching", since = "1.55.0")]
111 Zero,
112}
113
114impl ParseIntError {
115 #[must_use]
117 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
118 #[stable(feature = "int_error_matching", since = "1.55.0")]
119 pub const fn kind(&self) -> &IntErrorKind {
120 &self.kind
121 }
122}
123
124#[stable(feature = "rust1", since = "1.0.0")]
125impl fmt::Display for ParseIntError {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 match self.kind {
128 IntErrorKind::Empty => "cannot parse integer from empty string",
129 IntErrorKind::InvalidDigit => "invalid digit found in string",
130 IntErrorKind::PosOverflow => "number too large to fit in target type",
131 IntErrorKind::NegOverflow => "number too small to fit in target type",
132 IntErrorKind::Zero => "number would be zero for non-zero type",
133 }
134 .fmt(f)
135 }
136}
137
138#[stable(feature = "rust1", since = "1.0.0")]
139#[cfg(not(feature = "ferrocene_subset"))]
140impl Error for ParseIntError {}