#bounds #traits #debugging #macro-derive #standard #clone #default

macro derive-ex

Improved version of the macro to implement the traits defined in the standard library

9 releases

0.1.8 May 5, 2024
0.1.7 Oct 2, 2023
0.1.6 Sep 25, 2023
0.1.5 Aug 25, 2023
0.1.1 May 5, 2022

#2634 in Rust patterns

Download history 143/week @ 2024-07-20 118/week @ 2024-07-27 195/week @ 2024-08-03 116/week @ 2024-08-10 114/week @ 2024-08-17 123/week @ 2024-08-24 126/week @ 2024-08-31 94/week @ 2024-09-07 110/week @ 2024-09-14 138/week @ 2024-09-21 113/week @ 2024-09-28 95/week @ 2024-10-05 238/week @ 2024-10-12 357/week @ 2024-10-19 202/week @ 2024-10-26 135/week @ 2024-11-02

943 downloads per month
Used in 11 crates (5 directly)

MIT/Apache

145KB
3K SLoC

derive-ex

Crates.io Docs.rs Actions Status

Improved version of the macro to implement the traits defined in the standard library.

Documentation

See #[derive_ex] documentation for details.

Differences from standard derive macros

  • A trait bound that is automatically generated is smarter.
  • You can specify trait bound manually.
  • You can specify default values for each field.
  • You can specify comparison method for each field.
  • You can specify ignored field with the derivation of Debug.
  • Support derive Clone::clone_from.
  • Support derive operators. (Add, AddAssign, Not, Deref, etc.)

Supported traits

  • Copy
  • Clone
  • Debug
  • Default
  • Ord, PartialOrd, Eq, PartialEq, Hash
  • operators
    • Add-like (Add, Sub, Mul, Shl, etc.)
    • AddAssign-like (AddAssign, SubAssign, MulAssign, ShlAssign, etc.)
    • Not-like (Not, Neg)
    • Deref, DerefMut

Unsupported traits

The following traits are not supported as more suitable crates exist.

trait crate
Display, FromStr parse-display
Error thiserror

Install

Add this to your Cargo.toml:

[dependencies]
derive-ex = "0.1.7"

Example

use derive_ex::derive_ex;

#[derive(Eq, PartialEq, Debug)]
#[derive_ex(Add, AddAssign, Clone, Default)]
struct X {
    #[default(10)]
    a: u32,
}
assert_eq!(X { a: 1 } + X { a: 2 }, X { a: 3 });
assert_eq!(X::default(), X { a: 10 });

#[derive(Eq, PartialEq, Debug)]
#[derive_ex(Clone, Default)]
enum Y {
    A,
    #[default]
    B,
}
assert_eq!(Y::default(), Y::B);

License

This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~305–770KB
~17K SLoC