Skip to main content

datex_core/traits/
dyn_eq.rs

1use crate::values::core_values::native::DatexNative;
2use core::{any::Any, fmt::Debug};
3
4/// A trait for dynamic equality comparison of types that implement `Any`.
5pub trait DynEq: Any {
6    fn dyn_eq(&self, other: &dyn Any) -> bool;
7}
8
9/// Default implementation of `DynEq` for all types that implement `Any`.
10/// This implementation always returns `false`, indicating that the types are not equal.
11impl<T> DynEq for T
12where
13    T: Any,
14{
15    default fn dyn_eq(&self, _other: &dyn Any) -> bool {
16        todo!("Dynamic equality not implemented for this type");
17    }
18}
19
20/// Specialized implementation of `DynEq` for types that implement both `Any` and `PartialEq`.
21/// This implementation checks if the other type can be downcast to the same type and compares them for equality.
22impl<T> DynEq for T
23where
24    T: Any + PartialEq + Debug + DatexNative,
25{
26    fn dyn_eq(&self, other: &dyn Any) -> bool {
27        other.downcast_ref::<T>() == Some(self)
28    }
29}