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
use std::{
any::TypeId,
cell::{Cell, RefCell, UnsafeCell},
collections::{BTreeMap, HashMap},
rc::Rc,
sync::Arc,
};
pub use gazebo_derive::AnyLifetime;
pub unsafe trait ProvidesStaticType {
type StaticType: 'static + ?Sized;
}
unsafe impl<'a, T: ProvidesStaticType + 'a + ?Sized> AnyLifetime<'a> for T {
fn static_type_id() -> TypeId
where
Self: Sized,
{
TypeId::of::<T::StaticType>()
}
fn static_type_of(&self) -> TypeId {
TypeId::of::<T::StaticType>()
}
}
pub unsafe trait AnyLifetime<'a>: 'a {
fn static_type_id() -> TypeId
where
Self: Sized;
fn static_type_of(&self) -> TypeId;
}
impl<'a> dyn AnyLifetime<'a> {
pub fn is<T: AnyLifetime<'a>>(&self) -> bool {
self.static_type_of() == T::static_type_id()
}
pub fn downcast_ref<T: AnyLifetime<'a>>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe { Some(&*(self as *const Self as *const T)) }
} else {
None
}
}
pub fn downcast_mut<T: AnyLifetime<'a>>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut Self as *mut T)) }
} else {
None
}
}
}
macro_rules! any_lifetime {
( $t:ty ) => {
unsafe impl $crate::any::ProvidesStaticType for $t {
type StaticType = $t;
}
};
}
any_lifetime!(());
any_lifetime!(bool);
any_lifetime!(u8);
any_lifetime!(u16);
any_lifetime!(u32);
any_lifetime!(u64);
any_lifetime!(u128);
any_lifetime!(usize);
any_lifetime!(i8);
any_lifetime!(i16);
any_lifetime!(i32);
any_lifetime!(i64);
any_lifetime!(i128);
any_lifetime!(isize);
any_lifetime!(f32);
any_lifetime!(f64);
any_lifetime!(String);
any_lifetime!(str);
unsafe impl<'a, T: ProvidesStaticType + ?Sized> ProvidesStaticType for &'a T {
type StaticType = &'static T::StaticType;
}
unsafe impl<'a, T: ProvidesStaticType + ?Sized> ProvidesStaticType for &'a mut T {
type StaticType = &'static mut T::StaticType;
}
unsafe impl<'a, T: ProvidesStaticType + ?Sized> ProvidesStaticType for *const T {
type StaticType = *const T::StaticType;
}
unsafe impl<'a, T: ProvidesStaticType + ?Sized> ProvidesStaticType for *mut T {
type StaticType = *mut T::StaticType;
}
unsafe impl<T> ProvidesStaticType for [T]
where
T: ProvidesStaticType,
T::StaticType: Sized,
{
type StaticType = [T::StaticType];
}
unsafe impl<T: ProvidesStaticType + ?Sized> ProvidesStaticType for Box<T> {
type StaticType = Box<T::StaticType>;
}
unsafe impl<T: ProvidesStaticType + ?Sized> ProvidesStaticType for Rc<T> {
type StaticType = Rc<T::StaticType>;
}
unsafe impl<T: ProvidesStaticType + ?Sized> ProvidesStaticType for Arc<T> {
type StaticType = Arc<T::StaticType>;
}
unsafe impl<T: ProvidesStaticType> ProvidesStaticType for Cell<T> {
type StaticType = Cell<T::StaticType>;
}
unsafe impl<T: ProvidesStaticType> ProvidesStaticType for UnsafeCell<T> {
type StaticType = UnsafeCell<T::StaticType>;
}
unsafe impl<T: ProvidesStaticType> ProvidesStaticType for RefCell<T> {
type StaticType = RefCell<T::StaticType>;
}
unsafe impl<T> ProvidesStaticType for Option<T>
where
T: ProvidesStaticType,
T::StaticType: Sized,
{
type StaticType = Option<T::StaticType>;
}
unsafe impl<T, E> ProvidesStaticType for Result<T, E>
where
T: ProvidesStaticType,
T::StaticType: Sized,
E: ProvidesStaticType,
E::StaticType: Sized,
{
type StaticType = Result<T::StaticType, E::StaticType>;
}
unsafe impl<T> ProvidesStaticType for Vec<T>
where
T: ProvidesStaticType,
T::StaticType: Sized,
{
type StaticType = Vec<T::StaticType>;
}
unsafe impl<K, V> ProvidesStaticType for HashMap<K, V>
where
K: ProvidesStaticType,
K::StaticType: Sized,
V: ProvidesStaticType,
V::StaticType: Sized,
{
type StaticType = HashMap<K::StaticType, V::StaticType>;
}
unsafe impl<K, V> ProvidesStaticType for BTreeMap<K, V>
where
K: ProvidesStaticType,
K::StaticType: Sized,
V: ProvidesStaticType,
V::StaticType: Sized,
{
type StaticType = BTreeMap<K::StaticType, V::StaticType>;
}
#[cfg(test)]
mod tests {
use std::fmt::Display;
use super::*;
#[allow(unused_imports)]
use crate as gazebo;
#[test]
fn test_can_convert() {
#[derive(Debug, PartialEq, AnyLifetime)]
struct Value<'a>(&'a str);
#[derive(AnyLifetime)]
struct Value2<'a>(&'a str);
fn convert_value<'a>(x: &'a Value<'a>) -> Option<&'a Value<'a>> {
<dyn AnyLifetime>::downcast_ref(x)
}
fn convert_any<'p, 'a>(x: &'p dyn AnyLifetime<'a>) -> Option<&'p Value<'a>> {
x.downcast_ref()
}
let v = Value("test");
let v2 = Value2("test");
assert_eq!(convert_value(&v), Some(&v));
assert_eq!(convert_any(&v), Some(&v));
assert_eq!(convert_any(&v2), None);
}
#[test]
fn test_any_lifetime() {
fn test<'a, A: AnyLifetime<'a>>(expected: TypeId) {
assert_eq!(expected, A::static_type_id());
}
test::<&str>(TypeId::of::<&str>());
test::<&String>(TypeId::of::<&String>());
test::<Box<str>>(TypeId::of::<Box<str>>());
}
#[test]
fn test_provides_static_type_id() {
fn test<'a, A: AnyLifetime<'a>>(expected: TypeId) {
assert_eq!(expected, A::static_type_id());
}
#[derive(AnyLifetime)]
struct Aaa;
test::<Aaa>(TypeId::of::<Aaa>());
#[derive(AnyLifetime)]
struct Bbb<'a>(&'a str);
test::<Bbb>(TypeId::of::<Bbb<'static>>());
#[derive(AnyLifetime)]
struct Bbb2<'a, 'b>(&'a str, &'b str);
test::<Bbb2>(TypeId::of::<Bbb2<'static, 'static>>());
#[derive(AnyLifetime)]
struct Ccc<X>(X);
test::<Ccc<String>>(TypeId::of::<Ccc<String>>());
#[derive(AnyLifetime)]
struct LifetimeTypeConst<'a, T, const N: usize>([&'a T; N]);
test::<LifetimeTypeConst<i32, 3>>(TypeId::of::<LifetimeTypeConst<'static, i32, 3>>());
#[derive(AnyLifetime)]
struct TypeWithConstraint<T: Display>(T);
test::<TypeWithConstraint<String>>(TypeId::of::<TypeWithConstraint<String>>());
struct TypeWhichDoesNotImplementAnyLifetime;
#[derive(AnyLifetime)]
struct TypeWithStaticLifetime<T: 'static>(T);
test::<TypeWithStaticLifetime<TypeWhichDoesNotImplementAnyLifetime>>(TypeId::of::<
TypeWithStaticLifetime<TypeWhichDoesNotImplementAnyLifetime>,
>());
}
#[test]
fn test_provides_static_type_when_type_parameter_has_bound_with_lifetime() {
trait My<'a> {}
#[derive(AnyLifetime)]
struct FooBar<'x, P: My<'x>>(&'x P);
}
}