approx-derive & approxim


Rust
github.com/jonaspleyer/approx-derive

During my development of cellular_raza, I often came across the task of comparing floating point numbers, especially during testing. The Rust ecosystem has the aprox crate which is popular for this use-case and widely used. But the crate was missing derive macros for the AbsDiffEq and RelativeEq traits. So I decided to fix this by providing an external crate approx-derive which addresses this problem.

Functionality

The crate only provides the two derive macros AbsDiffEq and RelativeEq which generate implementations of the identically-named traits. It relies mainly on syn for parsing code, and quote for generating code.

The following example shows how to use the derive macro. During the development and usage of this crate, I have added multiple features (see docs.rs/approx-derive).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use approx::assert_abs_diff_eq;
use approx_derive::AbsDiffEq;

#[derive(AbsDiffEq, PartialEq, Debug)]
struct Player {
    x: f64,
    y: f64
    #[approx(equal)]
    id: (u64, u16),
}

#[test]
fn test() {
    let player1 = Player {
        x: 1.0,
        y: 2.0,
        id: (0, 0),
    };
    let player2 = Player {
        x: 1.1,
        y: 1.9,
        id: (0, 0),
    };
    assert_abs_diff_eq!(player1, player2, epsilon = 0.11);
    assert_abs_diff_ne!(player1, player2, epsilon = 0.01);
}

Approxim

At the time of writing, the approx crate has not been updated since $\approx$ 3 years and many PRs and feature requests have been ignored. Furthermore, derive macros have not been added to the crate. For this reason, I have forked approx, creating approxim which includes all open PRs as well as derive macros. In order to provide a working solution for two crates with different names, I needed to infer the name of the crate automatically. I guarded this functionality behind the infer_name feature which does not have to be activated when using with approx. Although this crate exists, I hope that my solution for derive macros will be incorporated within approx at some point in the future.

Update (02/2026)

I have now taken over maintainership from brendanzab for the approx crate and will release the additional features in upcoming versions.