2 releases

0.1.1 Mar 10, 2020
0.1.0 Mar 10, 2020

#836 in Procedural macros

MIT license

16KB
195 lines

multi_eq

multi_eq is a macro library for creating custom equality trait derives.

/// Custom comparison trait `CustomEq` with a method `custom_eq`
multi_eq_make_trait!(CustomEq, custom_eq);

#[derive(CustomEq)]
struct MyStruct {
  // Use `PartialEq` to compare this field
  #[custom_eq(cmp = "eq")]
  a: u32,

  // Ignore value of this field when checking equality
  #[custom_eq(ignore)]
  b: bool,
}

For more information, see the documentation.


lib.rs:

multi_eq

multi_eq is a macro library for creating custom equality derives.

Description

This crate exports two macros: multi_eq_make_trait!(), and multi_eq_make_derive!(). The first is for creating custom equality traits. The second is for creating a derive macro for a custom equality trait. Since derive macros can only be exported by a crate with the proc-macro crate type, a typical usage of this library is in multi-crate projects: a proc-macro crate for the derive macros, and a main crate importing the derive macros.

Example

File tree

custom-eq-example
├── Cargo.lock
├── Cargo.toml
├── custom-eq-derive
│   ├── Cargo.lock
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
└── src
    └── lib.rs

custom-eq-example/custom-eq-derive/Cargo.toml

# ...

[lib]
proc-macro = true

# ...

custom-eq-example/custom-eq-derive/src/lib.rs

use multi_eq::*;

/// Derive macro for a comparison trait `CustomEq` with a method `custom_eq`
multi_eq_make_derive!(pub, CustomEq, custom_eq);

custom-eq-example/Cargo.toml

# ...

[dependencies.custom-eq-derive]
path = "custom-eq-derive"

# ...

custom-eq-example/src/lib.rs

use multi_eq::*;
use custom_eq_derive::*;

/// Custom comparison trait `CustomEq` with a method `custom_eq`
multi_eq_make_trait!(CustomEq, custom_eq);

#[derive(CustomEq)]
struct MyStruct {
  // Use `PartialEq` to compare this field
  #[custom_eq(cmp = "eq")]
  a: u32,

  // Ignore value of this field when checking equality
  #[custom_eq(ignore)]
  b: bool,
}

Dependencies

~1.5MB
~34K SLoC