1
- mod type_info;
2
-
3
- pub use type_info:: * ;
4
-
5
1
use crate :: storage:: SparseSetIndex ;
6
2
use std:: {
7
3
alloc:: Layout ,
@@ -56,15 +52,8 @@ impl Default for StorageType {
56
52
57
53
#[ derive( Debug ) ]
58
54
pub struct ComponentInfo {
59
- name : String ,
60
55
id : ComponentId ,
61
- type_id : Option < TypeId > ,
62
- // SAFETY: This must remain private. It must only be set to "true" if this component is
63
- // actually Send + Sync
64
- is_send_and_sync : bool ,
65
- layout : Layout ,
66
- drop : unsafe fn ( * mut u8 ) ,
67
- storage_type : StorageType ,
56
+ descriptor : ComponentDescriptor ,
68
57
}
69
58
70
59
impl ComponentInfo {
@@ -75,44 +64,36 @@ impl ComponentInfo {
75
64
76
65
#[ inline]
77
66
pub fn name ( & self ) -> & str {
78
- & self . name
67
+ & self . descriptor . name
79
68
}
80
69
81
70
#[ inline]
82
71
pub fn type_id ( & self ) -> Option < TypeId > {
83
- self . type_id
72
+ self . descriptor . type_id
84
73
}
85
74
86
75
#[ inline]
87
76
pub fn layout ( & self ) -> Layout {
88
- self . layout
77
+ self . descriptor . layout
89
78
}
90
79
91
80
#[ inline]
92
81
pub fn drop ( & self ) -> unsafe fn ( * mut u8 ) {
93
- self . drop
82
+ self . descriptor . drop
94
83
}
95
84
96
85
#[ inline]
97
86
pub fn storage_type ( & self ) -> StorageType {
98
- self . storage_type
87
+ self . descriptor . storage_type
99
88
}
100
89
101
90
#[ inline]
102
91
pub fn is_send_and_sync ( & self ) -> bool {
103
- self . is_send_and_sync
92
+ self . descriptor . is_send_and_sync
104
93
}
105
94
106
95
fn new ( id : ComponentId , descriptor : ComponentDescriptor ) -> Self {
107
- ComponentInfo {
108
- id,
109
- name : descriptor. name ,
110
- storage_type : descriptor. storage_type ,
111
- type_id : descriptor. type_id ,
112
- is_send_and_sync : descriptor. is_send_and_sync ,
113
- drop : descriptor. drop ,
114
- layout : descriptor. layout ,
115
- }
96
+ ComponentInfo { id, descriptor }
116
97
}
117
98
}
118
99
@@ -142,6 +123,7 @@ impl SparseSetIndex for ComponentId {
142
123
}
143
124
}
144
125
126
+ #[ derive( Debug ) ]
145
127
pub struct ComponentDescriptor {
146
128
name : String ,
147
129
storage_type : StorageType ,
@@ -154,14 +136,30 @@ pub struct ComponentDescriptor {
154
136
}
155
137
156
138
impl ComponentDescriptor {
139
+ // SAFETY: The pointer points to a valid value of type `T` and it is safe to drop this value.
140
+ unsafe fn drop_ptr < T > ( x : * mut u8 ) {
141
+ x. cast :: < T > ( ) . drop_in_place ( )
142
+ }
143
+
157
144
pub fn new < T : Component > ( storage_type : StorageType ) -> Self {
158
145
Self {
159
146
name : std:: any:: type_name :: < T > ( ) . to_string ( ) ,
160
147
storage_type,
161
148
is_send_and_sync : true ,
162
149
type_id : Some ( TypeId :: of :: < T > ( ) ) ,
163
150
layout : Layout :: new :: < T > ( ) ,
164
- drop : TypeInfo :: drop_ptr :: < T > ,
151
+ drop : Self :: drop_ptr :: < T > ,
152
+ }
153
+ }
154
+
155
+ fn new_non_send < T : Any > ( storage_type : StorageType ) -> Self {
156
+ Self {
157
+ name : std:: any:: type_name :: < T > ( ) . to_string ( ) ,
158
+ storage_type,
159
+ is_send_and_sync : false ,
160
+ type_id : Some ( TypeId :: of :: < T > ( ) ) ,
161
+ layout : Layout :: new :: < T > ( ) ,
162
+ drop : Self :: drop_ptr :: < T > ,
165
163
}
166
164
}
167
165
@@ -181,19 +179,6 @@ impl ComponentDescriptor {
181
179
}
182
180
}
183
181
184
- impl From < TypeInfo > for ComponentDescriptor {
185
- fn from ( type_info : TypeInfo ) -> Self {
186
- Self {
187
- name : type_info. type_name ( ) . to_string ( ) ,
188
- storage_type : StorageType :: default ( ) ,
189
- is_send_and_sync : type_info. is_send_and_sync ( ) ,
190
- type_id : Some ( type_info. type_id ( ) ) ,
191
- drop : type_info. drop ( ) ,
192
- layout : type_info. layout ( ) ,
193
- }
194
- }
195
- }
196
-
197
182
#[ derive( Debug , Default ) ]
198
183
pub struct Components {
199
184
components : Vec < ComponentInfo > ,
@@ -231,7 +216,12 @@ impl Components {
231
216
232
217
#[ inline]
233
218
pub fn get_or_insert_id < T : Component > ( & mut self ) -> ComponentId {
234
- self . get_or_insert_with ( TypeId :: of :: < T > ( ) , TypeInfo :: of :: < T > )
219
+ // SAFE: The [`ComponentDescriptor`] matches the [`TypeId`]
220
+ unsafe {
221
+ self . get_or_insert_with ( TypeId :: of :: < T > ( ) , || {
222
+ ComponentDescriptor :: new :: < T > ( StorageType :: default ( ) )
223
+ } )
224
+ }
235
225
}
236
226
237
227
#[ inline]
@@ -279,42 +269,58 @@ impl Components {
279
269
280
270
#[ inline]
281
271
pub fn get_or_insert_resource_id < T : Component > ( & mut self ) -> ComponentId {
282
- self . get_or_insert_resource_with ( TypeId :: of :: < T > ( ) , TypeInfo :: of :: < T > )
272
+ // SAFE: The [`ComponentDescriptor`] matches the [`TypeId`]
273
+ unsafe {
274
+ self . get_or_insert_resource_with ( TypeId :: of :: < T > ( ) , || {
275
+ ComponentDescriptor :: new :: < T > ( StorageType :: default ( ) )
276
+ } )
277
+ }
283
278
}
284
279
285
280
#[ inline]
286
281
pub fn get_or_insert_non_send_resource_id < T : Any > ( & mut self ) -> ComponentId {
287
- self . get_or_insert_resource_with ( TypeId :: of :: < T > ( ) , TypeInfo :: of_non_send_and_sync :: < T > )
282
+ // SAFE: The [`ComponentDescriptor`] matches the [`TypeId`]
283
+ unsafe {
284
+ self . get_or_insert_resource_with ( TypeId :: of :: < T > ( ) , || {
285
+ ComponentDescriptor :: new_non_send :: < T > ( StorageType :: default ( ) )
286
+ } )
287
+ }
288
288
}
289
289
290
+ /// # Safety
291
+ ///
292
+ /// The [`ComponentDescriptor`] must match the [`TypeId`]
290
293
#[ inline]
291
- fn get_or_insert_resource_with (
294
+ unsafe fn get_or_insert_resource_with (
292
295
& mut self ,
293
296
type_id : TypeId ,
294
- func : impl FnOnce ( ) -> TypeInfo ,
297
+ func : impl FnOnce ( ) -> ComponentDescriptor ,
295
298
) -> ComponentId {
296
299
let components = & mut self . components ;
297
300
let index = self . resource_indices . entry ( type_id) . or_insert_with ( || {
298
- let type_info = func ( ) ;
301
+ let descriptor = func ( ) ;
299
302
let index = components. len ( ) ;
300
- components. push ( ComponentInfo :: new ( ComponentId ( index) , type_info . into ( ) ) ) ;
303
+ components. push ( ComponentInfo :: new ( ComponentId ( index) , descriptor ) ) ;
301
304
index
302
305
} ) ;
303
306
304
307
ComponentId ( * index)
305
308
}
306
309
310
+ /// # Safety
311
+ ///
312
+ /// The [`ComponentDescriptor`] must match the [`TypeId`]
307
313
#[ inline]
308
- pub ( crate ) fn get_or_insert_with (
314
+ pub ( crate ) unsafe fn get_or_insert_with (
309
315
& mut self ,
310
316
type_id : TypeId ,
311
- func : impl FnOnce ( ) -> TypeInfo ,
317
+ func : impl FnOnce ( ) -> ComponentDescriptor ,
312
318
) -> ComponentId {
313
319
let components = & mut self . components ;
314
320
let index = self . indices . entry ( type_id) . or_insert_with ( || {
315
- let type_info = func ( ) ;
321
+ let descriptor = func ( ) ;
316
322
let index = components. len ( ) ;
317
- components. push ( ComponentInfo :: new ( ComponentId ( index) , type_info . into ( ) ) ) ;
323
+ components. push ( ComponentInfo :: new ( ComponentId ( index) , descriptor ) ) ;
318
324
index
319
325
} ) ;
320
326
0 commit comments