From 3a538b0c5515778a7534c1bce5b737b82fc95284 Mon Sep 17 00:00:00 2001 From: Luca Ciucci Date: Fri, 18 Aug 2023 14:21:09 +0200 Subject: [PATCH 1/2] Remove the `Copy` trait Relevan issue: #245 --- src/real.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/real.rs b/src/real.rs index d4feee0f..0b1c574b 100644 --- a/src/real.rs +++ b/src/real.rs @@ -14,7 +14,7 @@ use crate::{Float, Num, NumCast}; /// for a list of data types that could meaningfully implement this trait. /// /// This trait is only available with the `std` feature, or with the `libm` feature otherwise. -pub trait Real: Num + Copy + NumCast + PartialOrd + Neg { +pub trait Real: Num + NumCast + PartialOrd + Neg { /// Returns the smallest finite value that this type can represent. /// /// ``` From 34fa48715d389d4129fd25217baed0e094c41f88 Mon Sep 17 00:00:00 2001 From: Luca Ciucci Date: Fri, 18 Aug 2023 14:36:47 +0200 Subject: [PATCH 2/2] Relax `Copy` to `Clone` --- src/float.rs | 64 ++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/float.rs b/src/float.rs index 87f8387b..64f87563 100644 --- a/src/float.rs +++ b/src/float.rs @@ -9,7 +9,7 @@ use crate::{Num, NumCast, ToPrimitive}; /// Generic trait for floating point numbers that works with `no_std`. /// /// This trait implements a subset of the `Float` trait. -pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { +pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Clone { /// Returns positive infinity. /// /// # Examples @@ -215,7 +215,7 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn is_finite(self) -> bool { - !(self.is_nan() || self.is_infinite()) + !(self.clone().is_nan() || self.is_infinite()) } /// Returns `true` if the number is neither zero, infinite, subnormal or NaN. @@ -315,8 +315,8 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn floor(self) -> Self { - let f = self.fract(); - if f.is_nan() || f.is_zero() { + let f = self.clone().fract(); + if f.clone().is_nan() || f.clone().is_zero() { self } else if self < Self::zero() { self - f - Self::one() @@ -349,8 +349,8 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn ceil(self) -> Self { - let f = self.fract(); - if f.is_nan() || f.is_zero() { + let f = self.clone().fract(); + if f.clone().is_nan() || f.clone().is_zero() { self } else if self > Self::zero() { self - f + Self::one() @@ -384,8 +384,8 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { fn round(self) -> Self { let one = Self::one(); let h = Self::from(0.5).expect("Unable to cast from 0.5"); - let f = self.fract(); - if f.is_nan() || f.is_zero() { + let f = self.clone().fract(); + if f.clone().is_nan() || f.clone().is_zero() { self } else if self > Self::zero() { if f < h { @@ -393,7 +393,7 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { } else { self - f + one } - } else if -f < h { + } else if -f.clone() < h { self - f } else { self - f - one @@ -424,8 +424,8 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn trunc(self) -> Self { - let f = self.fract(); - if f.is_nan() { + let f = self.clone().fract(); + if f.clone().is_nan() { self } else { self - f @@ -485,10 +485,10 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn abs(self) -> Self { - if self.is_sign_positive() { + if self.clone().is_sign_positive() { return self; } - if self.is_sign_negative() { + if self.clone().is_sign_negative() { return -self; } Self::nan() @@ -519,7 +519,7 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn signum(self) -> Self { - if self.is_nan() { + if self.clone().is_nan() { Self::nan() } else if self.is_sign_negative() { -Self::one() @@ -604,10 +604,10 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn min(self, other: Self) -> Self { - if self.is_nan() { + if self.clone().is_nan() { return other; } - if other.is_nan() { + if other.clone().is_nan() { return self; } if self < other { @@ -638,10 +638,10 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// ``` #[inline] fn max(self, other: Self) -> Self { - if self.is_nan() { + if self.clone().is_nan() { return other; } - if other.is_nan() { + if other.clone().is_nan() { return self; } if self > other { @@ -660,7 +660,7 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { /// use std::{f32, f64}; /// /// fn check(x: T, y: T) { - /// assert!(x.recip() == y); + /// assert!(x.clone().recip() == y); /// assert!(y.recip() == x); /// } /// @@ -702,7 +702,7 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { // It should always be possible to convert a positive `i32` to a `usize`. // Note, `i32::MIN` will wrap and still be negative, so we need to convert // to `u32` without sign-extension before growing to `usize`. - super::pow(self, (exp as u32).to_usize().unwrap()) + super::pow(self.clone(), (exp as u32).to_usize().unwrap()) } /// Converts to degrees, assuming the number is in radians. @@ -904,7 +904,7 @@ impl FloatCore for f64 { /// /// This trait is only available with the `std` feature, or with the `libm` feature otherwise. #[cfg(any(feature = "std", feature = "libm"))] -pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { +pub trait Float: Num + Clone + NumCast + PartialOrd + Neg { /// Returns the `NaN` value. /// /// ``` @@ -1861,7 +1861,7 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { /// assert!(f32::nan().copysign(1.0).is_nan()); /// ``` fn copysign(self, sign: Self) -> Self { - if self.is_sign_negative() == sign.is_sign_negative() { + if self.clone().is_sign_negative() == sign.is_sign_negative() { self } else { self.neg() @@ -2312,25 +2312,25 @@ mod tests { #[cfg(any(feature = "std", feature = "libm"))] fn test_copysign_generic(p: F, n: F, nan: F) { - assert!(p.is_sign_positive()); - assert!(n.is_sign_negative()); - assert!(nan.is_nan()); - assert!(!nan.is_subnormal()); + assert!(p.clone().is_sign_positive()); + assert!(n.clone().is_sign_negative()); + assert!(nan.clone().is_nan()); + assert!(!nan.clone().is_subnormal()); - assert_eq!(p, p.copysign(p)); - assert_eq!(p.neg(), p.copysign(n)); + assert_eq!(p.clone(), p.clone().copysign(p.clone())); + assert_eq!(p.clone().neg(), p.clone().copysign(n.clone())); - assert_eq!(n, n.copysign(n)); - assert_eq!(n.neg(), n.copysign(p)); + assert_eq!(n.clone(), n.clone().copysign(n.clone())); + assert_eq!(n.clone().neg(), n.clone().copysign(p.clone())); - assert!(nan.copysign(p).is_sign_positive()); + assert!(nan.clone().copysign(p).is_sign_positive()); assert!(nan.copysign(n).is_sign_negative()); } #[cfg(any(feature = "std", feature = "libm"))] fn test_subnormal() { let min_positive = F::min_positive_value(); - let lower_than_min = min_positive / F::from(2.0f32).unwrap(); + let lower_than_min = min_positive.clone() / F::from(2.0f32).unwrap(); assert!(!min_positive.is_subnormal()); assert!(lower_than_min.is_subnormal()); }