Skip to content

Commit 0d87660

Browse files
committed
Remove impl From<{Rational,SRational}> for {f32,f64}.
The From trait implies perfect conversions but these may lose information.
1 parent dfd6de4 commit 0d87660

File tree

3 files changed

+20
-38
lines changed

3 files changed

+20
-38
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
0.6
22

33
- Rust 1.60 or later is now required to build this package.
4+
- From<Rational> and From<SRational> implementations for f32 and f64
5+
have been removed. Use Rational::to_f64 and so on.
46

57
0.5.5 (2022-10-22)
68

src/doc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ pub mod news {}
3636
///
3737
/// ### API compatibilities
3838
///
39+
/// * `From<Rational>` and `From<SRational>` implementations for `f32`
40+
/// and `f64` have been removed.
41+
/// Use `Rational::to_f64` and so on.
42+
///
3943
/// ## Upgrade from 0.4.x to 0.5.x
4044
///
4145
/// ### API compatibilities

src/value.rs

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,6 @@ impl fmt::Display for Rational {
383383
}
384384
}
385385

386-
// This implementation has been deprecated. Use Rational::to_f64 instead.
387-
impl From<Rational> for f64 {
388-
#[inline]
389-
fn from(r: Rational) -> f64 { r.to_f64() }
390-
}
391-
392-
// This implementation has been deprecated. Use Rational::to_f32 instead.
393-
impl From<Rational> for f32 {
394-
#[inline]
395-
fn from(r: Rational) -> f32 { r.to_f32() }
396-
}
397-
398386
/// A signed rational number, which is a pair of 32-bit signed integers.
399387
#[derive(Copy, Clone)]
400388
pub struct SRational { pub num: i32, pub denom: i32 }
@@ -434,18 +422,6 @@ impl fmt::Display for SRational {
434422
}
435423
}
436424

437-
// This implementation has been deprecated. Use SRational::to_f64 instead.
438-
impl From<SRational> for f64 {
439-
#[inline]
440-
fn from(r: SRational) -> f64 { r.to_f64() }
441-
}
442-
443-
// This implementation has been deprecated. Use SRational::to_f32 instead.
444-
impl From<SRational> for f32 {
445-
#[inline]
446-
fn from(r: SRational) -> f32 { r.to_f32() }
447-
}
448-
449425
// Only u32 or i32 are expected for T.
450426
fn fmt_rational_sub<T>(f: &mut fmt::Formatter, num: u32, denom: T)
451427
-> String where T: fmt::Display {
@@ -974,30 +950,30 @@ mod tests {
974950
#[test]
975951
fn ratioanl_f64() {
976952
use std::{f64, u32};
977-
assert_eq!(f64::from(Rational::from((1, 2))), 0.5);
978-
assert_eq!(f64::from(Rational::from((1, u32::MAX))),
953+
assert_eq!(Rational::from((1, 2)).to_f64(), 0.5);
954+
assert_eq!(Rational::from((1, u32::MAX)).to_f64(),
979955
2.3283064370807974e-10);
980-
assert_eq!(f64::from(Rational::from((u32::MAX, 1))),
956+
assert_eq!(Rational::from((u32::MAX, 1)).to_f64(),
981957
u32::MAX as f64);
982-
assert_eq!(f64::from(Rational::from((u32::MAX - 1, u32::MAX))),
958+
assert_eq!(Rational::from((u32::MAX - 1, u32::MAX)).to_f64(),
983959
0.9999999997671694);
984-
assert_eq!(f64::from(Rational::from((u32::MAX, u32::MAX - 1))),
960+
assert_eq!(Rational::from((u32::MAX, u32::MAX - 1)).to_f64(),
985961
1.0000000002328306);
986-
assert_eq!(f64::from(Rational::from((1, 0))), f64::INFINITY);
987-
assert!(f64::from(Rational::from((0, 0))).is_nan());
962+
assert_eq!(Rational::from((1, 0)).to_f64(), f64::INFINITY);
963+
assert!(Rational::from((0, 0)).to_f64().is_nan());
988964

989-
assert_eq!(f64::from(SRational::from((1, 2))), 0.5);
990-
assert_eq!(f64::from(SRational::from((-1, 2))), -0.5);
991-
assert_eq!(f64::from(SRational::from((1, -2))), -0.5);
992-
assert_eq!(f64::from(SRational::from((-1, -2))), 0.5);
993-
assert_eq!(f64::from(SRational::from((1, 0))), f64::INFINITY);
994-
assert_eq!(f64::from(SRational::from((-1, 0))), f64::NEG_INFINITY);
965+
assert_eq!(SRational::from((1, 2)).to_f64(), 0.5);
966+
assert_eq!(SRational::from((-1, 2)).to_f64(), -0.5);
967+
assert_eq!(SRational::from((1, -2)).to_f64(), -0.5);
968+
assert_eq!(SRational::from((-1, -2)).to_f64(), 0.5);
969+
assert_eq!(SRational::from((1, 0)).to_f64(), f64::INFINITY);
970+
assert_eq!(SRational::from((-1, 0)).to_f64(), f64::NEG_INFINITY);
995971
}
996972

997973
#[test]
998974
fn rational_f32() {
999975
// If num and demon are converted to f32 before the division,
1000976
// the precision is lost in this example.
1001-
assert_eq!(f32::from(Rational::from((1, 16777217))), 5.960464e-8);
977+
assert_eq!(Rational::from((1, 16777217)).to_f32(), 5.960464e-8);
1002978
}
1003979
}

0 commit comments

Comments
 (0)