@@ -23,14 +23,41 @@ use crate::{
23
23
bundle:: BundleId ,
24
24
component:: { ComponentId , StorageType } ,
25
25
entity:: { Entity , EntityLocation } ,
26
- storage:: { ImmutableSparseSet , SparseArray , SparseSet , SparseSetIndex , TableId } ,
26
+ storage:: { ImmutableSparseSet , SparseArray , SparseSet , SparseSetIndex , TableId , TableRow } ,
27
27
} ;
28
28
use std:: {
29
29
collections:: HashMap ,
30
30
hash:: Hash ,
31
31
ops:: { Index , IndexMut } ,
32
32
} ;
33
33
34
+ /// An opaque location within a [`Archetype`].
35
+ ///
36
+ /// This can be used in conjunction with [`ArchetypeId`] to find the exact location
37
+ /// of an [`Entity`] within a [`World`]. An entity's archetype and index can be
38
+ /// retrieved via [`Entities::get`].
39
+ ///
40
+ /// [`World`]: crate::world::World
41
+ /// [`Entities::get`]: crate::entity::Entities
42
+ #[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
43
+ #[ repr( transparent) ]
44
+ pub struct ArchetypeRow ( usize ) ;
45
+
46
+ impl ArchetypeRow {
47
+ pub const INVALID : ArchetypeRow = ArchetypeRow ( usize:: MAX ) ;
48
+
49
+ /// Creates a `ArchetypeRow`.
50
+ pub const fn new ( index : usize ) -> Self {
51
+ Self ( index)
52
+ }
53
+
54
+ /// Gets the index of the row.
55
+ #[ inline]
56
+ pub const fn index ( self ) -> usize {
57
+ self . 0
58
+ }
59
+ }
60
+
34
61
/// An opaque unique ID for a single [`Archetype`] within a [`World`].
35
62
///
36
63
/// Archetype IDs are only valid for a given World, and are not globally unique.
@@ -226,7 +253,7 @@ impl Edges {
226
253
/// Metadata about an [`Entity`] in a [`Archetype`].
227
254
pub struct ArchetypeEntity {
228
255
entity : Entity ,
229
- table_row : usize ,
256
+ table_row : TableRow ,
230
257
}
231
258
232
259
impl ArchetypeEntity {
@@ -240,14 +267,14 @@ impl ArchetypeEntity {
240
267
///
241
268
/// [`Table`]: crate::storage::Table
242
269
#[ inline]
243
- pub const fn table_row ( & self ) -> usize {
270
+ pub const fn table_row ( & self ) -> TableRow {
244
271
self . table_row
245
272
}
246
273
}
247
274
248
275
pub ( crate ) struct ArchetypeSwapRemoveResult {
249
276
pub ( crate ) swapped_entity : Option < Entity > ,
250
- pub ( crate ) table_row : usize ,
277
+ pub ( crate ) table_row : TableRow ,
251
278
}
252
279
253
280
/// Internal metadata for a [`Component`] within a given [`Archetype`].
@@ -380,18 +407,18 @@ impl Archetype {
380
407
/// Fetches the row in the [`Table`] where the components for the entity at `index`
381
408
/// is stored.
382
409
///
383
- /// An entity's archetype index can be fetched from [`EntityLocation::index `], which
410
+ /// An entity's archetype index can be fetched from [`EntityLocation::archetype_row `], which
384
411
/// can be retrieved from [`Entities::get`].
385
412
///
386
413
/// # Panics
387
414
/// This function will panic if `index >= self.len()`.
388
415
///
389
416
/// [`Table`]: crate::storage::Table
390
- /// [`EntityLocation`]: crate::entity::EntityLocation::index
417
+ /// [`EntityLocation`]: crate::entity::EntityLocation::archetype_row
391
418
/// [`Entities::get`]: crate::entity::Entities::get
392
419
#[ inline]
393
- pub fn entity_table_row ( & self , index : usize ) -> usize {
394
- self . entities [ index] . table_row
420
+ pub fn entity_table_row ( & self , index : ArchetypeRow ) -> TableRow {
421
+ self . entities [ index. 0 ] . table_row
395
422
}
396
423
397
424
/// Updates if the components for the entity at `index` can be found
@@ -400,21 +427,25 @@ impl Archetype {
400
427
/// # Panics
401
428
/// This function will panic if `index >= self.len()`.
402
429
#[ inline]
403
- pub ( crate ) fn set_entity_table_row ( & mut self , index : usize , table_row : usize ) {
404
- self . entities [ index] . table_row = table_row;
430
+ pub ( crate ) fn set_entity_table_row ( & mut self , index : ArchetypeRow , table_row : TableRow ) {
431
+ self . entities [ index. 0 ] . table_row = table_row;
405
432
}
406
433
407
434
/// Allocates an entity to the archetype.
408
435
///
409
436
/// # Safety
410
437
/// valid component values must be immediately written to the relevant storages
411
438
/// `table_row` must be valid
412
- pub ( crate ) unsafe fn allocate ( & mut self , entity : Entity , table_row : usize ) -> EntityLocation {
439
+ pub ( crate ) unsafe fn allocate (
440
+ & mut self ,
441
+ entity : Entity ,
442
+ table_row : TableRow ,
443
+ ) -> EntityLocation {
413
444
self . entities . push ( ArchetypeEntity { entity, table_row } ) ;
414
445
415
446
EntityLocation {
416
447
archetype_id : self . id ,
417
- index : self . entities . len ( ) - 1 ,
448
+ archetype_row : ArchetypeRow ( self . entities . len ( ) - 1 ) ,
418
449
}
419
450
}
420
451
@@ -427,14 +458,14 @@ impl Archetype {
427
458
///
428
459
/// # Panics
429
460
/// This function will panic if `index >= self.len()`
430
- pub ( crate ) fn swap_remove ( & mut self , index : usize ) -> ArchetypeSwapRemoveResult {
431
- let is_last = index == self . entities . len ( ) - 1 ;
432
- let entity = self . entities . swap_remove ( index) ;
461
+ pub ( crate ) fn swap_remove ( & mut self , index : ArchetypeRow ) -> ArchetypeSwapRemoveResult {
462
+ let is_last = index. 0 == self . entities . len ( ) - 1 ;
463
+ let entity = self . entities . swap_remove ( index. 0 ) ;
433
464
ArchetypeSwapRemoveResult {
434
465
swapped_entity : if is_last {
435
466
None
436
467
} else {
437
- Some ( self . entities [ index] . entity )
468
+ Some ( self . entities [ index. 0 ] . entity )
438
469
} ,
439
470
table_row : entity. table_row ,
440
471
}
0 commit comments