@@ -3,8 +3,7 @@ use crate::{
3
3
component:: { CheckChangeTicks , ComponentId , ComponentInfo , ComponentTicks , Tick , TickCells } ,
4
4
entity:: { Entity , EntityRow } ,
5
5
query:: DebugCheckedUnwrap ,
6
- storage:: VecExtensions ,
7
- storage:: { AbortOnPanic , TableRow , ThinColumn } ,
6
+ storage:: { AbortOnPanic , TableRow , ThinColumn , VecExtensions } ,
8
7
} ;
9
8
use alloc:: { boxed:: Box , vec:: Vec } ;
10
9
use bevy_ptr:: { OwningPtr , Ptr } ;
@@ -155,7 +154,7 @@ impl ComponentSparseSet {
155
154
/// Returns `true` if the sparse set contains no component values.
156
155
#[ inline]
157
156
pub fn is_empty ( & self ) -> bool {
158
- self . entities . len ( ) == 0
157
+ self . entities . is_empty ( )
159
158
}
160
159
161
160
/// Inserts the `entity` key and component `value` pair into this sparse
@@ -330,12 +329,12 @@ impl ComponentSparseSet {
330
329
self . sparse . remove ( entity. row ( ) ) . map ( |dense_index| {
331
330
#[ cfg( debug_assertions) ]
332
331
assert_eq ! ( entity, self . entities[ dense_index. index( ) ] ) ;
333
- let len = self . entities . len ( ) ;
334
- if dense_index. index ( ) >= len - 1 {
332
+ let last = self . entities . len ( ) - 1 ;
333
+ if dense_index. index ( ) >= last {
335
334
#[ cfg( debug_assertions) ]
336
- assert_eq ! ( dense_index. index( ) , len - 1 ) ;
335
+ assert_eq ! ( dense_index. index( ) , last ) ;
337
336
// SAFETY: TODO
338
- unsafe { self . entities . set_len ( dense_index . index ( ) ) } ;
337
+ unsafe { self . entities . set_len ( last ) } ;
339
338
// SAFETY: TODO
340
339
unsafe {
341
340
self . dense
@@ -361,7 +360,7 @@ impl ComponentSparseSet {
361
360
// SAFETY: dense_index was just removed from `sparse`, which ensures that it is valid
362
361
unsafe {
363
362
self . dense
364
- . swap_remove_and_forget_unchecked ( len , dense_index)
363
+ . swap_remove_and_forget_unchecked_nonoverlapping ( last , dense_index)
365
364
}
366
365
}
367
366
} )
@@ -376,14 +375,14 @@ impl ComponentSparseSet {
376
375
. map ( |dense_index| {
377
376
#[ cfg( debug_assertions) ]
378
377
assert_eq ! ( entity, self . entities[ dense_index. index( ) ] ) ;
379
- let len = self . entities . len ( ) ;
380
- if dense_index. index ( ) >= len - 1 {
378
+ let last = self . entities . len ( ) - 1 ;
379
+ if dense_index. index ( ) >= last {
381
380
#[ cfg( debug_assertions) ]
382
- assert_eq ! ( dense_index. index( ) , len - 1 ) ;
381
+ assert_eq ! ( dense_index. index( ) , last ) ;
383
382
// SAFETY: TODO
384
- unsafe { self . entities . set_len ( dense_index . index ( ) ) } ;
383
+ unsafe { self . entities . set_len ( last ) } ;
385
384
// SAFETY: TODO
386
- unsafe { self . dense . drop_last_component ( dense_index . index ( ) ) } ;
385
+ unsafe { self . dense . drop_last_component ( last ) } ;
387
386
} else {
388
387
// SAFETY: TODO
389
388
unsafe {
@@ -403,8 +402,8 @@ impl ComponentSparseSet {
403
402
// SAFETY: TODO
404
403
unsafe {
405
404
self . dense
406
- . swap_remove_and_drop_unchecked_nonoverlapping ( len , dense_index) ;
407
- } ;
405
+ . swap_remove_and_drop_unchecked_nonoverlapping ( last , dense_index) ;
406
+ }
408
407
}
409
408
} )
410
409
. is_some ( )
@@ -415,6 +414,17 @@ impl ComponentSparseSet {
415
414
}
416
415
}
417
416
417
+ impl Drop for ComponentSparseSet {
418
+ fn drop ( & mut self ) {
419
+ let len = self . entities . len ( ) ;
420
+ self . entities . clear ( ) ;
421
+ // SAFETY: `cap` and `len` are correct. `dense` is never accessed again after this call.
422
+ unsafe {
423
+ self . dense . drop ( self . entities . capacity ( ) , len) ;
424
+ }
425
+ }
426
+ }
427
+
418
428
/// A data structure that blends dense and sparse storage
419
429
///
420
430
/// `I` is the type of the indices, while `V` is the type of data stored in the dense storage.
0 commit comments