partial_eq_dyn_derive

Crates.iopartial_eq_dyn_derive
lib.rspartial_eq_dyn_derive
version0.1.0
sourcesrc
created_at2023-07-06 06:28:43.114874
updated_at2023-07-06 06:28:43.114874
descriptionDerives for PartialEq on structs and enums with trait objects
homepage
repositoryhttps://github.com/StamesJames/partial_eq_dyn
max_upload_size
id909620
size29,341
(StamesJames)

documentation

README

Disclaimer

This is the first crate I published so I am new to making things production ready. Therefore use this crate with caution and feedback is welcome.

partial_eq_dyn_derive

To implement PartialEq on types with trait object fields you can use the derive macro PartialEqDyn. The implementation needs the traits that are present as trait objects to have AsAny and DynPartialEq as supertraits. For those traits there also exist derive macros AsAny and DynPartialEq. Here an Example:

use partial_eq_dyn::{AsAny, DynPartialEq};
use partial_eq_dyn_derive::{AsAny, DynPartialEq, PartialEqDyn};

trait TestTrait: core::fmt::Debug + AsAny + DynPartialEq {}

#[derive(AsAny, DynPartialEq, PartialEq)]
struct TestTraitImplementor(i32);

impl TestTrait for TestTraitImplementor {}

#[derive(PartialEqDyn)]
struct TestStruct {
    field1: i32,
    field2: Box<i32>,
    field3: Box<dyn TestTrait>,
}

Or if the type implements the trait itself:

use partial_eq_dyn::{AsAny, DynPartialEq};
use partial_eq_dyn_derive::{AsAny, DynPartialEq, PartialEqDyn};

trait TestTrait: core::fmt::Debug + AsAny + DynPartialEq {}

#[derive(AsAny, DynPartialEq, PartialEqDyn)]
struct TestStruct {
    field1: i32,
    field2: Box<i32>,
    field3: Option<dyn TestTrait>,
}

impl TestTrait for TestStruct {}
Commit count: 14

cargo fmt