cc_traits/
macros.rs

1/// Automatically defines the `CollectionRef::upcast_item_ref` function using the
2/// covariance of the `ItemRef<'a>` type with regards to `'a`.
3///
4/// ## Example
5///
6/// ```
7/// use cc_traits::{Collection, CollectionRef, covariant_item_ref};
8///
9/// pub struct MyVec<T>(Vec<T>);
10///
11/// impl<T> Collection for MyVec<T> {
12///   type Item = T;
13/// }
14///
15/// impl<T> CollectionRef for MyVec<T> {
16///   type ItemRef<'a>
17///   = &'a T where Self: 'a;
18///
19///   covariant_item_ref!();
20/// }
21/// ```
22#[macro_export]
23macro_rules! covariant_item_ref {
24	() => {
25		fn upcast_item_ref<'short, 'long: 'short>(r: Self::ItemRef<'long>) -> Self::ItemRef<'short>
26		where
27			Self: 'long,
28		{
29			r
30		}
31	};
32}
33
34/// Automatically defines the `CollectionMut::upcast_item_mut` function using the
35/// covariance of the `ItemMut<'a>` type with regards to `'a`.
36///
37/// ## Example
38///
39/// ```
40/// use cc_traits::{Collection, CollectionMut, covariant_item_mut};
41///
42/// pub struct MyVec<T>(Vec<T>);
43///
44/// impl<T> Collection for MyVec<T> {
45///   type Item = T;
46/// }
47///
48/// impl<T> CollectionMut for MyVec<T> {
49///   type ItemMut<'a>
50///   = &'a mut T where Self: 'a;
51///
52///   covariant_item_mut!();
53/// }
54/// ```
55#[macro_export]
56macro_rules! covariant_item_mut {
57	() => {
58		fn upcast_item_mut<'short, 'long: 'short>(r: Self::ItemMut<'long>) -> Self::ItemMut<'short>
59		where
60			Self: 'long,
61		{
62			r
63		}
64	};
65}
66
67/// Automatically defines the `KeyedRef::upcast_item_ref` function using the
68/// covariance of the `KeyRef<'a>` type with regards to `'a`.
69///
70/// ## Example
71///
72/// ```
73/// use cc_traits::{Collection, Keyed, KeyedRef, covariant_key_ref};
74///
75/// pub struct MyMap<K, V>(std::collections::HashMap<K, V>);
76///
77/// impl<K, V> Collection for MyMap<K, V> {
78///   type Item = V;
79/// }
80///
81/// impl<K, V> Keyed for MyMap<K, V> {
82///   type Key = K;
83/// }
84///
85/// impl<K, V> KeyedRef for MyMap<K, V> {
86///   type KeyRef<'a>
87///   = &'a K where Self: 'a;
88///
89///   covariant_key_ref!();
90/// }
91/// ```
92#[macro_export]
93macro_rules! covariant_key_ref {
94	() => {
95		fn upcast_key_ref<'short, 'long: 'short>(r: Self::KeyRef<'long>) -> Self::KeyRef<'short>
96		where
97			Self: 'long,
98		{
99			r
100		}
101	};
102}
103
104/// Automatically defines the `CollectionMut::upcast_item_mut` function using the
105/// covariance of the `ItemMut<'a>` type with regards to `'a`.
106///
107/// ## Example
108///
109/// ```
110/// use cc_traits::{Collection, CollectionMut, SimpleCollectionMut, covariant_item_mut, simple_collection_mut};
111///
112/// pub struct MyVec<T>(Vec<T>);
113///
114/// impl<T> Collection for MyVec<T> {
115///   type Item = T;
116/// }
117///
118/// impl<T> CollectionMut for MyVec<T> {
119///   type ItemMut<'a>
120///   = &'a mut T where Self: 'a;
121///
122///   covariant_item_mut!();
123/// }
124///
125/// impl<T> SimpleCollectionMut for MyVec<T> {
126///   simple_collection_mut!();
127/// }
128/// ```
129#[macro_export]
130macro_rules! simple_collection_mut {
131	() => {
132		fn into_mut<'r>(r: Self::ItemMut<'r>) -> &'r mut Self::Item
133		where
134			Self: 'r,
135		{
136			r
137		}
138	};
139}
140
141/// Automatically defines the `SimpleCollectionRef::into_ref` function.
142///
143/// ## Example
144///
145/// ```
146/// use cc_traits::{Collection, CollectionRef, SimpleCollectionRef, covariant_item_ref, simple_collection_ref};
147///
148/// pub struct MyVec<T>(Vec<T>);
149///
150/// impl<T> Collection for MyVec<T> {
151///   type Item = T;
152/// }
153///
154/// impl<T> CollectionRef for MyVec<T> {
155///   type ItemRef<'a>
156///   = &'a T where Self: 'a;
157///
158///   covariant_item_ref!();
159/// }
160///
161/// impl<T> SimpleCollectionRef for MyVec<T> {
162///   simple_collection_ref!();
163/// }
164/// ```
165#[macro_export]
166macro_rules! simple_collection_ref {
167	() => {
168		fn into_ref<'r>(r: Self::ItemRef<'r>) -> &'r Self::Item
169		where
170			Self: 'r,
171		{
172			r
173		}
174	};
175}
176
177/// Automatically defines the `SimpleKeyedRef::into_ref` function.
178///
179/// ## Example
180///
181/// ```
182/// use cc_traits::{Collection, Keyed, KeyedRef, SimpleKeyedRef, covariant_key_ref, simple_keyed_ref};
183///
184/// pub struct MyMap<K, V>(std::collections::HashMap<K, V>);
185///
186/// impl<K, V> Collection for MyMap<K, V> {
187///   type Item = V;
188/// }
189///
190/// impl<K, V> Keyed for MyMap<K, V> {
191///   type Key = K;
192/// }
193///
194/// impl<K, V> KeyedRef for MyMap<K, V> {
195///   type KeyRef<'a>
196///   = &'a K where Self: 'a;
197///
198///   covariant_key_ref!();
199/// }
200///
201/// impl<K, V> SimpleKeyedRef for MyMap<K, V> {
202///   simple_keyed_ref!();
203/// }
204/// ```
205#[macro_export]
206macro_rules! simple_keyed_ref {
207	() => {
208		fn into_ref<'r>(r: Self::KeyRef<'r>) -> &'r Self::Key
209		where
210			Self: 'r,
211		{
212			r
213		}
214	};
215}