Skip to main content

midenc_hir/
eq.rs

1use core::any::Any;
2
3use crate::{EntityMut, EntityRef};
4
5/// A type-erased version of [PartialEq]
6pub trait DynPartialEq: Any + 'static {
7    fn dyn_eq(&self, rhs: &dyn PartialEqable) -> bool;
8}
9
10impl<T> DynPartialEq for T
11where
12    T: Any + PartialEq + 'static,
13{
14    #[inline]
15    default fn dyn_eq(&self, rhs: &dyn PartialEqable) -> bool {
16        rhs.eqable().downcast_ref::<T>().map(|rhs| self.eq(rhs)).unwrap_or(false)
17    }
18}
19
20/// A trait implemented by all types that are valid operands for [DynPartialEq].
21///
22/// It can be used to override the concrete type that is used as the eqable value, and obtain
23/// debugging information about that type (i.e. it's type name).
24pub trait PartialEqable {
25    fn equable_type_name(&self) -> &'static str;
26    fn eqable(&self) -> &dyn core::any::Any;
27}
28
29impl<T: ?Sized + PartialEq + crate::any::AsAny + 'static> PartialEqable for T {
30    #[inline]
31    default fn equable_type_name(&self) -> &'static str {
32        <T as crate::any::AsAny>::type_name(self)
33    }
34
35    #[inline]
36    default fn eqable(&self) -> &dyn core::any::Any {
37        <T as crate::any::AsAny>::as_any(self)
38    }
39}
40
41impl<'a, T: ?Sized + PartialEq + crate::any::AsAny + 'static> PartialEqable for EntityRef<'a, T> {
42    #[inline]
43    fn equable_type_name(&self) -> &'static str {
44        <T as crate::any::AsAny>::type_name(self)
45    }
46
47    #[inline]
48    fn eqable(&self) -> &dyn core::any::Any {
49        <T as crate::any::AsAny>::as_any(self)
50    }
51}
52
53impl<'a, T: ?Sized + PartialEq + crate::any::AsAny + 'static> PartialEqable for EntityMut<'a, T> {
54    #[inline]
55    fn equable_type_name(&self) -> &'static str {
56        <T as crate::any::AsAny>::type_name(self)
57    }
58
59    #[inline]
60    fn eqable(&self) -> &dyn core::any::Any {
61        <T as crate::any::AsAny>::as_any(self)
62    }
63}