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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

#[macro_export]
macro_rules! as_boxed {
    ($trait:tt) => {
    fn clone_boxed(&self) -> Box<dyn $trait>;

    fn to_boxed(self) -> Box<dyn $trait>;
    };

    ($trait:tt<$($param:tt), *>) => {
    fn clone_boxed(&self) -> Box<dyn $trait<$($param), *>>;

    fn to_boxed(self) -> Box<dyn $trait<$($param), *>>;
    };

    (impl $trait:tt) => {
    fn clone_boxed(&self) -> Box<dyn $trait> {
        Box::new(self.clone())
    }

    fn to_boxed(self) -> Box<dyn $trait> {
        Box::new(self)
    }
    };

    (impl $trait:tt<$($param:tt), *>) => {
    fn clone_boxed(&self) -> Box<dyn $trait<$($param), *>> {
        Box::new(self.clone())
    }

    fn to_boxed(self) -> Box<dyn $trait<$($param), *>> {
        Box::new(self)
    }
    };

    (impl Hash for $trait:tt) => {
impl std::hash::Hash for Box<dyn $trait> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u64(self.as_ref().hashcode());
    }
}
    };

    (impl Hash for $trait:tt<$($param:tt), *>) => {
impl<$($param), *> std::hash::Hash for Box<dyn $trait<$($param), *>> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u64(self.as_ref().hashcode());
    }
}
    };

    (impl Hash for $trait:tt<$($param:tt: $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: $constraint0 $(+ $constraint)*), *> std::hash::Hash for Box<dyn $trait<$($param), *>> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u64(self.as_ref().hashcode());
    }
}
    };

    (impl Hash for $trait:tt<$($param:tt: ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: ?Sized + $constraint0 $(+ $constraint)*), *> std::hash::Hash for Box<dyn $trait<$($param), *>> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u64(self.as_ref().hashcode());
    }
}
    };

    (impl Hash for $trait:tt<$($param:tt: 'static + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: 'static + $constraint0 $(+ $constraint)*), *> std::hash::Hash for Box<dyn $trait<$($param), *>> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u64(self.as_ref().hashcode());
    }
}
    };

    (impl Hash for $trait:tt<$($param:tt: 'static + ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: 'static + ?Sized + $constraint0 $(+ $constraint)*), *> std::hash::Hash for Box<dyn $trait<$($param), *>> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u64(self.as_ref().hashcode());
    }
}
    };

    (impl PartialEq for $trait:tt) => {
impl PartialEq for Box<dyn $trait> {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref().equals(other.as_ref().as_any_ref())
    }
}
    };

    (impl PartialEq for $trait:tt<$($param:tt), *>) => {
impl<$($param), *> PartialEq for Box<dyn $trait<$($param), *>> {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref().equals(other.as_ref().as_any_ref())
    }
}
    };

    (impl PartialEq for $trait:tt<$($param:tt: $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: $constraint0 $(+ $constraint)*), *> PartialEq for Box<dyn $trait<$($param), *>> {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref().equals(other.as_ref().as_any_ref())
    }
}
    };

    (impl PartialEq for $trait:tt<$($param:tt: ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: ?Sized + $constraint0 $(+ $constraint)*), *> PartialEq for Box<dyn $trait<$($param), *>> {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref().equals(other.as_ref().as_any_ref())
    }
}
    };

    (impl PartialEq for $trait:tt<$($param:tt: 'static + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: 'static + $constraint0 $(+ $constraint)*), *> PartialEq for Box<dyn $trait<$($param), *>> {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref().equals(other.as_ref().as_any_ref())
    }
}
    };

    (impl PartialEq for $trait:tt<$($param:tt: 'static + ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: 'static + ?Sized + $constraint0 $(+ $constraint)*), *> PartialEq for Box<dyn $trait<$($param), *>> {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref().equals(other.as_ref().as_any_ref())
    }
}
    };

    (impl Eq for $trait:tt) => {
impl Eq for Box<dyn $trait> { }
    };

    (impl Eq for $trait:tt<$($param:tt), *>) => {
impl<$($param), *> Eq for Box<dyn $trait<$($param), *>> { }
    };

    (impl Eq for $trait:tt<$($param:tt: $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: $constraint0 $(+ $constraint)*), *> Eq for Box<dyn $trait<$($param), *>> { }
    };

    (impl Eq for $trait:tt<$($param:tt: ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: ?Sized + $constraint0 $(+ $constraint)*), *> Eq for Box<dyn $trait<$($param), *>> { }
    };

    (impl Eq for $trait:tt<$($param:tt: 'static + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: 'static + $constraint0 $(+ $constraint)*), *> Eq for Box<dyn $trait<$($param), *>> { }
    };

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

    (impl Clone for $trait:tt) => {
impl Clone for Box<dyn $trait> {
    fn clone(&self) -> Self {
        $trait::clone_boxed(self.as_ref())
    }
}
    };

    (impl Clone for $trait:tt<$($param:tt), *>) => {
impl<$($param), *> Clone for Box<dyn $trait<$($param), *>> {
    fn clone(&self) -> Self {
        $trait::clone_boxed(self.as_ref())
    }
}
    };

    (impl Clone for $trait:tt<$($param:tt: $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: $constraint0 $(+ $constraint)*), *> Clone for Box<dyn $trait<$($param), *>> {
    fn clone(&self) -> Self {
        $trait::clone_boxed(self.as_ref())
    }
}
    };

    (impl Clone for $trait:tt<$($param:tt: ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: ?Sized + $constraint0 $(+ $constraint)*), *> Clone for Box<dyn $trait<$($param), *>> {
    fn clone(&self) -> Self {
        $trait::clone_boxed(self.as_ref())
    }
}
    };

    (impl Clone for $trait:tt<$($param:tt: 'static + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: 'static + $constraint0 $(+ $constraint)*), *> Clone for Box<dyn $trait<$($param), *>> {
    fn clone(&self) -> Self {
        $trait::clone_boxed(self.as_ref())
    }
}
    };

    (impl Clone for $trait:tt<$($param:tt: 'static + ?Sized + $constraint0:tt $(+ $constraint:tt)*), *>) => {
impl<$($param: 'static + ?Sized + $constraint0 $(+ $constraint)*), *> Clone for Box<dyn $trait<$($param), *>> {
    fn clone(&self) -> Self {
        $trait::clone_boxed(self.as_ref())
    }
}
    };
}

#[macro_export]
macro_rules! as_trait {
    ($trait: tt) => {
    fn as_trait_ref(&self) -> &dyn $trait;

    fn as_trait_mut(&mut self) -> &mut dyn $trait;
    };

    ($trait:tt<$($param:tt), *>) => {
    fn as_trait_ref(&self) -> &dyn $trait<$($param), *>;

    fn as_trait_mut(&mut self) -> &mut dyn $trait<$($param), *>;
    };

    (impl $trait: tt) => {
    fn as_trait_ref(&self) -> &dyn $trait {
        self
    }

    fn as_trait_mut(&mut self) -> &mut dyn $trait {
        self
    }
    };

    (impl $trait:tt<$($param:tt), *>) => {
    fn as_trait_ref(&self) -> &dyn $trait<$($param), *> {
        self
    }

    fn as_trait_mut(&mut self) -> &mut dyn $trait<$($param), *> {
        self
    }
    };
}

#[cfg(test)]
mod tests {
    use crate::*;
    use std::marker::*;
    use crate::any::*;
    use std::fmt::Debug;

    trait SomeType: Key {
        fn say(&self);

    as_boxed!(SomeType);
    as_trait!(SomeType);
    }

    as_boxed!(impl Hash for SomeType);
    as_boxed!(impl PartialEq for SomeType);
    as_boxed!(impl Eq for SomeType);
    as_boxed!(impl Clone for SomeType);

    #[derive(Clone, PartialEq, Eq, Hash, Debug)]
    struct ST { }

    impl SomeType for ST {
        fn say(&self) {

        }

    as_boxed!(impl SomeType);
    as_trait!(impl SomeType);
    }

    trait SomeType1<K>: Key {
        fn say(&self, k: K);

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

    as_boxed!(impl Hash for SomeType1<K>);
    as_boxed!(impl PartialEq for SomeType1<K>);
    as_boxed!(impl Eq for SomeType1<K>);
    as_boxed!(impl Clone for SomeType1<K>);

    #[derive(Clone, PartialEq, Eq, Hash, Debug)]
    struct ST1<K: KeyConstraint> { 
        k: PhantomData<K>
    }

    impl<K: KeyConstraint> SomeType1<K> for ST1<K> {
        fn say(&self, _k: K) {

        }

    as_boxed!(impl SomeType1<K>);
    as_trait!(impl SomeType1<K>);
    }

    trait SomeType2<K, V>: Key {
        fn say(&self, k: K, v: V);

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

    as_boxed!(impl Hash for SomeType2<K, V>);
    as_boxed!(impl PartialEq for SomeType2<K, V>);
    as_boxed!(impl Eq for SomeType2<K, V>);
    as_boxed!(impl Clone for SomeType2<K, V>);

    #[derive(Clone, PartialEq, Eq, Hash, Debug)]
    struct ST2<K: KeyConstraint, V: ValueConstraint> { 
        k: PhantomData<K>,
        v: PhantomData<V>
    }

    impl<K: KeyConstraint, V: KeyConstraint> SomeType2<K, V> for ST2<K, V> {
        fn say(&self, _k: K, _v: V) {

        }

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

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

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

    as_boxed!(impl Hash for SomeType3<K: KeyConstraint, V: ValueConstraint>);
    as_boxed!(impl PartialEq for SomeType3<K: KeyConstraint, V: ValueConstraint>);
    as_boxed!(impl Eq for SomeType3<K: KeyConstraint, V: ValueConstraint>);
    as_boxed!(impl Clone for SomeType3<K: KeyConstraint, V: ValueConstraint>);

    #[derive(Clone, PartialEq, Eq, Hash, Debug)]
    struct ST3<K: ?Sized + KeyConstraint, V: ?Sized + ValueConstraint> { 
        k: PhantomData<K>,
        v: PhantomData<V>
    }

    impl<K: ?Sized + KeyConstraint, V: ?Sized + KeyConstraint> SomeType3<K, V> for ST3<K, V> {
        fn say(&self, _k: K, _v: V) {

        }

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

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

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

    as_boxed!(impl Hash for SomeType4<K: ?Sized + KeyConstraint, V: ?Sized + ValueConstraint>);
    as_boxed!(impl PartialEq for SomeType4<K: ?Sized + KeyConstraint, V: ?Sized + ValueConstraint>);
    as_boxed!(impl Eq for SomeType4<K: ?Sized + KeyConstraint, V: ?Sized + ValueConstraint>);
    as_boxed!(impl Clone for SomeType4<K: ?Sized + KeyConstraint, V: ?Sized + ValueConstraint>);

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

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

    as_boxed!(impl Hash for SomeType5<K: 'static + KeyConstraint, V: 'static + ValueConstraint>);
    as_boxed!(impl PartialEq for SomeType5<K: 'static + KeyConstraint, V: 'static + ValueConstraint>);
    as_boxed!(impl Eq for SomeType5<K: 'static + KeyConstraint, V: 'static + ValueConstraint>);
    as_boxed!(impl Clone for SomeType5<K: 'static + KeyConstraint, V: 'static + ValueConstraint>);

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

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

    as_boxed!(impl Hash for SomeType6<K: 'static + ?Sized + KeyConstraint, V: 'static + ?Sized + ValueConstraint>);
    as_boxed!(impl PartialEq for SomeType6<K: 'static + ?Sized + KeyConstraint, V: 'static + ?Sized + ValueConstraint>);
    as_boxed!(impl Eq for SomeType6<K: 'static + ?Sized + KeyConstraint, V: 'static + ?Sized + ValueConstraint>);
    as_boxed!(impl Clone for SomeType6<K: 'static + ?Sized + KeyConstraint, V: 'static + ?Sized + ValueConstraint>);

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

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

    as_boxed!(impl Hash for SomeType7<K: 'static + ?Sized + KeyConstraint + Debug, V: 'static + ?Sized + ValueConstraint>);
    as_boxed!(impl PartialEq for SomeType7<K: 'static + ?Sized + KeyConstraint + Debug, V: 'static + ?Sized + ValueConstraint>);
    as_boxed!(impl Eq for SomeType7<K: 'static + ?Sized + KeyConstraint + Debug, V: 'static + ?Sized + ValueConstraint>);
    as_boxed!(impl Clone for SomeType7<K: 'static + ?Sized + KeyConstraint + Debug, V: 'static + ?Sized + ValueConstraint>);
}