@@ -19,11 +19,12 @@ use crate::{
19
19
alloc:: { alloc, dealloc, Layout } ,
20
20
vec:: Vec ,
21
21
} ,
22
+ world:: ComponentId ,
22
23
Entity ,
23
24
} ;
24
25
use bevy_utils:: { HashMap , HashMapExt } ;
25
26
use core:: {
26
- any:: { type_name , TypeId } ,
27
+ any:: TypeId ,
27
28
cell:: UnsafeCell ,
28
29
mem,
29
30
ptr:: { self , NonNull } ,
@@ -38,7 +39,7 @@ use crate::{borrow::AtomicBorrow, query::Fetch, Access, Component, Query};
38
39
#[ derive( Debug ) ]
39
40
pub struct Archetype {
40
41
types : Vec < TypeInfo > ,
41
- state : HashMap < TypeId , TypeState > ,
42
+ state : HashMap < ComponentId , TypeState > ,
42
43
len : usize ,
43
44
entities : Vec < Entity > ,
44
45
// UnsafeCell allows unique references into `data` to be constructed while shared references
@@ -93,23 +94,23 @@ impl Archetype {
93
94
#[ allow( missing_docs) ]
94
95
#[ inline]
95
96
pub fn has < T : Component > ( & self ) -> bool {
96
- self . has_dynamic ( TypeId :: of :: < T > ( ) )
97
+ self . has_dynamic ( TypeId :: of :: < T > ( ) . into ( ) )
97
98
}
98
99
99
100
#[ allow( missing_docs) ]
100
101
#[ inline]
101
- pub fn has_type ( & self , ty : TypeId ) -> bool {
102
+ pub fn has_component ( & self , ty : ComponentId ) -> bool {
102
103
self . has_dynamic ( ty)
103
104
}
104
105
105
- pub ( crate ) fn has_dynamic ( & self , id : TypeId ) -> bool {
106
+ pub ( crate ) fn has_dynamic ( & self , id : ComponentId ) -> bool {
106
107
self . state . contains_key ( & id)
107
108
}
108
109
109
110
#[ allow( missing_docs) ]
110
111
#[ inline]
111
112
pub fn get < T : Component > ( & self ) -> Option < NonNull < T > > {
112
- let state = self . state . get ( & TypeId :: of :: < T > ( ) ) ?;
113
+ let state = self . state . get ( & TypeId :: of :: < T > ( ) . into ( ) ) ?;
113
114
Some ( unsafe {
114
115
NonNull :: new_unchecked (
115
116
( * self . data . get ( ) ) . as_ptr ( ) . add ( state. offset ) . cast :: < T > ( ) as * mut T
@@ -120,7 +121,7 @@ impl Archetype {
120
121
#[ allow( missing_docs) ]
121
122
#[ inline]
122
123
pub fn get_with_type_state < T : Component > ( & self ) -> Option < ( NonNull < T > , & TypeState ) > {
123
- let state = self . state . get ( & TypeId :: of :: < T > ( ) ) ?;
124
+ let state = self . state . get ( & TypeId :: of :: < T > ( ) . into ( ) ) ?;
124
125
Some ( unsafe {
125
126
(
126
127
NonNull :: new_unchecked (
@@ -132,51 +133,71 @@ impl Archetype {
132
133
}
133
134
134
135
#[ allow( missing_docs) ]
135
- pub fn get_type_state ( & self , ty : TypeId ) -> Option < & TypeState > {
136
+ pub fn get_type_state ( & self , ty : ComponentId ) -> Option < & TypeState > {
136
137
self . state . get ( & ty)
137
138
}
138
139
139
140
#[ allow( missing_docs) ]
140
- pub fn get_type_state_mut ( & mut self , ty : TypeId ) -> Option < & mut TypeState > {
141
+ pub fn get_type_state_mut ( & mut self , ty : ComponentId ) -> Option < & mut TypeState > {
141
142
self . state . get_mut ( & ty)
142
143
}
143
144
144
145
#[ allow( missing_docs) ]
145
146
#[ inline]
146
147
pub fn borrow < T : Component > ( & self ) {
147
- if self
148
- . state
149
- . get ( & TypeId :: of :: < T > ( ) )
150
- . map_or ( false , |x| !x. borrow . borrow ( ) )
151
- {
152
- panic ! ( "{} already borrowed uniquely" , type_name:: <T >( ) ) ;
148
+ self . borrow_component ( std:: any:: TypeId :: of :: < T > ( ) . into ( ) )
149
+ }
150
+
151
+ #[ allow( missing_docs) ]
152
+ #[ inline]
153
+ pub fn borrow_component ( & self , id : ComponentId ) {
154
+ if self . state . get ( & id) . map_or ( false , |x| !x. borrow . borrow ( ) ) {
155
+ panic ! ( "{:?} already borrowed uniquely" , id) ;
153
156
}
154
157
}
155
158
156
159
#[ allow( missing_docs) ]
157
160
#[ inline]
158
161
pub fn borrow_mut < T : Component > ( & self ) {
162
+ self . borrow_component_mut ( std:: any:: TypeId :: of :: < T > ( ) . into ( ) )
163
+ }
164
+
165
+ #[ allow( missing_docs) ]
166
+ #[ inline]
167
+ pub fn borrow_component_mut ( & self , id : ComponentId ) {
159
168
if self
160
169
. state
161
- . get ( & TypeId :: of :: < T > ( ) )
170
+ . get ( & id )
162
171
. map_or ( false , |x| !x. borrow . borrow_mut ( ) )
163
172
{
164
- panic ! ( "{} already borrowed" , type_name :: < T > ( ) ) ;
173
+ panic ! ( "{:? } already borrowed" , id ) ;
165
174
}
166
175
}
167
176
168
177
#[ allow( missing_docs) ]
169
178
#[ inline]
170
179
pub fn release < T : Component > ( & self ) {
171
- if let Some ( x) = self . state . get ( & TypeId :: of :: < T > ( ) ) {
180
+ self . release_component ( std:: any:: TypeId :: of :: < T > ( ) . into ( ) ) ;
181
+ }
182
+
183
+ #[ allow( missing_docs) ]
184
+ #[ inline]
185
+ pub fn release_component ( & self , id : ComponentId ) {
186
+ if let Some ( x) = self . state . get ( & id) {
172
187
x. borrow . release ( ) ;
173
188
}
174
189
}
175
190
176
191
#[ allow( missing_docs) ]
177
192
#[ inline]
178
193
pub fn release_mut < T : Component > ( & self ) {
179
- if let Some ( x) = self . state . get ( & TypeId :: of :: < T > ( ) ) {
194
+ self . release_component_mut ( std:: any:: TypeId :: of :: < T > ( ) . into ( ) )
195
+ }
196
+
197
+ #[ allow( missing_docs) ]
198
+ #[ inline]
199
+ pub fn release_component_mut ( & self , id : ComponentId ) {
200
+ if let Some ( x) = self . state . get ( & id) {
180
201
x. borrow . release_mut ( ) ;
181
202
}
182
203
}
@@ -216,7 +237,7 @@ impl Archetype {
216
237
/// `index` must be in-bounds
217
238
pub ( crate ) unsafe fn get_dynamic (
218
239
& self ,
219
- ty : TypeId ,
240
+ ty : ComponentId ,
220
241
size : usize ,
221
242
index : usize ,
222
243
) -> Option < NonNull < u8 > > {
@@ -356,7 +377,7 @@ impl Archetype {
356
377
pub ( crate ) unsafe fn move_to (
357
378
& mut self ,
358
379
index : usize ,
359
- mut f : impl FnMut ( * mut u8 , TypeId , usize , bool , bool ) ,
380
+ mut f : impl FnMut ( * mut u8 , ComponentId , usize , bool , bool ) ,
360
381
) -> Option < Entity > {
361
382
let last = self . len - 1 ;
362
383
for ty in & self . types {
@@ -400,7 +421,7 @@ impl Archetype {
400
421
pub unsafe fn put_dynamic (
401
422
& mut self ,
402
423
component : * mut u8 ,
403
- ty : TypeId ,
424
+ ty : ComponentId ,
404
425
size : usize ,
405
426
index : usize ,
406
427
added : bool ,
@@ -484,7 +505,7 @@ impl TypeState {
484
505
/// Metadata required to store a component
485
506
#[ derive( Debug , Copy , Clone ) ]
486
507
pub struct TypeInfo {
487
- id : TypeId ,
508
+ id : ComponentId ,
488
509
layout : Layout ,
489
510
drop : unsafe fn ( * mut u8 ) ,
490
511
}
@@ -497,15 +518,15 @@ impl TypeInfo {
497
518
}
498
519
499
520
Self {
500
- id : TypeId :: of :: < T > ( ) ,
521
+ id : TypeId :: of :: < T > ( ) . into ( ) ,
501
522
layout : Layout :: new :: < T > ( ) ,
502
523
drop : drop_ptr :: < T > ,
503
524
}
504
525
}
505
526
506
527
#[ allow( missing_docs) ]
507
528
#[ inline]
508
- pub fn id ( & self ) -> TypeId {
529
+ pub fn id ( & self ) -> ComponentId {
509
530
self . id
510
531
}
511
532
@@ -527,7 +548,7 @@ impl PartialOrd for TypeInfo {
527
548
}
528
549
529
550
impl Ord for TypeInfo {
530
- /// Order by alignment, descending. Ties broken with TypeId .
551
+ /// Order by alignment, descending. Ties broken with ComponentId .
531
552
fn cmp ( & self , other : & Self ) -> core:: cmp:: Ordering {
532
553
self . layout
533
554
. align ( )
0 commit comments