@@ -140,7 +140,8 @@ impl ComponentSparseSet {
140
140
141
141
/// Removes all of the values stored within.
142
142
pub ( crate ) fn clear ( & mut self ) {
143
- unsafe { self . dense . clear ( self . entities . len ( ) ) } ;
143
+ // SAFETY: This is using the size of the ComponentSparseSet.
144
+ unsafe { self . dense . clear ( self . len ( ) ) } ;
144
145
self . entities . clear ( ) ;
145
146
self . sparse . clear ( ) ;
146
147
}
@@ -185,8 +186,10 @@ impl ComponentSparseSet {
185
186
186
187
let _guard = AbortOnPanic ;
187
188
if capacity != self . entities . capacity ( ) {
189
+ // SAFETY: An entity was just pushed onto `entities`, its capacity cannot be zero.
188
190
let new_capacity = unsafe { NonZero :: new_unchecked ( self . entities . capacity ( ) ) } ;
189
191
if let Some ( capacity) = NonZero :: new ( capacity) {
192
+ // SAFETY: This is using the layout of the previous allocation.
190
193
unsafe { self . dense . realloc ( capacity, new_capacity) } ;
191
194
} else {
192
195
self . dense . alloc ( new_capacity) ;
@@ -333,31 +336,37 @@ impl ComponentSparseSet {
333
336
if dense_index. index ( ) >= last {
334
337
#[ cfg( debug_assertions) ]
335
338
assert_eq ! ( dense_index. index( ) , last) ;
336
- // SAFETY: TODO
339
+ // SAFETY: This is strictly decreasing the length, so it cannot outgrow
340
+ // it also cannot underflow as an item was just removed from the sparse array.
337
341
unsafe { self . entities . set_len ( last) } ;
338
- // SAFETY: TODO
342
+ // SAFETY: `last` is guaranteed to be the last element in `dense` as the length is synced with
343
+ // the `entities` store.
339
344
unsafe {
340
345
self . dense
341
346
. get_data_unchecked ( dense_index)
342
347
. assert_unique ( )
343
348
. promote ( )
344
349
}
345
350
} else {
346
- // SAFETY: TODO
351
+ // SAFETY: The above check ensures that `dense_index` and the last element are not
352
+ // overlapping, and thus also within bounds.
347
353
unsafe {
348
354
self . entities
349
355
. swap_remove_nonoverlapping_unchecked ( dense_index. index ( ) ) ;
350
356
} ;
351
- // SAFETY: TODO
357
+ // SAFETY: The above check ensures that `dense_index` is in bounds.
352
358
let swapped_entity = unsafe { self . entities . get_unchecked ( dense_index. index ( ) ) } ;
353
359
#[ cfg( not( debug_assertions) ) ]
354
360
let index = swapped_entity;
355
361
#[ cfg( debug_assertions) ]
356
362
let index = swapped_entity. row ( ) ;
363
+ // SAFETY: The swapped entity was just fetched from the entity Vec, it must have already
364
+ // been inserted and in bounds.
357
365
unsafe {
358
366
* self . sparse . get_mut ( index) . debug_checked_unwrap ( ) = dense_index;
359
367
}
360
- // SAFETY: dense_index was just removed from `sparse`, which ensures that it is valid
368
+ // SAFETY: The above check ensures that `dense_index` and the last element are not
369
+ // overlapping, and thus also within bounds.
361
370
unsafe {
362
371
self . dense
363
372
. swap_remove_and_forget_unchecked_nonoverlapping ( last, dense_index)
@@ -379,27 +388,33 @@ impl ComponentSparseSet {
379
388
if dense_index. index ( ) >= last {
380
389
#[ cfg( debug_assertions) ]
381
390
assert_eq ! ( dense_index. index( ) , last) ;
382
- // SAFETY: TODO
391
+ // SAFETY: This is strictly decreasing the length, so it cannot outgrow
392
+ // it also cannot underflow as an item was just removed from the sparse array.
383
393
unsafe { self . entities . set_len ( last) } ;
384
- // SAFETY: TODO
394
+ // SAFETY: `last` is guaranteed to be the last element in `dense` as the length is synced with
395
+ // the `entities` store.
385
396
unsafe { self . dense . drop_last_component ( last) } ;
386
397
} else {
387
- // SAFETY: TODO
398
+ // SAFETY: The above check ensures that `dense_index` and the last element are not
399
+ // overlapping, and thus also within bounds.
388
400
unsafe {
389
401
self . entities
390
402
. swap_remove_nonoverlapping_unchecked ( dense_index. index ( ) ) ;
391
403
} ;
392
- // SAFETY: TODO
393
404
let swapped_entity =
405
+ // SAFETY: The above check ensures that `dense_index` is in bounds.
394
406
unsafe { self . entities . get_unchecked ( dense_index. index ( ) ) } ;
395
407
#[ cfg( not( debug_assertions) ) ]
396
408
let index = swapped_entity;
397
409
#[ cfg( debug_assertions) ]
398
410
let index = swapped_entity. row ( ) ;
411
+ // SAFETY: The swapped entity was just fetched from the entity Vec, it must have already
412
+ // been inserted and in bounds.
399
413
unsafe {
400
414
* self . sparse . get_mut ( index) . debug_checked_unwrap ( ) = dense_index;
401
415
}
402
- // SAFETY: TODO
416
+ // SAFETY: The above check ensures that `dense_index` and the last element are not
417
+ // overlapping, and thus also within bounds.
403
418
unsafe {
404
419
self . dense
405
420
. swap_remove_and_drop_unchecked_nonoverlapping ( last, dense_index) ;
@@ -410,6 +425,7 @@ impl ComponentSparseSet {
410
425
}
411
426
412
427
pub ( crate ) fn check_change_ticks ( & mut self , check : CheckChangeTicks ) {
428
+ // SAFETY: This is using the valid size of the column.
413
429
unsafe { self . dense . check_change_ticks ( self . len ( ) , check) } ;
414
430
}
415
431
}
0 commit comments