Skip to content

Commit afa2fe5

Browse files
committed
Implement 'FromStr' for 'f128';
1 parent 7ad23f4 commit afa2fe5

File tree

1 file changed

+22
-10
lines changed
  • library/core/src/num/dec2flt

1 file changed

+22
-10
lines changed

library/core/src/num/dec2flt/mod.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ pub mod lemire;
108108
pub mod parse;
109109

110110
macro_rules! from_str_float_impl {
111-
($t:ty) => {
112-
#[stable(feature = "rust1", since = "1.0.0")]
113-
impl FromStr for $t {
111+
($($ty:ty)*) => {
112+
$(#[stable(feature = "rust1", since = "1.0.0")]
113+
impl FromStr for $ty {
114114
type Err = ParseFloatError;
115115

116116
/// Converts a string in base 10 to a float.
@@ -151,14 +151,14 @@ macro_rules! from_str_float_impl {
151151
///
152152
/// # Arguments
153153
///
154-
/// * src - A string
154+
/// * `s` - A string
155155
///
156156
/// # Return value
157157
///
158158
/// `Err(ParseFloatError)` if the string did not represent a valid
159159
/// number. Otherwise, `Ok(n)` where `n` is the closest
160160
/// representable floating-point number to the number represented
161-
/// by `src` (following the same rules for rounding as for the
161+
/// by `s` (following the same rules for rounding as for the
162162
/// results of primitive operations).
163163
// We add the `#[inline(never)]` attribute, since its content will
164164
// be filled with that of `dec2flt`, which has #[inline(always)].
@@ -167,17 +167,19 @@ macro_rules! from_str_float_impl {
167167
// generation of `dec2flt`, despite the fact only a maximum of 2
168168
// possible instances can ever exist. Adding #[inline(never)] avoids this.
169169
#[inline(never)]
170-
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
171-
dec2flt(src)
170+
fn from_str(s: &str) -> Result<Self, ParseFloatError> {
171+
dec2flt(s)
172172
}
173-
}
173+
})*
174174
};
175175
}
176176

177177
#[cfg(target_has_reliable_f16)]
178178
from_str_float_impl!(f16);
179179
from_str_float_impl!(f32);
180180
from_str_float_impl!(f64);
181+
#[cfg(target_has_reliable_f128)]
182+
from_str_float_impl!(f128);
181183

182184
// FIXME(f16_f128): A fallback is used when the backend+target does not support f16 well, in order
183185
// to avoid ICEs.
@@ -187,8 +189,18 @@ impl FromStr for f16 {
187189
type Err = ParseFloatError;
188190

189191
#[inline]
190-
fn from_str(_src: &str) -> Result<Self, ParseFloatError> {
191-
unimplemented!("requires target_has_reliable_f16")
192+
fn from_str(_s: &str) -> Result<Self, ParseFloatError> {
193+
unimplemented!("parsing strings as `f16` requires `target_has_reliable_f16`")
194+
}
195+
}
196+
197+
#[cfg(not(target_has_reliable_f128))]
198+
impl FromStr for f128 {
199+
type Err = ParseFloatError;
200+
201+
#[inline]
202+
fn from_str(_s: &str) -> Result<Self, ParseFloatError> {
203+
unimplemented!("parsing strings as `f128` requires `target_has_reliable_f128`")
192204
}
193205
}
194206

0 commit comments

Comments
 (0)