If you have used the approx
crate you may have noticed that it does not provide any derive(AbsDiffEq)
or derive(RelativeEq)
macros.
The approx-derive
crate fixes this. It has a new release 0.2.1
which now also has support for enums and more complex nested structures. You can find the repo at github.com/jonaspleyer/approx-derive.
use approx::*;
use approx_derive::*;
#[derive(Clone, Eq, PartialEq, Debug)]
enum SamplingMethod {
LatinHyperCube,
Grid,
MonteCarlo,
}
#[derive(AbsDiffEq, PartialEq, Debug)]
enum Parameter {
Sampled {
min: f32,
max: f32,
#[approx(equal)]
sampling_method: SamplingMethod,
},
Fixed(f32),
}
let p1 = Parameter::Fixed(1.0);
let p2 = Parameter::Sampled {
min: 0.0,
max: 10.0,
sampling_method: SamplingMethod::Grid,
};
let p3 = Parameter::Sampled {
min: 0.1,
max: 9.99,
sampling_method: SamplingMethod::Grid,
};
approx::assert_abs_diff_ne!(p1, p2);
approx::assert_abs_diff_eq!(p2, p3, epsilon = 0.2);