File size: 2,053 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::{
    any::{Any, TypeId},
    hash::{Hash, Hasher},
};

/// A dynamic-dispatch version of the [`PartialEq`] trait. Implemented for every type that
/// implements [`Any`] + [`PartialEq`].
pub trait DynPartialEq: Any {
    fn dyn_partial_eq(&self, other: &dyn Any) -> bool;
}

impl<T: Any + PartialEq> DynPartialEq for T {
    fn dyn_partial_eq(&self, other: &dyn Any) -> bool {
        other
            .downcast_ref::<Self>()
            .map(|other| PartialEq::eq(self, other))
            .unwrap_or(false)
    }
}

#[macro_export]
macro_rules! impl_partial_eq_for_dyn {
    ($ty:ty) => {
        impl ::std::cmp::PartialEq for $ty {
            fn eq(&self, other: &Self) -> bool {
                $crate::DynPartialEq::dyn_partial_eq(self, other as &dyn ::std::any::Any)
            }
        }
    };
}

/// A dynamic-dispatch version of the [`Eq`] trait. Implemented for every type that implements
/// [`Any`] + [`Eq`].
pub trait DynEq: DynPartialEq {}

impl<T: Any + Eq> DynEq for T {}

#[macro_export]
macro_rules! impl_eq_for_dyn {
    ($ty:ty) => {
        impl ::std::cmp::Eq for $ty {}
    };
}

/// A dynamic-dispatch version of the [`Hash`] trait. Implemented for every type that implements
/// [`Any`] + [`Hash`].
///
/// The `TypeId::of::<Self>()` value is included in the resulting hash, so the hash generated by
/// `DynEqHash` will not match the hash generated by the underlying `Hash` implementation. In other
/// words, the hash of `T` will not match the hash of `T as dyn SomeDynHashTrait`.
pub trait DynHash {
    fn dyn_hash(&self, state: &mut dyn Hasher);
}

impl<T: Any + Hash> DynHash for T {
    fn dyn_hash(&self, mut state: &mut dyn Hasher) {
        Hash::hash(&(TypeId::of::<Self>(), self), &mut state);
    }
}

#[macro_export]
macro_rules! impl_hash_for_dyn {
    ($ty:ty) => {
        impl ::std::hash::Hash for $ty {
            fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
                $crate::DynHash::dyn_hash(self, state as &mut dyn (::std::hash::Hasher))
            }
        }
    };
}