Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the Copy trait #284

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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<Output = Self> + PartialOrd + Copy {
pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Clone {
/// Returns positive infinity.
///
/// # Examples
Expand Down Expand Up @@ -216,7 +216,7 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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.
Expand Down Expand Up @@ -316,8 +316,8 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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()
Expand Down Expand Up @@ -350,8 +350,8 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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()
Expand Down Expand Up @@ -385,16 +385,16 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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 {
self - f
} else {
self - f + one
}
} else if -f < h {
} else if -f.clone() < h {
self - f
} else {
self - f - one
Expand Down Expand Up @@ -425,8 +425,8 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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
Expand Down Expand Up @@ -486,10 +486,10 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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()
Expand Down Expand Up @@ -520,7 +520,7 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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()
Expand Down Expand Up @@ -605,10 +605,10 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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 {
Expand Down Expand Up @@ -639,10 +639,10 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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 {
Expand All @@ -661,7 +661,7 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
/// use std::{f32, f64};
///
/// fn check<T: FloatCore>(x: T, y: T) {
/// assert!(x.recip() == y);
/// assert!(x.clone().recip() == y);
/// assert!(y.recip() == x);
/// }
///
Expand Down Expand Up @@ -703,7 +703,7 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + 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.
Expand Down Expand Up @@ -905,7 +905,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<Output = Self> {
pub trait Float: Num + Clone + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the `NaN` value.
///
/// ```
Expand Down Expand Up @@ -1862,7 +1862,7 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
/// 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()
Expand Down Expand Up @@ -2396,25 +2396,25 @@ mod tests {

#[cfg(any(feature = "std", feature = "libm"))]
fn test_copysign_generic<F: crate::float::Float + ::core::fmt::Debug>(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<F: crate::float::Float + ::core::fmt::Debug>() {
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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Output = Self> {
pub trait Real: Num + NumCast + PartialOrd + Neg<Output = Self> {
/// Returns the smallest finite value that this type can represent.
///
/// ```
Expand Down