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) IntErrorKind);
12
13impl TryFromIntError {
14 #[must_use]
16 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
17 pub const fn kind(&self) -> &IntErrorKind {
18 &self.0
19 }
20}
21
22#[stable(feature = "try_from", since = "1.34.0")]
23impl fmt::Display for TryFromIntError {
24 #[ferrocene::prevalidated]
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 "out of range integral type conversion attempted".fmt(f)
27 }
28}
29
30#[stable(feature = "try_from", since = "1.34.0")]
31impl Error for TryFromIntError {}
32
33#[stable(feature = "try_from", since = "1.34.0")]
34#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
35impl const From<Infallible> for TryFromIntError {
36 #[ferrocene::prevalidated]
37 fn from(x: Infallible) -> TryFromIntError {
38 match x {}
39 }
40}
41
42#[unstable(feature = "never_type", issue = "35121")]
43#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
44impl const From<!> for TryFromIntError {
45 #[inline]
46 #[ferrocene::prevalidated]
47 fn from(never: !) -> TryFromIntError {
48 match never {}
52 }
53}
54
55#[derive(Debug, Clone, PartialEq, Eq)]
77#[stable(feature = "rust1", since = "1.0.0")]
78#[ferrocene::prevalidated]
79pub struct ParseIntError {
80 pub(super) kind: IntErrorKind,
81}
82
83#[stable(feature = "int_error_matching", since = "1.55.0")]
96#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
97#[non_exhaustive]
98#[ferrocene::prevalidated]
99pub enum IntErrorKind {
100 #[stable(feature = "int_error_matching", since = "1.55.0")]
104 Empty,
105 #[stable(feature = "int_error_matching", since = "1.55.0")]
113 InvalidDigit,
114 #[stable(feature = "int_error_matching", since = "1.55.0")]
116 PosOverflow,
117 #[stable(feature = "int_error_matching", since = "1.55.0")]
119 NegOverflow,
120 #[stable(feature = "int_error_matching", since = "1.55.0")]
125 Zero,
126 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
133 NotAPowerOfTwo,
135}
136
137impl ParseIntError {
138 #[must_use]
140 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
141 #[stable(feature = "int_error_matching", since = "1.55.0")]
142 #[ferrocene::prevalidated]
143 pub const fn kind(&self) -> &IntErrorKind {
144 &self.kind
145 }
146}
147
148#[stable(feature = "rust1", since = "1.0.0")]
149impl fmt::Display for ParseIntError {
150 #[ferrocene::prevalidated]
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 match self.kind {
153 IntErrorKind::Empty => "cannot parse integer from empty string",
154 IntErrorKind::InvalidDigit => "invalid digit found in string",
155 IntErrorKind::PosOverflow => "number too large to fit in target type",
156 IntErrorKind::NegOverflow => "number too small to fit in target type",
157 IntErrorKind::Zero => "number would be zero for non-zero type",
158 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
159 }
160 .fmt(f)
161 }
162}
163
164#[stable(feature = "rust1", since = "1.0.0")]
165impl Error for ParseIntError {}