diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index cedfdbafca203c..be142a594cd1d1 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -199,7 +199,7 @@ impl<'a> core::iter::ExactSizeIterator for ReserveEntitiesIterator<'a> {} #[derive(Debug, Default)] pub struct Entities { - pub meta: Vec, + pub(crate) meta: Vec, /// The `pending` and `free_cursor` fields describe three sets of Entity IDs /// that have been freed or are in the process of being allocated: @@ -544,6 +544,12 @@ impl Entities { } } + /// Accessor for getting the length of the vec in `self.meta` + #[inline] + pub fn meta_len(&self) -> usize { + self.meta.len() + } + #[inline] pub fn len(&self) -> u32 { self.len diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 5060a8cfbfd7c3..f673bdd36cc92f 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -135,8 +135,12 @@ impl World { } /// Retrieves this world's [Entities] collection mutably + /// + /// # Safety + /// Mutable reference must not be used to put the [`Entities`] data + /// in an invalid state for this [`World`] #[inline] - pub fn entities_mut(&mut self) -> &mut Entities { + pub unsafe fn entities_mut(&mut self) -> &mut Entities { &mut self.entities } diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 05429f026cf9d3..5d7127c0ce7b1e 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -189,7 +189,7 @@ impl Plugin for RenderPlugin { // reserve all existing app entities for use in render_app // they can only be spawned using `get_or_spawn()` - let meta_len = app_world.entities().meta.len(); + let meta_len = app_world.entities().meta_len(); render_app .world .entities() @@ -198,7 +198,7 @@ impl Plugin for RenderPlugin { // flushing as "invalid" ensures that app world entities aren't added as "empty archetype" entities by default // these entities cannot be accessed without spawning directly onto them // this _only_ works as expected because clear_entities() is called at the end of every frame. - render_app.world.entities_mut().flush_as_invalid(); + unsafe { render_app.world.entities_mut() }.flush_as_invalid(); } {