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
#![no_std]
use core::any::TypeId;
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
pub mod provider;
pub mod tag;
pub trait Tag<'a>: Sized + 'static {
type Type: 'a;
}
mod private {
pub trait Sealed {}
}
pub unsafe trait Tagged<'a>: private::Sealed + 'a {
fn tag_id(&self) -> TypeId;
}
#[repr(transparent)]
struct TaggedImpl<'a, I>
where
I: Tag<'a>,
{
_value: I::Type,
}
impl<'a, I> private::Sealed for TaggedImpl<'a, I> where I: Tag<'a> {}
unsafe impl<'a, I> Tagged<'a> for TaggedImpl<'a, I>
where
I: Tag<'a>,
{
fn tag_id(&self) -> TypeId {
TypeId::of::<I>()
}
}
impl<'a> dyn Tagged<'a> {
pub fn tag_ref<I>(value: &I::Type) -> &dyn Tagged<'a>
where
I: Tag<'a>,
{
unsafe { &*(value as *const I::Type as *const TaggedImpl<'a, I>) }
}
pub fn tag_mut<I>(value: &mut I::Type) -> &mut dyn Tagged<'a>
where
I: Tag<'a>,
{
unsafe { &mut *(value as *mut I::Type as *mut TaggedImpl<'a, I>) }
}
#[cfg(feature = "alloc")]
pub fn tag_box<I>(value: Box<I::Type>) -> Box<dyn Tagged<'a>>
where
I: Tag<'a>,
{
unsafe { Box::from_raw(Box::into_raw(value) as *mut TaggedImpl<'a, I>) }
}
#[inline]
pub fn is<I>(&self) -> bool
where
I: Tag<'a>,
{
self.tag_id() == TypeId::of::<I>()
}
#[inline]
pub fn downcast_ref<I>(&self) -> Option<&I::Type>
where
I: Tag<'a>,
{
if self.is::<I>() {
unsafe { Some(&*(self as *const dyn Tagged<'a> as *const I::Type)) }
} else {
None
}
}
#[inline]
pub fn downcast_mut<I>(&mut self) -> Option<&mut I::Type>
where
I: Tag<'a>,
{
if self.is::<I>() {
unsafe { Some(&mut *(self as *mut dyn Tagged<'a> as *mut I::Type)) }
} else {
None
}
}
#[inline]
#[cfg(feature = "alloc")]
pub fn downcast_box<I>(self: Box<Self>) -> Result<Box<I::Type>, Box<Self>>
where
I: Tag<'a>,
{
if self.is::<I>() {
unsafe {
let raw: *mut dyn Tagged<'a> = Box::into_raw(self);
Ok(Box::from_raw(raw as *mut I::Type))
}
} else {
Err(self)
}
}
}