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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::fmt::Debug;
use std::any::Any;

use super::*;

pub trait ValueConstraint: 'static + PartialEq + Eq + Debug + Clone { }

impl<T: 'static + ?Sized + PartialEq + Eq + Debug + Clone> ValueConstraint for T { }

pub trait Value: Any + AnyExtension + AsAny + Debug {
    fn equals(&self, other: &dyn Any) -> bool {
        self.reference_equals(other)
    }

as_trait!(Value);
as_boxed!(Value);
}

impl<T: ?Sized + ValueConstraint> Value for T {
    fn equals(&self, other: &dyn Any) -> bool {
        match other.downcast_ref::<T>() {
            Some(r) => *self == *r,
            None => false
        }
    }
 
as_trait!(impl Value);
as_boxed!(impl Value);
}

#[macro_export]
macro_rules! boxed_value_trait {
    ($trait:tt) => {
as_boxed!(impl PartialEq for $trait);
as_boxed!(impl Eq for $trait);
as_boxed!(impl Clone for $trait);
    };

    ($trait:tt<$($param:tt), *>) => {
as_boxed!(impl PartialEq for $trait<$($param), *>);
as_boxed!(impl Eq for $trait<$($param), *>);
as_boxed!(impl Clone for $trait<$($param), *>);
    };

    ($trait:tt<$($param:tt: $constraint0:tt $(+ $constraint:tt)*), *>) => {
as_boxed!(impl PartialEq for $trait<$($param: $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Eq for $trait<$($param: $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Clone for $trait<$($param: $constraint0 $(+ $constraint)*), *>);
    };

    ($trait:tt<$($param:tt: ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
as_boxed!(impl PartialEq for $trait<$($param: ?Sized + $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Eq for $trait<$($param: ?Sized + $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Clone for $trait<$($param: ?Sized + $constraint0 $(+ $constraint)*), *>);
    };

    ($trait:tt<$($param:tt: 'static + $constraint0:tt $(+ $constraint:tt)*), *>) => {
as_boxed!(impl PartialEq for $trait<$($param: 'static + $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Eq for $trait<$($param: 'static + $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Clone for $trait<$($param: 'static + $constraint0 $(+ $constraint)*), *>);
    };

    ($trait:tt<$($param:tt: 'static + ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
as_boxed!(impl PartialEq for $trait<$($param: 'static + ?Sized + $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Eq for $trait<$($param: 'static + ?Sized + $constraint0 $(+ $constraint)*), *>);
as_boxed!(impl Clone for $trait<$($param: 'static + ?Sized + $constraint0 $(+ $constraint)*), *>);
    };
}

boxed_value_trait!(Value);

#[cfg(test)]
mod tests {

    use super::*;

    #[derive(PartialEq, Eq, Debug, Clone)]
    struct S1 {
        a: i32,
        b: u32
    }

    #[test]
    fn eq() {
        let s = S1 {
            a: 1,
            b: 2
        };
        let t: Box<dyn Value> = Box::new(s.clone());
        let t2: Box<dyn Value> = Box::new(s.clone());
        assert!(t == t2);
    }

    #[test]
    fn debug() {
        let s = S1 {
            a: 1,
            b: 2
        };
        let t: Box<dyn Value> = Box::new(s);
        println!("{:?}", t);
    }

    #[test]
    fn clone() {
        let s = S1 {
            a: 1,
            b: 2
        };
        let b = s.clone_boxed();
        assert_eq!(type_name::<S1>(), b.as_ref().type_name());
        assert_eq!(type_name::<Box<dyn Value>>(), b.type_name());
        assert!(s.equals(b.as_ref().as_any_ref()));
    }

    #[test]
    fn as_trait_as_boxed() {
        let s = S1 {
            a: 1,
            b: 2
        };
        s.as_trait_ref();

        println!("{:p}", s.as_trait_ref() as &dyn Value);

        let mut s = s;
        s.as_trait_mut();

        assert_eq!(&s.clone_boxed(), &s.to_boxed());
    }

    trait SomeType0<K: KeyConstraint, V: ValueConstraint>: Value {
        fn say(&self, k: K, v: V);

    as_boxed!(SomeType0<K, V>);
    as_trait!(SomeType0<K, V>);
    }

boxed_value_trait!(SomeType0<K: KeyConstraint, V: ValueConstraint>);

    trait SomeType1<K: KeyConstraint + Debug, V: ValueConstraint>: Value {
        fn say(&self, k: K, v: V);

    as_boxed!(SomeType1<K, V>);
    as_trait!(SomeType1<K, V>);
    }

boxed_value_trait!(SomeType1<K: KeyConstraint + Debug, V: ValueConstraint>);

     trait SomeType2<K: ?Sized + KeyConstraint, V: ?Sized + ValueConstraint>: Value {
        fn say(&self, k: K, v: V);

    as_boxed!(SomeType2<K, V>);
    as_trait!(SomeType2<K, V>);
    }

boxed_value_trait!(SomeType2<K: ?Sized + KeyConstraint, V: ?Sized + ValueConstraint>);

     trait SomeType3<K: 'static + KeyConstraint, V: 'static + ValueConstraint>: Value {
        fn say(&self, k: K, v: V);

    as_boxed!(SomeType3<K, V>);
    as_trait!(SomeType3<K, V>);
    }

boxed_value_trait!(SomeType3<K: 'static + KeyConstraint, V: 'static + ValueConstraint>);

     trait SomeType4<K: 'static + ?Sized + KeyConstraint, V: 'static + ?Sized + ValueConstraint>: Value {
        fn say(&self, k: K, v: V);

    as_boxed!(SomeType4<K, V>);
    as_trait!(SomeType4<K, V>);
    }

boxed_value_trait!(SomeType4<K: 'static + ?Sized + KeyConstraint, V: 'static + ?Sized + ValueConstraint>);
}