3 releases

0.1.2 May 16, 2021
0.1.1 May 15, 2021
0.1.0 May 15, 2021

#1608 in Rust patterns

Download history 307/week @ 2023-12-10 285/week @ 2023-12-17 164/week @ 2023-12-24 100/week @ 2023-12-31 289/week @ 2024-01-07 230/week @ 2024-01-14 210/week @ 2024-01-21 276/week @ 2024-01-28 231/week @ 2024-02-04 287/week @ 2024-02-11 504/week @ 2024-02-18 626/week @ 2024-02-25 371/week @ 2024-03-03 419/week @ 2024-03-10 386/week @ 2024-03-17 447/week @ 2024-03-24

1,645 downloads per month
Used in tuio-rs

MIT/Apache

7KB

dyn_partial_eq

PartialEq for trait objects

This crate provides macros for deriving PartialEq for Box<dyn Trait>, removing the boilerplate of having to provide your own implementations. Inspired by this blog post: https://dev.to/magnusstrale/rust-trait-objects-in-a-vector-non-trivial-4co5

Add the crate to your project:

cargo add dyn_partial_eq
use dyn_partial_eq::*;

// Use this to add a DynPartialEq supertrait and implement PartialEq for your trait.
#[dyn_partial_eq]
trait SomeTrait {}

// Derive DynPartialEq and PartialEq on your types that implement your trait.
#[derive(DynPartialEq, PartialEq)]
struct A(usize);
impl SomeTrait for A {}

#[derive(DynPartialEq, PartialEq)]
struct B((usize, usize));
impl SomeTrait for B {}

And voila:

let boxed_a_zero: Box<dyn SomeTrait> = Box::new(A(0));
let boxed_a_one: Box<dyn SomeTrait> = Box::new(A(1));
let boxed_b: Box<dyn SomeTrait> = Box::new(B((1, 2)));

assert_eq!(&boxed_a_zero == &boxed_a_zero, true);
assert_eq!(&boxed_a_zero == &boxed_a_one, false);
assert_eq!(&boxed_a_zero == &boxed_b, false);

assert_eq!(&boxed_a_one == &boxed_a_zero, false);
assert_eq!(&boxed_a_one == &boxed_a_one, true);
assert_eq!(&boxed_a_one == &boxed_b, false);

assert_eq!(&boxed_b == &boxed_a_zero, false);
assert_eq!(&boxed_b == &boxed_a_one, false);
assert_eq!(&boxed_b == &boxed_b, true);

Dependencies

~1.5MB
~33K SLoC