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<IntErrorKind>;
*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<IntErrorKind>;
}
}Eq)]
10pub struct TryFromIntError(pub(crate) IntErrorKind);
1112impl TryFromIntError {
13/// Outputs the detailed cause of converting an integer failing.
14#[must_use]
15 #[unstable(feature = "try_from_int_error_kind", issue = "153978")]
16pub const fn kind(&self) -> &IntErrorKind {
17&self.0
18}
19}
2021#[stable(feature = "try_from", since = "1.34.0")]
22impl fmt::Displayfor TryFromIntError {
23fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24"out of range integral type conversion attempted".fmt(f)
25 }
26}
2728#[stable(feature = "try_from", since = "1.34.0")]
29impl Errorfor TryFromIntError {}
3031#[stable(feature = "try_from", since = "1.34.0")]
32#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
33impl const From<Infallible> for TryFromIntError {
34fn from(x: Infallible) -> TryFromIntError {
35match x {}
36 }
37}
3839#[unstable(feature = "never_type", issue = "35121")]
40#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
41impl const From<!> for TryFromIntError {
42#[inline]
43fn from(never: !) -> TryFromIntError {
44// Match rather than coerce to make sure that code like
45 // `From<Infallible> for TryFromIntError` above will keep working
46 // when `Infallible` becomes an alias to `!`.
47match never {}
48 }
49}
5051/// An error which can be returned when parsing an integer.
52///
53/// For example, this error is returned by the `from_str_radix()` functions
54/// on the primitive integer types (such as [`i8::from_str_radix`])
55/// and is used as the error type in their [`FromStr`] implementations.
56///
57/// [`FromStr`]: crate::str::FromStr
58///
59/// # Potential causes
60///
61/// Among other causes, `ParseIntError` can be thrown because of leading or trailing whitespace
62/// in the string e.g., when it is obtained from the standard input.
63/// Using the [`str::trim()`] method ensures that no whitespace remains before parsing.
64///
65/// # Example
66///
67/// ```
68/// if let Err(e) = i32::from_str_radix("a12", 10) {
69/// println!("Failed conversion to i32: {e}");
70/// }
71/// ```
72#[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)]
73#[stable(feature = "rust1", since = "1.0.0")]
74pub struct ParseIntError {
75pub(super) kind: IntErrorKind,
76}
7778/// Enum to store the various types of errors that can cause parsing or converting an
79/// integer to fail.
80///
81/// # Example
82///
83/// ```
84/// # fn main() {
85/// if let Err(e) = i32::from_str_radix("a12", 10) {
86/// println!("Failed conversion to i32: {:?}", e.kind());
87/// }
88/// # }
89/// ```
90#[stable(feature = "int_error_matching", since = "1.55.0")]
91#[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",
IntErrorKind::NotAPowerOfTwo => "NotAPowerOfTwo",
})
}
}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)]
92#[non_exhaustive]
93pub enum IntErrorKind {
94/// Value being parsed is empty.
95 ///
96 /// This variant will be constructed when parsing an empty string.
97#[stable(feature = "int_error_matching", since = "1.55.0")]
98Empty,
99/// Contains an invalid digit in its context.
100 ///
101 /// Among other causes, this variant will be constructed when parsing a string that
102 /// contains a non-ASCII char.
103 ///
104 /// This variant is also constructed when a `+` or `-` is misplaced within a string
105 /// either on its own or in the middle of a number.
106#[stable(feature = "int_error_matching", since = "1.55.0")]
107InvalidDigit,
108/// Integer is too large to store in target integer type.
109#[stable(feature = "int_error_matching", since = "1.55.0")]
110PosOverflow,
111/// Integer is too small to store in target integer type.
112#[stable(feature = "int_error_matching", since = "1.55.0")]
113NegOverflow,
114/// Value was Zero
115 ///
116 /// This variant will be emitted when the parsing string or the converting integer
117 /// has a value of zero, which would be illegal for non-zero types.
118#[stable(feature = "int_error_matching", since = "1.55.0")]
119Zero,
120/// Value is not a power of two.
121 ///
122 /// This variant will be emitted when converting an integer that is not a power of
123 /// two. This is required in some cases such as constructing an [`Alignment`].
124 ///
125 /// [`Alignment`]: core::mem::Alignment "mem::Alignment"
126#[unstable(feature = "try_from_int_error_kind", issue = "153978")]
127// Also, #[unstable(feature = "ptr_alignment_type", issue = "102070")]
128NotAPowerOfTwo,
129}
130131impl ParseIntError {
132/// Outputs the detailed cause of parsing an integer failing.
133#[must_use]
134 #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
135 #[stable(feature = "int_error_matching", since = "1.55.0")]
136pub const fn kind(&self) -> &IntErrorKind {
137&self.kind
138 }
139}
140141#[stable(feature = "rust1", since = "1.0.0")]
142impl fmt::Displayfor ParseIntError {
143fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144match self.kind {
145 IntErrorKind::Empty => "cannot parse integer from empty string",
146 IntErrorKind::InvalidDigit => "invalid digit found in string",
147 IntErrorKind::PosOverflow => "number too large to fit in target type",
148 IntErrorKind::NegOverflow => "number too small to fit in target type",
149 IntErrorKind::Zero => "number would be zero for non-zero type",
150 IntErrorKind::NotAPowerOfTwo => "number is not a power of two",
151 }
152 .fmt(f)
153 }
154}
155156#[stable(feature = "rust1", since = "1.0.0")]
157impl Errorfor ParseIntError {}