multi_trait_object/trait_impl/
try_partial_eq.rs

1use std::any::{Any};
2use crate::MultitraitObject;
3
4/// Compares the given value with an Any trait object.
5/// Register this trait if you want to compare two Multitrait objects
6/// with the [TryPartialEq] trait.
7pub trait PartialEqAny {
8    fn partial_equal_any(&self, other: &dyn Any) -> bool;
9}
10
11impl<T: PartialEq + Any> PartialEqAny for T {
12    fn partial_equal_any(&self, other: &dyn Any) -> bool {
13        if let Some(other) = other.downcast_ref::<T>() {
14            self.eq(other)
15        } else {
16            false
17        }
18    }
19}
20
21/// Tries to compare the MultitraitObject with another object
22/// and returns Some(bool) when the underlying type implements PartialEq and
23/// has the [PartialEqAny] trait registered on the object.
24pub trait TryPartialEq {
25    fn try_eq(&self, other: &Self) -> Option<bool>;
26}
27
28impl TryPartialEq for MultitraitObject {
29    fn try_eq(&self, other: &Self) -> Option<bool> {
30        if let Some(eq_any) = self.downcast_trait::<dyn PartialEqAny>() {
31            let any = other.downcast_trait::<dyn Any>().unwrap();
32            Some(eq_any.partial_equal_any(any))
33        } else {
34            None
35        }
36    }
37}