#trait-object #eq #dyn #no-alloc

no-std dyn-eq

Test equality between trait objects

3 releases

0.1.3 Jul 20, 2023
0.1.2 Feb 17, 2023
0.1.1 Feb 17, 2023
0.1.0 Feb 16, 2023

#1236 in Rust patterns

Download history 373/week @ 2024-03-13 286/week @ 2024-03-20 228/week @ 2024-03-27 188/week @ 2024-04-03 307/week @ 2024-04-10 293/week @ 2024-04-17 320/week @ 2024-04-24 266/week @ 2024-05-01 362/week @ 2024-05-08 333/week @ 2024-05-15 264/week @ 2024-05-22 242/week @ 2024-05-29 258/week @ 2024-06-05 335/week @ 2024-06-12 400/week @ 2024-06-19 337/week @ 2024-06-26

1,376 downloads per month
Used in bevy_mod_static_inventory

MPL-2.0 license

13KB
123 lines

Test equality between trait objects

github crates.io doc.rs license build passively-maintained

This crate provides a DynEq trait which permit comparing trait objects. If the two objects are instances of different structs, they will always be not equal. If they are instances of the same struct, the struct's Eq will be used.

Example

use dyn_eq::DynEq;

trait MyTrait: DynEq {}
dyn_eq::eq_trait_object!(MyTrait);

impl MyTrait for u8 {}
impl MyTrait for u16 {}

let a: &dyn MyTrait = &5u8;
let a_bis: &dyn MyTrait = &5u8;
let b: &dyn MyTrait = &10u8;
let c: &dyn MyTrait = &5u16;
let d: &dyn MyTrait = &10u16;

// Same type, same value
assert!(a == a_bis);
// Same type, different value
assert!(a != b);
// Different type, different value
assert!(a != d);
// Different type, same value
// Even if the value is the same, the fact that it's a diffrent type means it's not equal
assert!(a != c);

// Now data structures containing Box<dyn MyTrait> can derive Eq.
#[derive(PartialEq, Eq)]
struct Container {
    field: Box<dyn MyTrait>
}

No runtime deps

Features