@@ -48,7 +48,7 @@ pub struct ComponentDescriptor {
48
48
}
49
49
50
50
impl ComponentDescriptor {
51
- pub unsafe fn new (
51
+ pub unsafe fn new_dynamic (
52
52
name : Option < String > ,
53
53
storage_type : StorageType ,
54
54
is_send_and_sync : bool ,
@@ -65,7 +65,7 @@ impl ComponentDescriptor {
65
65
}
66
66
}
67
67
68
- pub fn from_generic < T : Component > ( storage_type : StorageType ) -> Self {
68
+ pub fn new < T : Component > ( storage_type : StorageType ) -> Self {
69
69
Self {
70
70
name : std:: any:: type_name :: < T > ( ) . to_string ( ) ,
71
71
storage_type,
@@ -76,7 +76,7 @@ impl ComponentDescriptor {
76
76
}
77
77
}
78
78
79
- pub fn non_send_from_generic < T : ' static > ( storage_type : StorageType ) -> Self {
79
+ pub fn new_non_send_sync < T : ' static > ( storage_type : StorageType ) -> Self {
80
80
Self {
81
81
name : std:: any:: type_name :: < T > ( ) . to_string ( ) ,
82
82
storage_type,
@@ -146,12 +146,12 @@ impl SparseSetIndex for RelationKindId {
146
146
}
147
147
148
148
#[ derive( Debug ) ]
149
- pub struct RelationshipKindInfo {
149
+ pub struct RelationKindInfo {
150
150
data : ComponentDescriptor ,
151
151
id : RelationKindId ,
152
152
}
153
153
154
- impl RelationshipKindInfo {
154
+ impl RelationKindInfo {
155
155
pub fn data_layout ( & self ) -> & ComponentDescriptor {
156
156
& self . data
157
157
}
@@ -161,98 +161,96 @@ impl RelationshipKindInfo {
161
161
}
162
162
}
163
163
164
- #[ derive( Debug , Copy , Clone , Hash , Eq , PartialEq ) ]
165
- pub struct DummyInfo {
166
- rust_type : Option < TypeId > ,
167
- id : DummyId ,
168
- }
169
- #[ derive( Debug , Copy , Clone , Hash , Eq , PartialEq ) ]
170
- pub struct DummyId ( usize ) ;
171
-
172
- #[ derive( Default , Debug ) ]
173
- pub struct Relationships {
174
- kinds : Vec < RelationshipKindInfo > ,
164
+ #[ derive( Debug , Default ) ]
165
+ pub struct Components {
166
+ kinds : Vec < RelationKindInfo > ,
175
167
// These are only used by bevy. Scripting/dynamic components should
176
168
// use their own hashmap to lookup CustomId -> RelationshipKindId
177
169
component_indices : HashMap < TypeId , RelationKindId , fxhash:: FxBuildHasher > ,
178
170
resource_indices : HashMap < TypeId , RelationKindId , fxhash:: FxBuildHasher > ,
179
171
}
180
172
181
173
#[ derive( Debug , Error ) ]
182
- pub enum RelationshipsError {
183
- #[ error( "A relationship of type {0:?} already exists" ) ]
184
- RelationshipAlreadyExists ( RelationKindId ) ,
174
+ pub enum RelationsError {
175
+ #[ error( "A component of type {name:?} ({type_id:?}) already exists" ) ]
176
+ ComponentAlreadyExists { type_id : TypeId , name : String } ,
177
+ #[ error( "A resource of type {name:?} ({type_id:?}) already exists" ) ]
178
+ ResourceAlreadyExists { type_id : TypeId , name : String } ,
185
179
}
186
180
187
- impl Relationships {
188
- pub fn new_component_kind (
189
- & mut self ,
190
- type_id : TypeId ,
191
- layout : ComponentDescriptor ,
192
- ) -> & RelationshipKindInfo {
181
+ impl Components {
182
+ pub fn new_relation_kind ( & mut self , layout : ComponentDescriptor ) -> & RelationKindInfo {
183
+ let id = RelationKindId ( self . kinds . len ( ) ) ;
184
+ self . kinds . push ( RelationKindInfo { data : layout, id } ) ;
185
+ self . kinds . last ( ) . unwrap ( )
186
+ }
187
+
188
+ pub fn new_component_kind ( & mut self , layout : ComponentDescriptor ) -> & RelationKindInfo {
193
189
let id = RelationKindId ( self . kinds . len ( ) ) ;
194
- let prev_inserted = self . component_indices . insert ( type_id, id) ;
190
+ let prev_inserted = self . component_indices . insert ( layout . type_id ( ) . unwrap ( ) , id) ;
195
191
assert ! ( prev_inserted. is_none( ) ) ;
196
- self . kinds . push ( RelationshipKindInfo { data : layout, id } ) ;
192
+ self . kinds . push ( RelationKindInfo { data : layout, id } ) ;
197
193
self . kinds . last ( ) . unwrap ( )
198
194
}
199
195
200
- pub fn new_resource_kind (
201
- & mut self ,
202
- type_id : TypeId ,
203
- layout : ComponentDescriptor ,
204
- ) -> & RelationshipKindInfo {
196
+ pub fn new_resource_kind ( & mut self , layout : ComponentDescriptor ) -> & RelationKindInfo {
205
197
let id = RelationKindId ( self . kinds . len ( ) ) ;
206
- let prev_inserted = self . resource_indices . insert ( type_id, id) ;
198
+ let prev_inserted = self . resource_indices . insert ( layout . type_id ( ) . unwrap ( ) , id) ;
207
199
assert ! ( prev_inserted. is_none( ) ) ;
208
- self . kinds . push ( RelationshipKindInfo { data : layout, id } ) ;
200
+ self . kinds . push ( RelationKindInfo { data : layout, id } ) ;
209
201
self . kinds . last ( ) . unwrap ( )
210
202
}
211
203
212
- pub fn get_component_kind ( & self , type_id : TypeId ) -> Option < & RelationshipKindInfo > {
204
+ pub fn get_relation_kind ( & self , id : RelationKindId ) -> & RelationKindInfo {
205
+ self . kinds . get ( id. 0 ) . unwrap ( )
206
+ }
207
+
208
+ pub fn get_component_kind ( & self , type_id : TypeId ) -> Option < & RelationKindInfo > {
213
209
let id = self . component_indices . get ( & type_id) . copied ( ) ?;
214
210
Some ( & self . kinds [ id. 0 ] )
215
211
}
216
212
217
- pub fn get_resource_kind ( & self , type_id : TypeId ) -> Option < & RelationshipKindInfo > {
213
+ pub fn get_resource_kind ( & self , type_id : TypeId ) -> Option < & RelationKindInfo > {
218
214
let id = self . resource_indices . get ( & type_id) . copied ( ) ?;
219
215
Some ( & self . kinds [ id. 0 ] )
220
216
}
221
217
222
218
pub fn get_component_kind_or_insert (
223
219
& mut self ,
224
- type_id : TypeId ,
225
220
layout : ComponentDescriptor ,
226
- ) -> & RelationshipKindInfo {
227
- match self . component_indices . get ( & type_id) . copied ( ) {
221
+ ) -> & RelationKindInfo {
222
+ match self
223
+ . component_indices
224
+ . get ( & layout. type_id ( ) . unwrap ( ) )
225
+ . copied ( )
226
+ {
228
227
Some ( kind) => & self . kinds [ kind. 0 ] ,
229
- None => self . new_component_kind ( type_id , layout) ,
228
+ None => self . new_component_kind ( layout) ,
230
229
}
231
230
}
232
231
233
232
pub fn get_resource_kind_or_insert (
234
233
& mut self ,
235
- type_id : TypeId ,
236
234
layout : ComponentDescriptor ,
237
- ) -> & RelationshipKindInfo {
238
- match self . resource_indices . get ( & type_id) . copied ( ) {
235
+ ) -> & RelationKindInfo {
236
+ match self
237
+ . resource_indices
238
+ . get ( & layout. type_id ( ) . unwrap ( ) )
239
+ . copied ( )
240
+ {
239
241
Some ( kind) => & self . kinds [ kind. 0 ] ,
240
- None => self . new_resource_kind ( type_id , layout) ,
242
+ None => self . new_resource_kind ( layout) ,
241
243
}
242
244
}
243
245
244
246
#[ inline]
245
- pub fn kinds_len ( & self ) -> usize {
247
+ pub fn len ( & self ) -> usize {
246
248
self . kinds . len ( )
247
249
}
248
250
249
251
#[ inline]
250
- pub fn kinds_is_empty ( & self ) -> bool {
251
- self . kinds . len ( ) == 0
252
- }
253
-
254
- pub fn get_relation_kind ( & self , id : RelationKindId ) -> Option < & RelationshipKindInfo > {
255
- self . kinds . get ( id. 0 )
252
+ pub fn is_empty ( & self ) -> bool {
253
+ self . kinds . is_empty ( )
256
254
}
257
255
}
258
256
0 commit comments