Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Archetypes.archetype_component_count private #10774

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,6 @@ struct ArchetypeComponents {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ArchetypeComponentId(usize);

impl ArchetypeComponentId {
#[inline]
pub(crate) const fn new(index: usize) -> Self {
Self(index)
}
}

impl SparseSetIndex for ArchetypeComponentId {
#[inline]
fn sparse_set_index(&self) -> usize {
Expand All @@ -614,7 +607,7 @@ impl SparseSetIndex for ArchetypeComponentId {
/// [module level documentation]: crate::archetype
pub struct Archetypes {
pub(crate) archetypes: Vec<Archetype>,
pub(crate) archetype_component_count: usize,
archetype_component_count: usize,
by_components: bevy_utils::HashMap<ArchetypeComponents, ArchetypeId>,
}

Expand Down Expand Up @@ -666,6 +659,22 @@ impl Archetypes {
}
}

/// Generate and store a new [`ArchetypeComponentId`].
///
/// This simply increment the counter and return the new value.
///
/// # Panics
///
/// On archetype component id overflow.
pub(crate) fn new_archetype_component_id(&mut self) -> ArchetypeComponentId {
stepancheg marked this conversation as resolved.
Show resolved Hide resolved
let id = ArchetypeComponentId(self.archetype_component_count);
self.archetype_component_count = self
.archetype_component_count
.checked_add(1)
.expect("archetype_component_count overflow");
id
}

/// Fetches an immutable reference to an [`Archetype`] using its
/// ID. Returns `None` if no corresponding archetype exists.
#[inline]
Expand Down
12 changes: 4 additions & 8 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,13 +1679,11 @@ impl World {
&mut self,
component_id: ComponentId,
) -> &mut ResourceData<true> {
let archetype_component_count = &mut self.archetypes.archetype_component_count;
let archetypes = &mut self.archetypes;
self.storages
.resources
.initialize_with(component_id, &self.components, || {
let id = ArchetypeComponentId::new(*archetype_component_count);
*archetype_component_count += 1;
id
archetypes.new_archetype_component_id()
})
}

Expand All @@ -1696,13 +1694,11 @@ impl World {
&mut self,
component_id: ComponentId,
) -> &mut ResourceData<false> {
let archetype_component_count = &mut self.archetypes.archetype_component_count;
let archetypes = &mut self.archetypes;
self.storages
.non_send_resources
.initialize_with(component_id, &self.components, || {
let id = ArchetypeComponentId::new(*archetype_component_count);
*archetype_component_count += 1;
id
archetypes.new_archetype_component_id()
})
}

Expand Down