1//! Error types for conversion to integral types.
23use crate::convert::Infallible;
4use crate::error::Error;
5use crate::fmt;
67/// The error type returned when a checked integral type conversion fails.
8#[stable(feature = "try_from", since = "1.34.0")]
9#[derive(#[automatically_derived]
#[stable(feature = "try_from", since = "1.34.0")]
impl crate::fmt::Debug for TryFromIntError {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_tuple_field1_finish(f, "TryFromIntError",
&&self.0)
}
}Debug, #[automatically_derived]
#[stable(feature = "try_from", since = "1.34.0")]
impl crate::marker::Copy for TryFromIntError { }Copy, #[automatically_derived]
#[stable(feature = "try_from", since = "1.34.0")]
impl crate::clone::Clone for TryFromIntError {
#[inline]
fn clone(&self) -> TryFromIntError {
let _: crate::clone::AssertParamIsClone<()>;
*self
}
}Clone, #[automatically_derived]
#[stable(feature = "try_from", since = "1.34.0")]
impl crate::cmp::PartialEq for TryFromIntError {
#[inline]
fn eq(&self, other: &TryFromIntError) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
#[stable(feature = "try_from", since = "1.34.0")]
impl crate::cmp::Eq for TryFromIntError {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) { let _: crate::cmp::AssertParamIsEq<()>; }
}Eq)]
10pub struct TryFromIntError(pub(crate) ());
1112#[stable(feature = "try_from", since = "1.34.0")]
13impl fmt::Displayfor TryFromIntError {
14fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15"out of range integral type conversion attempted".fmt(f)
16 }
17}
1819#[stable(feature = "try_from", since = "1.34.0")]
20impl Errorfor TryFromIntError {}
2122#[stable(feature = "try_from", since = "1.34.0")]
23#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
24impl const From<Infallible> for TryFromIntError {
25fn from(x: Infallible) -> TryFromIntError {
26match x {}
27 }
28}
2930#[unstable(feature = "never_type", issue = "35121")]
31#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
32impl const From<!> for TryFromIntError {
33#[inline]
34fn from(never: !) -> TryFromIntError {
35// Match rather than coerce to make sure that code like
36 // `From<Infallible> for TryFromIntError` above will keep working
37 // when `Infallible` becomes an alias to `!`.
38match never {}
39 }
40}
4142/// An error which can be returned when parsing an integer.
43///
44/// For example, this error is returned by the `from_str_radix()` functions
45/// on the primitive integer types (such as [`i8::from_str_radix`])
46/// and is used as the error type in their [`FromStr`] implementations.
47///
48/// [`FromStr`]: crate::str::FromStr
49///
50/// # Potential causes
51///
52/// Among other causes, `ParseIntError` can be thrown because of leading or trailing whitespace
53/// in the string e.g., when it is obtained from the standard input.
54/// Using the [`str::trim()`] method ensures that no whitespace remains before parsing.
55///
56/// # Example
57///
58/// ```
59/// if let Err(e) = i32::from_str_radix("a12", 10) {
60/// println!("Failed conversion to i32: {e}");
61/// }
62/// ```
63#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl crate::fmt::Debug for ParseIntError {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_struct_field1_finish(f, "ParseIntError",
"kind", &&self.kind)
}
}Debug, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl crate::clone::Clone for ParseIntError {
#[inline]
fn clone(&self) -> ParseIntError {
ParseIntError { kind: crate::clone::Clone::clone(&self.kind) }
}
}Clone, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl crate::cmp::PartialEq for ParseIntError {
#[inline]
fn eq(&self, other: &ParseIntError) -> bool { self.kind == other.kind }
}PartialEq, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl crate::cmp::Eq for ParseIntError {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<IntErrorKind>;
}
}Eq)]
64#[stable(feature = "rust1", since = "1.0.0")]
65pub struct ParseIntError {
66pub(super) kind: IntErrorKind,
67}
6869/// Enum to store the various types of errors that can cause parsing an integer to fail.
70///
71/// # Example
72///
73/// ```
74/// # fn main() {
75/// if let Err(e) = i32::from_str_radix("a12", 10) {
76/// println!("Failed conversion to i32: {:?}", e.kind());
77/// }
78/// # }
79/// ```
80#[stable(feature = "int_error_matching", since = "1.55.0")]
81#[derive(#[automatically_derived]
#[stable(feature = "int_error_matching", since = "1.55.0")]
impl crate::fmt::Debug for IntErrorKind {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::write_str(f,
match self {
IntErrorKind::Empty => "Empty",
IntErrorKind::InvalidDigit => "InvalidDigit",
IntErrorKind::PosOverflow => "PosOverflow",
IntErrorKind::NegOverflow => "NegOverflow",
IntErrorKind::Zero => "Zero",
})
}
}Debug, #[automatically_derived]
#[stable(feature = "int_error_matching", since = "1.55.0")]
impl crate::clone::Clone for IntErrorKind {
#[inline]
fn clone(&self) -> IntErrorKind { *self }
}Clone, #[automatically_derived]
#[stable(feature = "int_error_matching", since = "1.55.0")]
impl crate::cmp::PartialEq for IntErrorKind {
#[inline]
fn eq(&self, other: &IntErrorKind) -> bool {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
#[stable(feature = "int_error_matching", since = "1.55.0")]
impl crate::cmp::Eq for IntErrorKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
#[stable(feature = "int_error_matching", since = "1.55.0")]
impl crate::marker::Copy for IntErrorKind { }Copy, #[automatically_derived]
#[stable(feature = "int_error_matching", since = "1.55.0")]
impl crate::hash::Hash for IntErrorKind {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = crate::intrinsics::discriminant_value(self);
crate::hash::Hash::hash(&__self_discr, state)
}
}Hash)]
82#[non_exhaustive]
83pub enum IntErrorKind {
84/// Value being parsed is empty.
85 ///
86 /// This variant will be constructed when parsing an empty string.
87#[stable(feature = "int_error_matching", since = "1.55.0")]
88Empty,
89/// Contains an invalid digit in its context.
90 ///
91 /// Among other causes, this variant will be constructed when parsing a string that
92 /// contains a non-ASCII char.
93 ///
94 /// This variant is also constructed when a `+` or `-` is misplaced within a string
95 /// either on its own or in the middle of a number.
96#[stable(feature = "int_error_matching", since = "1.55.0")]
97InvalidDigit,
98/// Integer is too large to store in target integer type.
99#[stable(feature = "int_error_matching", since = "1.55.0")]
100PosOverflow,
101/// Integer is too small to store in target integer type.
102#[stable(feature = "int_error_matching", since = "1.55.0")]
103NegOverflow,
104/// Value was Zero
105 ///
106 /// This variant will be emitted when the parsing string has a value of zero, which
107 /// would be illegal for non-zero types.
108#[stable(feature = "int_error_matching", since = "1.55.0")]
109Zero,
110}
111112impl ParseIntError {
113/// Outputs the detailed cause of parsing an integer failing.
114#[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")]
117pub const fn kind(&self) -> &IntErrorKind {
118&self.kind
119 }
120}
121122#[stable(feature = "rust1", since = "1.0.0")]
123impl fmt::Displayfor ParseIntError {
124fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125match 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}
135136#[stable(feature = "rust1", since = "1.0.0")]
137impl Errorfor ParseIntError {}