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 match self.0 {
27 IntErrorKind::Empty | IntErrorKind::InvalidDigit => unreachable!(),
28 IntErrorKind::PosOverflow => "number too large to fit in target type",
29 IntErrorKind::NegOverflow => "number too small to fit in target type",
30 IntErrorKind::Zero => "number would be zero for non-zero type",
31 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
32 }
33 .fmt(f)
34 }
35}
36
37#[stable(feature = "try_from", since = "1.34.0")]
38impl Error for TryFromIntError {}
39
40#[stable(feature = "try_from", since = "1.34.0")]
41#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
42const impl From<Infallible> for TryFromIntError {
43 #[ferrocene::prevalidated]
44 fn from(x: Infallible) -> TryFromIntError {
45 match x {}
46 }
47}
48
49#[unstable(feature = "never_type", issue = "35121")]
50#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
51const impl From<!> for TryFromIntError {
52 #[inline]
53 #[ferrocene::prevalidated]
54 fn from(never: !) -> TryFromIntError {
55 match never {}
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Eq)]
84#[stable(feature = "rust1", since = "1.0.0")]
85#[ferrocene::prevalidated]
86pub struct ParseIntError {
87 pub(super) kind: IntErrorKind,
88}
89
90#[stable(feature = "int_error_matching", since = "1.55.0")]
103#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
104#[non_exhaustive]
105#[ferrocene::prevalidated]
106pub enum IntErrorKind {
107 #[stable(feature = "int_error_matching", since = "1.55.0")]
111 Empty,
112 #[stable(feature = "int_error_matching", since = "1.55.0")]
120 InvalidDigit,
121 #[stable(feature = "int_error_matching", since = "1.55.0")]
123 PosOverflow,
124 #[stable(feature = "int_error_matching", since = "1.55.0")]
126 NegOverflow,
127 #[stable(feature = "int_error_matching", since = "1.55.0")]
132 Zero,
133 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
140 NotAPowerOfTwo,
142}
143
144impl ParseIntError {
145 #[must_use]
147 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
148 #[stable(feature = "int_error_matching", since = "1.55.0")]
149 #[ferrocene::prevalidated]
150 pub const fn kind(&self) -> &IntErrorKind {
151 &self.kind
152 }
153}
154
155#[stable(feature = "rust1", since = "1.0.0")]
156impl fmt::Display for ParseIntError {
157 #[ferrocene::prevalidated]
158 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159 match self.kind {
160 IntErrorKind::Empty => "cannot parse integer from empty string",
161 IntErrorKind::InvalidDigit => "invalid digit found in string",
162 IntErrorKind::PosOverflow => "number too large to fit in target type",
163 IntErrorKind::NegOverflow => "number too small to fit in target type",
164 IntErrorKind::Zero => "number would be zero for non-zero type",
165 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
166 }
167 .fmt(f)
168 }
169}
170
171#[stable(feature = "rust1", since = "1.0.0")]
172impl Error for ParseIntError {}