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, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 "out of range integral type conversion attempted".fmt(f)
16 }
17}
18
19#[stable(feature = "try_from", since = "1.34.0")]
20impl Error for TryFromIntError {}
21
22#[stable(feature = "try_from", since = "1.34.0")]
23#[rustc_const_unstable(feature = "const_try", issue = "74935")]
24impl const From<Infallible> for TryFromIntError {
25 fn from(x: Infallible) -> TryFromIntError {
26 match x {}
27 }
28}
29
30#[unstable(feature = "never_type", issue = "35121")]
31#[rustc_const_unstable(feature = "const_try", issue = "74935")]
32impl const From<!> for TryFromIntError {
33 #[inline]
34 fn from(never: !) -> TryFromIntError {
35 match never {}
39 }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
64#[stable(feature = "rust1", since = "1.0.0")]
65pub struct ParseIntError {
66 pub(super) kind: IntErrorKind,
67}
68
69#[stable(feature = "int_error_matching", since = "1.55.0")]
81#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
82#[non_exhaustive]
83pub enum IntErrorKind {
84 #[stable(feature = "int_error_matching", since = "1.55.0")]
88 Empty,
89 #[stable(feature = "int_error_matching", since = "1.55.0")]
97 InvalidDigit,
98 #[stable(feature = "int_error_matching", since = "1.55.0")]
100 PosOverflow,
101 #[stable(feature = "int_error_matching", since = "1.55.0")]
103 NegOverflow,
104 #[stable(feature = "int_error_matching", since = "1.55.0")]
109 Zero,
110}
111
112impl ParseIntError {
113 #[must_use]
115 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
116 #[stable(feature = "int_error_matching", since = "1.55.0")]
117 pub const fn kind(&self) -> &IntErrorKind {
118 &self.kind
119 }
120}
121
122#[stable(feature = "rust1", since = "1.0.0")]
123impl fmt::Display for ParseIntError {
124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125 match self.kind {
126 IntErrorKind::Empty => "cannot parse integer from empty string",
127 IntErrorKind::InvalidDigit => "invalid digit found in string",
128 IntErrorKind::PosOverflow => "number too large to fit in target type",
129 IntErrorKind::NegOverflow => "number too small to fit in target type",
130 IntErrorKind::Zero => "number would be zero for non-zero type",
131 }
132 .fmt(f)
133 }
134}
135
136#[stable(feature = "rust1", since = "1.0.0")]
137impl Error for ParseIntError {}