This crate provides derive macros for the AbsDiffEq and RelativeEq traits of the approx crate.
These derive macros only implement both traits with ...<Rhs = Self>.
The macros infer the EPSILON type of the [AbsDiffEq] trait by looking
at the type of the first struct or enum field or any type specified by the user.
This table lists all attributes which can be used to customize the derived traits.
They are ordered in descending priority, meaning setting the #[approx(equal)] will overwrite
any specifications made in the #[approx(map = ...)] attribute.
| Field Attribute | Functionality |
|---|---|
#[approx(skip)] |
Skips the field entirely |
#[approx(equal)] |
Checks this field with == for Equality |
#[approx(cast_field)] |
Casts the field with .. as .. syntax. |
#[approx(cast_value)] |
Casts the epsilon value with .. as .. syntax. |
#[approx(map = ..)] |
Maps values before comparing them. |
#[approx(epsilon_map = ..)] |
Maps epsilon values before using them. |
#[approx(static_epsilon = ..)] |
Defines a static epsilon value for this particular field. |
#[approx(into_iter)] |
Tries to use the into_iterator method to compare fields. |
| Object Attribute | |
#[approx(default_epsilon = ...)] |
Sets the default epsilon value |
#[approx(default_max_relative = ...)] |
Sets the default max_relative value. |
#[approx(epsilon_type = ...)] |
Sets the type of the epsilon value |
Usage
# use *;
use AbsDiffEq;
// Define a new type and derive the AbsDiffEq trait
// Compare if two given positions match
// with respect to geiven epsilon.
let p1 = Position ;
let p2 = Position ;
assert_abs_diff_eq!;
In this case, the generated code looks something like this:
const _ : () =
{
#[automatically_derived] impl approx :: AbsDiffEq for Position
{
type Epsilon = <f64 as approx::AbsDiffEq>::Epsilon;
fn default_epsilon() -> Self :: Epsilon {
<f64 as approx::AbsDiffEq>::default_epsilon()
}
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
<f64 as approx::AbsDiffEq>::abs_diff_eq(
&self.x,
& other.x,
epsilon.clone()
) &&
<f64 as approx::AbsDiffEq>::abs_diff_eq(
&self.y,
&other.y,
epsilon.clone()
) && true
}
}
};
The [AbsDiffEq] derive macro calls the abs_diff_eq method repeatedly on all fields
to determine if all are matching.
Enums
Since approx-derive supports enums since 0.2
# use *;
use AbsDiffEq;
let p1 = Smooth ;
let p2 = Smooth ;
let p3 = Lattice ;
assert_abs_diff_eq!;
# use approx::*;
# use approx_derive::AbsDiffEq;
# #[derive(AbsDiffEq, PartialEq, Debug)]
# enum Position {
# Smooth { x: f32, y: f32, },
# #[approx(cast_value)]
# Lattice { x: isize, y: isize },
# }
# let p1 = Position::Smooth { x: 1.0, y: 1.1 };
# let p3 = Position::Lattice { x: 1, y: 1 };
// Note! Different enum variants can never be equal!
assert_abs_diff_eq!(p1, p3, epsilon = 1000.0);
Field Attributes
Skipping Fields
Sometimes, we only want to compare certain fields and omit others completely.
# use *;
# use *;
let player1 = Player ;
let player2 = Player ;
assert_abs_diff_eq!;
Testing for Equality
When identical equality is desired, we can specify this with the #[approx(equal)] attribute.
# use *;
# use *;
Note that in this case, the type of the epsilon value for the implementation of
AbsDiffEq is inferred from the
first field of the Prediction struct.
This means if we reorder the arguments of the struct, we need to manually set the epsilon type.
# use *;
Casting Fields
Structs which consist of multiple fields with different numeric types, can not be derived without additional hints. After all, we should specify how this type mismatch will be handled.
# use approx::*;
# use approx_derive::*;
#[derive(AbsDiffEq, PartialEq, Debug)]
struct MyStruct {
v1: f32,
v2: f64,
}
We can use the #[approx(cast_field)] and #[approx(cast_value)]
attributes to achieve this goal.
Example 1
Here, the second field will be casted to the type of the inferred epsilon value (f32).
We can check this by testing if a change in the size of f64::MIN_POSITIVE would get lost by
this procedure.
# use *;
# use *;
#
#
let ms1 = MyStruct ;
let ms2 = MyStruct ;
assert_relative_eq!;
Example 2
In this example, we cast the f64 type to isize.
# use *;
# use *;
let ms1 = MyStruct ;
let ms2 = MyStruct ;
assert_abs_diff_eq!;
// The underlying generated code performs
assert!;
When we use the #[approx(cast_value)] syntax, we get a different result.
# use *;
# use *;
let ms1 = MyStruct2 ;
let ms2 = MyStruct2 ;
assert_abs_diff_ne!;
// Here, the epsilon value for isize is casted to f64
assert!;
Mapping Values
We can map values before comparing them.
By default, we need to return an option of the value in question.
This allows to do computations where error can occur.
Although this error is not caught, the comparison will fail if any of the two compared objects
return a None value.
# use *;
# use *;
# let t1 = Tower ;
# let t2 = Tower ;
# assert_abs_diff_ne!;
This functionality can also be useful when having more complex datatypes.
# use *;
# use *;
Mapping Epsilon Values
We can also map epsilon values before using them. This is usefull i.e. for tuples or arrays.
# use *;
# use *;
# let d1 = DifferentialEvolution ;
# let d2 = DifferentialEvolution ;
# assert_abs_diff_eq!;
Static Values
We can force a static EPSILON or max_relative value for individual fields.
# use *;
# use *;
let r1 = Rectangle ;
let r2 = Rectangle ;
// This is always true although the epsilon is smaller than the
// difference between fields a and b respectively.
assert_abs_diff_eq!;
assert_abs_diff_eq!;
assert_abs_diff_eq!;
// Here, the epsilon value has become larger than the difference between the
// b field values.
assert_abs_diff_ne!;
Object Attributes
Default Epsilon
The [AbsDiffEq] trait allows to specify a default value for its EPSILON associated type.
We can control this value by specifying it on an object level.
# use *;
# use *;
let benchmark1 = Benchmark ;
let benchmark2 = Benchmark ;
// When testing with not additional arguments, the results match
assert_abs_diff_eq!;
// Once we specify a lower epsilon, the values do not agree anymore.
assert_abs_diff_ne!;
Default Max Relative
Similarly to [Default Epsilon], we can also choose a default max_relative devaition.
# use *;
# use *;
let bench1 = Benchmark ;
let bench2 = Benchmark ;
assert_relative_eq!;
assert_relative_ne!;
Epsilon Type
When specifying nothing, the macros will infer the EPSILON type from the type of the
first struct/enum field (the order in which it is parsed).
This can be problematic in certain scenarios which is why we can also manually specify this
type.
# use *;
# use *;
let car1 = Car ;
let car2 = Car ;
assert_relative_eq!;
assert_relative_ne!;
Into Iterator
To compare two fields which consist of a iterable list of values, we can use the
#[approx(into_iter)] field attribute.
# use *;
# use *;
let p1 = Parameter ;
let p2 = Parameter ;
assert_abs_diff_ne!;
assert_abs_diff_eq!;
It has to be noted that whenever both iterator are not of the same length, that the comparison will fail.
# use approx::*;
# use approx_derive::*;
#[derive(AbsDiffEq, PartialEq, Debug)]
#[approx(epsilon_type = f64)]
struct Polynomial {
#[approx(into_iter)]
coefficients: Vec<f64>,
}
let poly1 = Polynomial { coefficients: vec![1.0, 0.5] };
let poly2 = Polynomial { coefficients: vec![1.0, 0.5, 1.0/6.0] };
assert_abs_diff_eq!(poly1, poly2);