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
/// Automatically defines the `CollectionRef::upcast_item_ref` function using the
/// covariance of the `ItemRef<'a>` type with regards to `'a`.
///
/// ## Example
///
/// ```
/// use cc_traits::{Collection, CollectionRef, covariant_item_ref};
///
/// pub struct MyVec<T>(Vec<T>);
///
/// impl<T> Collection for MyVec<T> {
///   type Item = T;
/// }
///
/// impl<T> CollectionRef for MyVec<T> {
///   type ItemRef<'a>
///   = &'a T where Self: 'a;
///
///   covariant_item_ref!();
/// }
/// ```
#[macro_export]
macro_rules! covariant_item_ref {
	() => {
		fn upcast_item_ref<'short, 'long: 'short>(r: Self::ItemRef<'long>) -> Self::ItemRef<'short>
		where
			Self: 'long,
		{
			r
		}
	};
}

/// Automatically defines the `CollectionMut::upcast_item_mut` function using the
/// covariance of the `ItemMut<'a>` type with regards to `'a`.
///
/// ## Example
///
/// ```
/// use cc_traits::{Collection, CollectionMut, covariant_item_mut};
///
/// pub struct MyVec<T>(Vec<T>);
///
/// impl<T> Collection for MyVec<T> {
///   type Item = T;
/// }
///
/// impl<T> CollectionMut for MyVec<T> {
///   type ItemMut<'a>
///   = &'a mut T where Self: 'a;
///
///   covariant_item_mut!();
/// }
/// ```
#[macro_export]
macro_rules! covariant_item_mut {
	() => {
		fn upcast_item_mut<'short, 'long: 'short>(r: Self::ItemMut<'long>) -> Self::ItemMut<'short>
		where
			Self: 'long,
		{
			r
		}
	};
}

/// Automatically defines the `KeyedRef::upcast_item_ref` function using the
/// covariance of the `KeyRef<'a>` type with regards to `'a`.
///
/// ## Example
///
/// ```
/// use cc_traits::{Collection, Keyed, KeyedRef, covariant_key_ref};
///
/// pub struct MyMap<K, V>(std::collections::HashMap<K, V>);
///
/// impl<K, V> Collection for MyMap<K, V> {
///   type Item = V;
/// }
///
/// impl<K, V> Keyed for MyMap<K, V> {
///   type Key = K;
/// }
///
/// impl<K, V> KeyedRef for MyMap<K, V> {
///   type KeyRef<'a>
///   = &'a K where Self: 'a;
///
///   covariant_key_ref!();
/// }
/// ```
#[macro_export]
macro_rules! covariant_key_ref {
	() => {
		fn upcast_key_ref<'short, 'long: 'short>(r: Self::KeyRef<'long>) -> Self::KeyRef<'short>
		where
			Self: 'long,
		{
			r
		}
	};
}