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)]
10#[ferrocene::prevalidated]
11pub struct TryFromIntError(pub(crate) ());
12
13#[stable(feature = "try_from", since = "1.34.0")]
14impl fmt::Display for TryFromIntError {
15 #[ferrocene::prevalidated]
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 "out of range integral type conversion attempted".fmt(f)
18 }
19}
20
21#[stable(feature = "try_from", since = "1.34.0")]
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 #[ferrocene::prevalidated]
28 fn from(x: Infallible) -> TryFromIntError {
29 match x {}
30 }
31}
32
33#[unstable(feature = "never_type", issue = "35121")]
34#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
35impl const From<!> for TryFromIntError {
36 #[inline]
37 #[ferrocene::prevalidated]
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")]
69#[ferrocene::prevalidated]
70pub struct ParseIntError {
71 pub(super) kind: IntErrorKind,
72}
73
74#[stable(feature = "int_error_matching", since = "1.55.0")]
86#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
87#[non_exhaustive]
88#[ferrocene::prevalidated]
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 #[ferrocene::prevalidated]
124 pub const fn kind(&self) -> &IntErrorKind {
125 &self.kind
126 }
127}
128
129#[stable(feature = "rust1", since = "1.0.0")]
130impl fmt::Display for ParseIntError {
131 #[ferrocene::prevalidated]
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 match self.kind {
134 IntErrorKind::Empty => "cannot parse integer from empty string",
135 IntErrorKind::InvalidDigit => "invalid digit found in string",
136 IntErrorKind::PosOverflow => "number too large to fit in target type",
137 IntErrorKind::NegOverflow => "number too small to fit in target type",
138 IntErrorKind::Zero => "number would be zero for non-zero type",
139 }
140 .fmt(f)
141 }
142}
143
144#[stable(feature = "rust1", since = "1.0.0")]
145impl Error for ParseIntError {}