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
use crate::cast_target::DynDynCastTarget;
use cfg_if::cfg_if;
use core::any::TypeId;
use core::fmt::{self, Debug};
use core::marker::Unsize;
use core::mem;
use core::ptr::DynMetadata;
#[cfg(doc)]
use crate::dyn_dyn_cast;
#[derive(Debug, Clone, Copy)]
pub struct AnyDynMetadata(*const ());
impl AnyDynMetadata {
pub const unsafe fn downcast<T: DynDynCastTarget + ?Sized>(self) -> DynMetadata<T> {
mem::transmute(self.0)
}
}
impl<T: ?Sized> const From<DynMetadata<T>> for AnyDynMetadata {
fn from(meta: DynMetadata<T>) -> Self {
unsafe { AnyDynMetadata(mem::transmute(meta)) }
}
}
unsafe impl Send for AnyDynMetadata {}
unsafe impl Sync for AnyDynMetadata {}
cfg_if! {
if #[cfg(feature = "dynamic-names")] {
type TypeName = &'static str;
const fn type_name<T: ?Sized>() -> TypeName {
core::any::type_name::<T>()
}
} else {
#[derive(Debug, Clone, Copy)]
struct TypeName;
const fn type_name<T: ?Sized>() -> TypeName { TypeName }
}
}
#[derive(Debug, Clone, Copy)]
struct DynInfo(TypeId, TypeName);
impl DynInfo {
pub const fn of<T: 'static + ?Sized>() -> DynInfo {
DynInfo(TypeId::of::<T>(), type_name::<T>())
}
pub fn type_id(self) -> TypeId {
self.0
}
#[cfg(feature = "dynamic-names")]
pub fn name(self) -> &'static str {
self.1
}
}
impl PartialEq for DynInfo {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for DynInfo {}
pub struct DynDynTableEntry {
ty: DynInfo,
meta: AnyDynMetadata,
}
impl DynDynTableEntry {
#[doc(hidden)]
pub const fn new<T: Unsize<D>, D: ?Sized + ~const DynDynCastTarget + 'static>(
) -> DynDynTableEntry {
DynDynTableEntry {
ty: DynInfo::of::<D>(),
meta: D::meta_for_ty::<T>().into(),
}
}
pub fn type_id(&self) -> TypeId {
self.ty.type_id()
}
#[cfg(feature = "dynamic-names")]
pub fn type_name(&self) -> &'static str {
self.ty.name()
}
}
impl Debug for DynDynTableEntry {
#[cfg(feature = "dynamic-names")]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"DynDynTableEntry(<{}>: {:?})",
self.type_name(),
self.meta
)
}
#[cfg(not(feature = "dynamic-names"))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DynDynTableEntry({:?}: {:?})", self.type_id(), self.meta)
}
}
#[derive(Debug, Clone, Copy)]
pub struct DynDynTable {
traits: &'static [DynDynTableEntry],
}
impl DynDynTable {
pub fn find_untyped(&self, type_id: TypeId) -> Option<AnyDynMetadata> {
self.traits
.iter()
.find(|&entry| entry.ty.type_id() == type_id)
.map(|entry| entry.meta)
}
pub fn find<D: ?Sized + DynDynCastTarget + 'static>(&self) -> Option<DynMetadata<D>> {
self.find_untyped(TypeId::of::<D>()).map(|meta| {
unsafe { meta.downcast() }
})
}
pub fn into_slice(self) -> &'static [DynDynTableEntry] {
self.traits
}
#[doc(hidden)]
pub const fn new(traits: &'static [DynDynTableEntry]) -> DynDynTable {
DynDynTable { traits }
}
}
impl IntoIterator for DynDynTable {
type Item = &'static DynDynTableEntry;
type IntoIter = DynDynTableIterator;
fn into_iter(self) -> Self::IntoIter {
DynDynTableIterator(self.traits.iter())
}
}
pub struct DynDynTableIterator(core::slice::Iter<'static, DynDynTableEntry>);
impl Iterator for DynDynTableIterator {
type Item = &'static DynDynTableEntry;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}