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 ,
@@ -140,32 +136,30 @@ pub struct ComponentDescriptor {
140
136
}
141
137
142
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
+
143
144
pub fn new < T : Component > ( storage_type : StorageType ) -> Self {
144
145
Self {
145
146
name : std:: any:: type_name :: < T > ( ) . to_string ( ) ,
146
147
storage_type,
147
148
is_send_and_sync : true ,
148
149
type_id : Some ( TypeId :: of :: < T > ( ) ) ,
149
150
layout : Layout :: new :: < T > ( ) ,
150
- drop : TypeInfo :: drop_ptr :: < T > ,
151
+ drop : Self :: drop_ptr :: < T > ,
151
152
}
152
153
}
153
154
154
- /// # Safety
155
- ///
156
- /// The [`TypeInfo`] must match the [`TypeId`]
157
- pub unsafe fn from_type_info (
158
- storage_type : StorageType ,
159
- type_id : Option < TypeId > ,
160
- type_info : TypeInfo ,
161
- ) -> Self {
155
+ fn new_non_send < T : Any > ( storage_type : StorageType ) -> Self {
162
156
Self {
163
- name : type_info . type_name ( ) . to_string ( ) ,
157
+ name : std :: any :: type_name :: < T > ( ) . to_string ( ) ,
164
158
storage_type,
165
- is_send_and_sync : type_info . is_send_and_sync ( ) ,
166
- type_id,
167
- layout : type_info . layout ( ) ,
168
- drop : type_info . drop ( ) ,
159
+ is_send_and_sync : false ,
160
+ type_id : Some ( TypeId :: of :: < T > ( ) ) ,
161
+ layout : Layout :: new :: < T > ( ) ,
162
+ drop : Self :: drop_ptr :: < T > ,
169
163
}
170
164
}
171
165
@@ -222,8 +216,12 @@ impl Components {
222
216
223
217
#[ inline]
224
218
pub fn get_or_insert_id < T : Component > ( & mut self ) -> ComponentId {
225
- // SAFE: The [`TypeInfo`] matches the [`TypeId`]
226
- unsafe { 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
+ }
227
225
}
228
226
229
227
#[ inline]
@@ -271,39 +269,38 @@ impl Components {
271
269
272
270
#[ inline]
273
271
pub fn get_or_insert_resource_id < T : Component > ( & mut self ) -> ComponentId {
274
- // SAFE: The [`TypeInfo`] matches the [`TypeId`]
275
- unsafe { 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
+ }
276
278
}
277
279
278
280
#[ inline]
279
281
pub fn get_or_insert_non_send_resource_id < T : Any > ( & mut self ) -> ComponentId {
280
- // SAFE: The [`TypeInfo `] matches the [`TypeId`]
282
+ // SAFE: The [`ComponentDescriptor `] matches the [`TypeId`]
281
283
unsafe {
282
- self . get_or_insert_resource_with ( TypeId :: of :: < T > ( ) , TypeInfo :: of_non_send_and_sync :: < T > )
284
+ self . get_or_insert_resource_with ( TypeId :: of :: < T > ( ) , || {
285
+ ComponentDescriptor :: new_non_send :: < T > ( StorageType :: default ( ) )
286
+ } )
283
287
}
284
288
}
285
289
286
290
/// # Safety
287
291
///
288
- /// The [`TypeInfo `] must match the [`TypeId`]
292
+ /// The [`ComponentDescriptor `] must match the [`TypeId`]
289
293
#[ inline]
290
294
unsafe fn get_or_insert_resource_with (
291
295
& mut self ,
292
296
type_id : TypeId ,
293
- func : impl FnOnce ( ) -> TypeInfo ,
297
+ func : impl FnOnce ( ) -> ComponentDescriptor ,
294
298
) -> ComponentId {
295
299
let components = & mut self . components ;
296
300
let index = self . resource_indices . entry ( type_id) . or_insert_with ( || {
297
- let type_info = func ( ) ;
301
+ let descriptor = func ( ) ;
298
302
let index = components. len ( ) ;
299
- components. push ( ComponentInfo :: new (
300
- ComponentId ( index) ,
301
- ComponentDescriptor :: from_type_info (
302
- StorageType :: default ( ) ,
303
- Some ( type_id) ,
304
- type_info,
305
- ) ,
306
- ) ) ;
303
+ components. push ( ComponentInfo :: new ( ComponentId ( index) , descriptor) ) ;
307
304
index
308
305
} ) ;
309
306
@@ -312,25 +309,18 @@ impl Components {
312
309
313
310
/// # Safety
314
311
///
315
- /// The [`TypeInfo `] must match the [`TypeId`]
312
+ /// The [`ComponentDescriptor `] must match the [`TypeId`]
316
313
#[ inline]
317
314
pub ( crate ) unsafe fn get_or_insert_with (
318
315
& mut self ,
319
316
type_id : TypeId ,
320
- func : impl FnOnce ( ) -> TypeInfo ,
317
+ func : impl FnOnce ( ) -> ComponentDescriptor ,
321
318
) -> ComponentId {
322
319
let components = & mut self . components ;
323
320
let index = self . indices . entry ( type_id) . or_insert_with ( || {
324
- let type_info = func ( ) ;
321
+ let descriptor = func ( ) ;
325
322
let index = components. len ( ) ;
326
- components. push ( ComponentInfo :: new (
327
- ComponentId ( index) ,
328
- ComponentDescriptor :: from_type_info (
329
- StorageType :: default ( ) ,
330
- Some ( type_id) ,
331
- type_info,
332
- ) ,
333
- ) ) ;
323
+ components. push ( ComponentInfo :: new ( ComponentId ( index) , descriptor) ) ;
334
324
index
335
325
} ) ;
336
326
0 commit comments