/
any.rs
176 lines (157 loc) · 4.89 KB
/
any.rs
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
pub struct TypeId(*const u8, usize);
use core::{
cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
hash::{Hash, Hasher},
};
impl PartialEq for TypeId {
fn eq(&self, other: &TypeId) -> bool {
if core::ptr::eq(self.0, other.0) {
unsafe { ::__lccc::builtins::C::__builtin_assume(self.1 == other.1) }
true
} else if self.1 != other.1 {
false
} else {
let mut p0 = self.0;
let mut p1 = other.0;
// Safety:
// both p0 and p1 point to Null Terminated Multibyte Strings according to the ABI
// So before the loop passes the end of the allocation, it will read a null terminator in either or both
unsafe {
loop {
if *p0 == 0 || *p1 == 0 {
return *p1 == *p0;
} else if *p0 != *p1 {
return false;
} else {
p0 = p0.offset(1);
p1 = p1.offset(1);
}
}
}
}
}
}
impl PartialOrd for TypeId {
fn partial_cmp(&self, other: &TypeId) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for TypeId {
fn cmp(&self, other: &TypeId) -> Ordering {
let mut p0 = self.0;
let mut p1 = other.0;
// Safety:
// both p0 and p1 point to Null Terminated Multibyte Strings according to the ABI
// So before the loop passes the end of the allocation, it will read a null terminator in either or both
unsafe {
loop {
if *p0 != *p1 {
return (*p0).cmp(&*p1);
} else if *p0 == 0 {
return Ordering::Equal;
} else {
p0 = p0.offset(1);
p1 = p1.offset(1);
}
}
}
}
}
impl Eq for TypeId {}
impl Hash for TypeId {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_usize(self.0)
}
}
impl TypeId {
pub fn of<T: ?Sized + 'static>() -> Self {
let (a, b) = ::__lccc::builtins::Rust::__builtin_typeid::<T>();
Self(a, b)
}
}
use core::marker::{Send, Sync};
unsafe impl Send for TypeId {}
unsafe impl Sync for TypeId {}
pub trait Any: 'static {
fn type_id(&self) -> TypeId;
}
impl<T: ?Sized + 'static> Any for T {
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
impl dyn Any {
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id()
}
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
if self.is::<T>() {
// Safety:
// The type of self has been verified above
// &self is valid for lifetime of returned reference
Some(unsafe { &*(self as *const dyn Any as *const T) })
} else {
None
}
}
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
// Safety:
// The type of self has been verified above
// &self is valid for lifetime of returned reference
Some(unsafe { &mut *(self as *mut dyn Any as *mut T) })
} else {
None
}
}
}
impl dyn Any + Send {
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id()
}
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
if self.is::<T>() {
// Safety:
// The type of self has been verified above
// &self is valid for lifetime of returned reference
Some(unsafe { &*(self as *const (dyn Any + Send) as *const T) })
} else {
None
}
}
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
// Safety:
// The type of self has been verified above
// &self is valid for lifetime of returned reference
Some(unsafe { &mut *(self as *mut (dyn Any + Send) as *mut T) })
} else {
None
}
}
}
impl dyn Any + Send + Sync {
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id()
}
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
if self.is::<T>() {
// Safety:
// The type of self has been verified above
// &self is valid for lifetime of returned reference
Some(unsafe { &*(self as *const (dyn Any + Send + Sync) as *const T) })
} else {
None
}
}
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
// Safety:
// The type of self has been verified above
// &self is valid for lifetime of returned reference
Some(unsafe { &mut *(self as *mut (dyn Any + Send + Sync) as *mut T) })
} else {
None
}
}
}