1use crate::error::Error;
4use crate::fmt;
5use crate::num::imp::dec2flt;
6use crate::str::FromStr;
7
8macro_rules! from_str_float_impl {
9 ($t:ty) => {
10 #[stable(feature = "rust1", since = "1.0.0")]
11 impl FromStr for $t {
12 type Err = ParseFloatError;
13
14 #[inline(never)]
68 fn from_str(src: &str) -> Result<Self, ParseFloatError> {
69 dec2flt::dec2flt(src)
70 }
71 }
72 };
73}
74
75#[cfg(target_has_reliable_f16)]
76#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for f16 {
type Err = ParseFloatError;
#[inline(never)]
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
dec2flt::dec2flt(src)
}
}from_str_float_impl!(f16);
77#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for f32 {
type Err = ParseFloatError;
#[inline(never)]
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
dec2flt::dec2flt(src)
}
}from_str_float_impl!(f32);
78#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for f64 {
type Err = ParseFloatError;
#[inline(never)]
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
dec2flt::dec2flt(src)
}
}from_str_float_impl!(f64);
79
80#[cfg(not(target_has_reliable_f16))]
84#[expect(ineffective_unstable_trait_impl, reason = "stable trait on unstable type")]
85#[unstable(feature = "f16", issue = "116909")]
86impl FromStr for f16 {
87 type Err = ParseFloatError;
88
89 #[inline]
90 fn from_str(_src: &str) -> Result<Self, ParseFloatError> {
91 unimplemented!("requires target_has_reliable_f16")
92 }
93}
94
95#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::fmt::Debug for ParseFloatError {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_struct_field1_finish(f,
"ParseFloatError kind", &&self.kind)
}
}Debug, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::clone::Clone for ParseFloatError {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
ParseFloatError { kind: crate::clone::Clone::clone(&self.kind) }
}
}Clone, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::marker::StructuralPartialEq for ParseFloatError { }
#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::cmp::PartialEq for ParseFloatError {
#[inline]
fn eq(&self, other: &Self) -> bool { self.kind == other.kind }
}PartialEq, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::cmp::Eq for ParseFloatError {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<FloatErrorKind>;
}
}Eq)]
110#[stable(feature = "rust1", since = "1.0.0")]
111pub struct ParseFloatError {
112 pub(super) kind: FloatErrorKind,
113}
114
115#[derive(#[automatically_derived]#[unstable(feature = "extended_float_error_kind",
issue = "164793")]
impl crate::fmt::Debug for FloatErrorKind {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::write_str(f,
match self {
FloatErrorKind::Empty => "Empty",
FloatErrorKind::Invalid => "Invalid",
})
}
}Debug, #[automatically_derived]#[doc(hidden)]#[unstable(feature =
"extended_float_error_kind", issue = "164793")]
unsafe impl crate::clone::TrivialClone for FloatErrorKind { }
#[automatically_derived]#[unstable(feature = "extended_float_error_kind",
issue = "164793")]
impl crate::clone::Clone for FloatErrorKind {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]#[unstable(feature = "extended_float_error_kind",
issue = "164793")]
impl crate::marker::StructuralPartialEq for FloatErrorKind { }
#[automatically_derived]#[unstable(feature = "extended_float_error_kind",
issue = "164793")]
impl crate::cmp::PartialEq for FloatErrorKind {
#[inline]
fn eq(&self, other: &Self) -> bool {
crate::intrinsics::discriminant_value(self) ==
crate::intrinsics::discriminant_value(other)
}
}PartialEq, #[automatically_derived]#[unstable(feature = "extended_float_error_kind",
issue = "164793")]
impl crate::cmp::Eq for FloatErrorKind { }Eq, #[automatically_derived]#[unstable(feature = "extended_float_error_kind",
issue = "164793")]
impl crate::marker::Copy for FloatErrorKind { }Copy, #[automatically_derived]#[unstable(feature = "extended_float_error_kind",
issue = "164793")]
impl crate::hash::Hash for FloatErrorKind {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&crate::intrinsics::discriminant_value(self),
state)
}
}Hash)]
132#[unstable(feature = "extended_float_error_kind", issue = "164793")]
133#[non_exhaustive]
134pub enum FloatErrorKind {
135 #[unstable(feature = "extended_float_error_kind", issue = "164793")]
137 Empty,
138 #[unstable(feature = "extended_float_error_kind", issue = "164793")]
140 Invalid,
141}
142
143impl ParseFloatError {
144 #[must_use]
146 #[inline]
147 #[rustc_const_unstable(feature = "extended_float_error_kind", issue = "164793")]
148 #[unstable(feature = "extended_float_error_kind", issue = "164793")]
149 pub const fn kind(&self) -> FloatErrorKind {
150 self.kind
151 }
152}
153
154#[stable(feature = "rust1", since = "1.0.0")]
155impl Error for ParseFloatError {}
156
157#[stable(feature = "rust1", since = "1.0.0")]
158impl fmt::Display for ParseFloatError {
159 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160 match self.kind {
161 FloatErrorKind::Empty => "cannot parse float from empty string",
162 FloatErrorKind::Invalid => "invalid float literal",
163 }
164 .fmt(f)
165 }
166}