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)]
65#[stable(feature = "rust1", since = "1.0.0")]
66pub struct ParseIntError {
67    pub(super) kind: IntErrorKind,
68}
69
70#[stable(feature = "int_error_matching", since = "1.55.0")]
82#[derive(Debug, Clone, PartialEq, Eq)]
83#[non_exhaustive]
84pub enum IntErrorKind {
85    #[stable(feature = "int_error_matching", since = "1.55.0")]
89    Empty,
90    #[stable(feature = "int_error_matching", since = "1.55.0")]
98    InvalidDigit,
99    #[stable(feature = "int_error_matching", since = "1.55.0")]
101    PosOverflow,
102    #[stable(feature = "int_error_matching", since = "1.55.0")]
104    NegOverflow,
105    #[stable(feature = "int_error_matching", since = "1.55.0")]
110    Zero,
111}
112
113impl ParseIntError {
114    #[must_use]
116    #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
117    #[stable(feature = "int_error_matching", since = "1.55.0")]
118    pub const fn kind(&self) -> &IntErrorKind {
119        &self.kind
120    }
121}
122
123#[stable(feature = "rust1", since = "1.0.0")]
124impl fmt::Display for ParseIntError {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        #[allow(deprecated)]
127        self.description().fmt(f)
128    }
129}
130
131#[stable(feature = "rust1", since = "1.0.0")]
132impl Error for ParseIntError {
133    #[allow(deprecated)]
134    fn description(&self) -> &str {
135        match self.kind {
136            IntErrorKind::Empty => "cannot parse integer from empty string",
137            IntErrorKind::InvalidDigit => "invalid digit found in string",
138            IntErrorKind::PosOverflow => "number too large to fit in target type",
139            IntErrorKind::NegOverflow => "number too small to fit in target type",
140            IntErrorKind::Zero => "number would be zero for non-zero type",
141        }
142    }
143}