Skip to content

Commit d132239

Browse files
authored
Misc. docs and renames for niche ECS internals (#16786)
## Objective Some structs and methods in the ECS internals have names that don't describe their purpose very well, and sometimes don't have docs either. Also, the function `remove_bundle_from_archetype` is a counterpart to `BundleInfo::add_bundle_to_archetype`, but isn't a method and is in a different file. ## Solution - Renamed the following structs and added docs: | Before | After | |----------------------|------------------------------| | `AddBundle` | `ArchetypeAfterBundleInsert` | | `InsertBundleResult` | `ArchetypeMoveType` | - Renamed the following methods: | Before | After | |---------------------------------------|----------------------------------------------| | `Edges::get_add_bundle` | `Edges::get_archetype_after_bundle_insert` | | `Edges::insert_add_bundle` | `Edges::cache_archetype_after_bundle_insert` | | `Edges::get_remove_bundle` | `Edges::get_archetype_after_bundle_remove` | | `Edges::insert_remove_bundle` | `Edges::cache_archetype_after_bundle_remove` | | `Edges::get_take_bundle` | `Edges::get_archetype_after_bundle_take` | | `Edges::insert_take_bundle` | `Edges::cache_archetype_after_bundle_take` | - Moved `remove_bundle_from_archetype` from `world/entity_ref.rs` to `BundleInfo`. I left the function in entity_ref in the first commit for comparison, look there for the diff of comments and whatnot. - Tidied up docs: - General grammar and spacing. - Made the usage of "insert" and "add" more consistent. - Removed references to information that isn't there. - Renamed `BundleInfo::add_bundle_to_archetype` to `BundleInfo::insert_bundle_into_archetype` for consistency.
1 parent ced6159 commit d132239

File tree

3 files changed

+311
-271
lines changed

3 files changed

+311
-271
lines changed

crates/bevy_ecs/src/archetype.rs

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,20 @@ impl ArchetypeId {
109109
}
110110
}
111111

112-
/// Used in [`AddBundle`] to track whether components in the bundle are newly
112+
/// Used in [`ArchetypeAfterBundleInsert`] to track whether components in the bundle are newly
113113
/// added or already existed in the entity's archetype.
114114
#[derive(Copy, Clone, Eq, PartialEq)]
115115
pub(crate) enum ComponentStatus {
116116
Added,
117117
Existing,
118118
}
119119

120-
pub(crate) struct AddBundle {
121-
/// The target archetype after the bundle is added to the source archetype
120+
/// Used in [`Edges`] to cache the result of inserting a bundle into the source archetype.
121+
pub(crate) struct ArchetypeAfterBundleInsert {
122+
/// The target archetype after the bundle is inserted into the source archetype.
122123
pub archetype_id: ArchetypeId,
123124
/// For each component iterated in the same order as the source [`Bundle`](crate::bundle::Bundle),
124-
/// indicate if the component is newly added to the target archetype or if it already existed
125+
/// indicate if the component is newly added to the target archetype or if it already existed.
125126
pub bundle_status: Vec<ComponentStatus>,
126127
/// The set of additional required components that must be initialized immediately when adding this Bundle.
127128
///
@@ -134,7 +135,7 @@ pub(crate) struct AddBundle {
134135
pub existing: Vec<ComponentId>,
135136
}
136137

137-
impl AddBundle {
138+
impl ArchetypeAfterBundleInsert {
138139
pub(crate) fn iter_inserted(&self) -> impl Iterator<Item = ComponentId> + Clone + '_ {
139140
self.added.iter().chain(self.existing.iter()).copied()
140141
}
@@ -149,18 +150,18 @@ impl AddBundle {
149150
}
150151

151152
/// This trait is used to report the status of [`Bundle`](crate::bundle::Bundle) components
152-
/// being added to a given entity, relative to that entity's original archetype.
153+
/// being inserted into a given entity, relative to that entity's original archetype.
153154
/// See [`crate::bundle::BundleInfo::write_components`] for more info.
154155
pub(crate) trait BundleComponentStatus {
155-
/// Returns the Bundle's component status for the given "bundle index"
156+
/// Returns the Bundle's component status for the given "bundle index".
156157
///
157158
/// # Safety
158159
/// Callers must ensure that index is always a valid bundle index for the
159160
/// Bundle associated with this [`BundleComponentStatus`]
160161
unsafe fn get_status(&self, index: usize) -> ComponentStatus;
161162
}
162163

163-
impl BundleComponentStatus for AddBundle {
164+
impl BundleComponentStatus for ArchetypeAfterBundleInsert {
164165
#[inline]
165166
unsafe fn get_status(&self, index: usize) -> ComponentStatus {
166167
// SAFETY: caller has ensured index is a valid bundle index for this bundle
@@ -173,7 +174,7 @@ pub(crate) struct SpawnBundleStatus;
173174
impl BundleComponentStatus for SpawnBundleStatus {
174175
#[inline]
175176
unsafe fn get_status(&self, _index: usize) -> ComponentStatus {
176-
// Components added during a spawn call are always treated as added
177+
// Components inserted during a spawn call are always treated as added.
177178
ComponentStatus::Added
178179
}
179180
}
@@ -194,37 +195,36 @@ impl BundleComponentStatus for SpawnBundleStatus {
194195
/// [`World`]: crate::world::World
195196
#[derive(Default)]
196197
pub struct Edges {
197-
add_bundle: SparseArray<BundleId, AddBundle>,
198+
insert_bundle: SparseArray<BundleId, ArchetypeAfterBundleInsert>,
198199
remove_bundle: SparseArray<BundleId, Option<ArchetypeId>>,
199200
take_bundle: SparseArray<BundleId, Option<ArchetypeId>>,
200201
}
201202

202203
impl Edges {
203-
/// Checks the cache for the target archetype when adding a bundle to the
204-
/// source archetype. For more information, see [`EntityWorldMut::insert`].
204+
/// Checks the cache for the target archetype when inserting a bundle into the
205+
/// source archetype.
205206
///
206207
/// If this returns `None`, it means there has not been a transition from
207208
/// the source archetype via the provided bundle.
208-
///
209-
/// [`EntityWorldMut::insert`]: crate::world::EntityWorldMut::insert
210209
#[inline]
211-
pub fn get_add_bundle(&self, bundle_id: BundleId) -> Option<ArchetypeId> {
212-
self.get_add_bundle_internal(bundle_id)
210+
pub fn get_archetype_after_bundle_insert(&self, bundle_id: BundleId) -> Option<ArchetypeId> {
211+
self.get_archetype_after_bundle_insert_internal(bundle_id)
213212
.map(|bundle| bundle.archetype_id)
214213
}
215214

216-
/// Internal version of `get_add_bundle` that fetches the full `AddBundle`.
215+
/// Internal version of `get_archetype_after_bundle_insert` that
216+
/// fetches the full `ArchetypeAfterBundleInsert`.
217217
#[inline]
218-
pub(crate) fn get_add_bundle_internal(&self, bundle_id: BundleId) -> Option<&AddBundle> {
219-
self.add_bundle.get(bundle_id)
218+
pub(crate) fn get_archetype_after_bundle_insert_internal(
219+
&self,
220+
bundle_id: BundleId,
221+
) -> Option<&ArchetypeAfterBundleInsert> {
222+
self.insert_bundle.get(bundle_id)
220223
}
221224

222-
/// Caches the target archetype when adding a bundle to the source archetype.
223-
/// For more information, see [`EntityWorldMut::insert`].
224-
///
225-
/// [`EntityWorldMut::insert`]: crate::world::EntityWorldMut::insert
225+
/// Caches the target archetype when inserting a bundle into the source archetype.
226226
#[inline]
227-
pub(crate) fn insert_add_bundle(
227+
pub(crate) fn cache_archetype_after_bundle_insert(
228228
&mut self,
229229
bundle_id: BundleId,
230230
archetype_id: ArchetypeId,
@@ -233,9 +233,9 @@ impl Edges {
233233
added: Vec<ComponentId>,
234234
existing: Vec<ComponentId>,
235235
) {
236-
self.add_bundle.insert(
236+
self.insert_bundle.insert(
237237
bundle_id,
238-
AddBundle {
238+
ArchetypeAfterBundleInsert {
239239
archetype_id,
240240
bundle_status,
241241
required_components,
@@ -245,52 +245,57 @@ impl Edges {
245245
);
246246
}
247247

248-
/// Checks the cache for the target archetype when removing a bundle to the
249-
/// source archetype. For more information, see [`EntityWorldMut::remove`].
248+
/// Checks the cache for the target archetype when removing a bundle from the
249+
/// source archetype.
250250
///
251251
/// If this returns `None`, it means there has not been a transition from
252252
/// the source archetype via the provided bundle.
253253
///
254254
/// If this returns `Some(None)`, it means that the bundle cannot be removed
255255
/// from the source archetype.
256-
///
257-
/// [`EntityWorldMut::remove`]: crate::world::EntityWorldMut::remove
258256
#[inline]
259-
pub fn get_remove_bundle(&self, bundle_id: BundleId) -> Option<Option<ArchetypeId>> {
257+
pub fn get_archetype_after_bundle_remove(
258+
&self,
259+
bundle_id: BundleId,
260+
) -> Option<Option<ArchetypeId>> {
260261
self.remove_bundle.get(bundle_id).cloned()
261262
}
262263

263-
/// Caches the target archetype when removing a bundle to the source archetype.
264-
/// For more information, see [`EntityWorldMut::remove`].
265-
///
266-
/// [`EntityWorldMut::remove`]: crate::world::EntityWorldMut::remove
264+
/// Caches the target archetype when removing a bundle from the source archetype.
267265
#[inline]
268-
pub(crate) fn insert_remove_bundle(
266+
pub(crate) fn cache_archetype_after_bundle_remove(
269267
&mut self,
270268
bundle_id: BundleId,
271269
archetype_id: Option<ArchetypeId>,
272270
) {
273271
self.remove_bundle.insert(bundle_id, archetype_id);
274272
}
275273

276-
/// Checks the cache for the target archetype when removing a bundle to the
277-
/// source archetype. For more information, see [`EntityWorldMut::remove`].
274+
/// Checks the cache for the target archetype when taking a bundle from the
275+
/// source archetype.
276+
///
277+
/// Unlike `remove`, `take` will only succeed if the source archetype
278+
/// contains all of the components in the bundle.
278279
///
279280
/// If this returns `None`, it means there has not been a transition from
280281
/// the source archetype via the provided bundle.
281282
///
282-
/// [`EntityWorldMut::remove`]: crate::world::EntityWorldMut::remove
283+
/// If this returns `Some(None)`, it means that the bundle cannot be taken
284+
/// from the source archetype.
283285
#[inline]
284-
pub fn get_take_bundle(&self, bundle_id: BundleId) -> Option<Option<ArchetypeId>> {
286+
pub fn get_archetype_after_bundle_take(
287+
&self,
288+
bundle_id: BundleId,
289+
) -> Option<Option<ArchetypeId>> {
285290
self.take_bundle.get(bundle_id).cloned()
286291
}
287292

288-
/// Caches the target archetype when removing a bundle to the source archetype.
289-
/// For more information, see [`EntityWorldMut::take`].
293+
/// Caches the target archetype when taking a bundle from the source archetype.
290294
///
291-
/// [`EntityWorldMut::take`]: crate::world::EntityWorldMut::take
295+
/// Unlike `remove`, `take` will only succeed if the source archetype
296+
/// contains all of the components in the bundle.
292297
#[inline]
293-
pub(crate) fn insert_take_bundle(
298+
pub(crate) fn cache_archetype_after_bundle_take(
294299
&mut self,
295300
bundle_id: BundleId,
296301
archetype_id: Option<ArchetypeId>,
@@ -577,11 +582,11 @@ impl Archetype {
577582
self.entities.reserve(additional);
578583
}
579584

580-
/// Removes the entity at `index` by swapping it out. Returns the table row the entity is stored
585+
/// Removes the entity at `row` by swapping it out. Returns the table row the entity is stored
581586
/// in.
582587
///
583588
/// # Panics
584-
/// This function will panic if `index >= self.len()`
589+
/// This function will panic if `row >= self.entities.len()`
585590
#[inline]
586591
pub(crate) fn swap_remove(&mut self, row: ArchetypeRow) -> ArchetypeSwapRemoveResult {
587592
let is_last = row.index() == self.entities.len() - 1;

0 commit comments

Comments
 (0)