all destructs increment the generation

This commit is contained in:
Elliott Pierce 2025-05-31 17:49:13 -04:00
parent 0c194b734a
commit 180b349721
3 changed files with 11 additions and 22 deletions

View File

@ -299,9 +299,12 @@ impl<'m> SceneEntityMapper<'m> {
/// safely allocate any more references, this method takes ownership of `self` in order to render it unusable.
pub fn finish(self, world: &mut World) {
// SAFETY: We never constructed the entity and never released it for something else to construct.
unsafe {
world.release_generations_unchecked(self.dead_start.row(), self.generations);
}
let reuse_row = unsafe {
world
.entities
.mark_free(self.dead_start.row(), self.generations)
};
world.allocator.free(reuse_row);
}
/// Creates an [`SceneEntityMapper`] from a provided [`World`] and [`EntityHashMap<Entity>`], then calls the

View File

@ -2373,6 +2373,7 @@ impl<'w> EntityWorldMut<'w> {
/// Destructs the entity, without releasing it.
/// This may be later [`constructed`](Self::construct).
/// Note that this still increases the generation to differentiate different constructions of the same row.
#[track_caller]
pub fn destruct(&mut self) -> &mut Self {
self.destruct_with_caller(MaybeLocation::caller())
@ -2517,7 +2518,10 @@ impl<'w> EntityWorldMut<'w> {
}
// finish
// SAFETY: We just destructed it.
self.entity = unsafe { self.world.entities.mark_free(self.entity.row(), 1) };
self.world.flush();
self.update_location(); // In case some command re-constructs this entity.
self
}
@ -2536,11 +2540,7 @@ impl<'w> EntityWorldMut<'w> {
pub(crate) fn despawn_with_caller(mut self, caller: MaybeLocation) {
self.destruct_with_caller(caller);
// SAFETY: We just destructed.
unsafe {
self.world
.release_generations_unchecked(self.entity.row(), 1);
}
self.world.allocator.free(self.entity);
}
/// Ensures any commands triggered by the actions of Self are applied, equivalent to [`World::flush`]

View File

@ -1553,20 +1553,6 @@ impl World {
Ok(entity)
}
/// Releases `entity` to be reused.
///
/// # Safety
///
/// It must have been destructed.
pub(crate) unsafe fn release_generations_unchecked(
&mut self,
entity: EntityRow,
generations: u32,
) {
self.allocator
.free(self.entities.mark_free(entity, generations));
}
/// Clears the internal component tracker state.
///
/// The world maintains some internal state about changed and removed components. This state