Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.

Commit 7a69d35

Browse files
author
Mihai Budiu
authored
FromStr for floats (#214)
* FromStr for floats Signed-off-by: Mihai Budiu <[email protected]> * Address review comments Signed-off-by: Mihai Budiu <[email protected]> Signed-off-by: Mihai Budiu <[email protected]>
1 parent 0834e04 commit 7a69d35

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/algebra/floats.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use size_of::SizeOf;
44
use std::{
55
fmt::{self, Debug, Display},
66
iter::{Product, Sum},
7+
num::ParseFloatError,
78
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
9+
str::FromStr,
810
};
911

1012
#[cfg(feature = "with-serde")]
@@ -361,6 +363,15 @@ macro_rules! float {
361363
Display::fmt(&self.0.0, f)
362364
}
363365
}
366+
367+
impl FromStr for $outer {
368+
type Err = ParseFloatError;
369+
370+
#[inline]
371+
fn from_str(src: &str) -> Result<$outer, ParseFloatError> {
372+
$inner::from_str(src).map($outer::new)
373+
}
374+
}
364375
)*
365376
};
366377
}
@@ -384,3 +395,10 @@ impl From<F64> for F32 {
384395
Self::new(float.into_inner() as f32)
385396
}
386397
}
398+
399+
#[test]
400+
fn fromstr() {
401+
assert_eq!(Ok(F32::new(10.0)), F32::from_str("10"));
402+
assert_eq!(Ok(F64::new(-10.0)), F64::from_str("-10"));
403+
assert!(F32::from_str("what").is_err());
404+
}

0 commit comments

Comments
 (0)