Skip to content

Commit

Permalink
Fix broken Copy and Clone derive for tolerance struct.
Browse files Browse the repository at this point in the history
Copy and Clone are implemented explicitly since deriving these
traits doesn't work because of the generics used.
See related issue: rust-lang/rust#108894
  • Loading branch information
se-mo committed Aug 15, 2023
1 parent 3c1d960 commit e98d29c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
4 changes: 2 additions & 2 deletions nearly-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub fn nearly_eq_ulps_derive(input: TokenStream) -> TokenStream {
/// use nearly::NearlyEq;
/// use nearly::assert_nearly;
///
/// #[derive(NearlyEq, Debug, Copy, Clone)]
/// #[derive(NearlyEq, Debug)]
/// struct Point {
/// x: f32,
/// y: f32,
Expand Down Expand Up @@ -259,7 +259,7 @@ pub fn nearly_eq_ulps_derive(input: TokenStream) -> TokenStream {
/// use nearly::NearlyEq;
/// use nearly::assert_nearly;
///
/// #[derive(NearlyEq, Debug, Copy, Clone)]
/// #[derive(NearlyEq, Debug)]
/// struct Point {
/// x: f32,
/// y: f64,
Expand Down
39 changes: 38 additions & 1 deletion nearly/src/tolerance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub type ToleranceF64 = Tolerance<f64>;
/// This data type combines an absolute epsilon value that will be used for comparisons based on
/// absolute epsilon values and an ulps value that will be used for comparisons based on
/// ulps values.
#[derive(Clone, Copy, Debug)]
#[derive(Debug)]
pub struct Tolerance<Lhs, Rhs = Lhs>
where
Lhs: ?Sized + EpsAndUlpsTolerance<Rhs>,
Expand Down Expand Up @@ -144,6 +144,27 @@ where
}
}

// We have to implement Copy and Clone explicitly, since derive
// of these traits doesn't work because of the generics used.
// See related issue: https://github.com/rust-lang/rust/issues/108894

impl<Lhs, Rhs> Clone for Tolerance<Lhs, Rhs>
where
Lhs: ?Sized + EpsAndUlpsTolerance<Rhs>,
Rhs: ?Sized,
{
fn clone(&self) -> Self {
*self
}
}

impl<Lhs, Rhs> Copy for Tolerance<Lhs, Rhs>
where
Lhs: ?Sized + EpsAndUlpsTolerance<Rhs>,
Rhs: ?Sized,
{
}

#[cfg(test)]
mod tests {
use super::{EpsToleranceType, Tolerance, UlpsToleranceType};
Expand Down Expand Up @@ -207,4 +228,20 @@ mod tests {
assert_eq!(tuple.0, 0.01);
assert_eq!(tuple.1, 5);
}

#[test]
fn clone_f32() {
let tolerance = Tolerance::<f32>::new(0.01, 5);
let tolerance_2 = tolerance.clone();
assert_eq!(tolerance.eps, tolerance_2.eps);
assert_eq!(tolerance.ulps, tolerance_2.ulps);
}

#[test]
fn clone_f64() {
let tolerance = Tolerance::<f64>::new(0.01, 5);
let tolerance_2 = tolerance.clone();
assert_eq!(tolerance.eps, tolerance_2.eps);
assert_eq!(tolerance.ulps, tolerance_2.ulps);
}
}
18 changes: 9 additions & 9 deletions nearly/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ mod nearly_eq_ulps {
mod nearly_eq {
use nearly::{nearly, NearlyEq, Tolerance};

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
struct NamedStructSameType {
x: f32,
y: f32,
Expand Down Expand Up @@ -394,7 +394,7 @@ mod nearly_eq {
assert!(nearly!(a != c));
}

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
struct UnnamedStructSameType(f32, f32, f32);

#[test]
Expand Down Expand Up @@ -425,13 +425,13 @@ mod nearly_eq {
assert!(nearly!(a != c));
}

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
struct NamedPair {
a: f32,
b: f64,
}

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
struct NamedStructDifferentType {
x: f32,
y: f64,
Expand Down Expand Up @@ -487,10 +487,10 @@ mod nearly_eq {
assert!(nearly!(a != c));
}

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
struct UnnamedPair(f32, f64);

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
struct UnnamedStructDifferentType(f32, f64, UnnamedPair);

#[test]
Expand Down Expand Up @@ -532,7 +532,7 @@ mod nearly_eq {
assert!(nearly!(a != c));
}

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
struct UnitStruct;

#[test]
Expand All @@ -547,7 +547,7 @@ mod nearly_eq {
assert!(nearly!(a == b));
}

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
enum EnumSameType {
X(f32),
Y,
Expand Down Expand Up @@ -618,7 +618,7 @@ mod nearly_eq {
assert!(nearly!(d != f));
}

#[derive(NearlyEq, Clone, Copy)]
#[derive(NearlyEq)]
enum EnumDifferentType {
X(f64),
Y,
Expand Down

0 comments on commit e98d29c

Please sign in to comment.