Make Archetypes.archetype_component_count private (#10774)

Make more clear where it is used and how.
This commit is contained in:
Stepan Koltsov 2024-02-03 00:07:50 +00:00 committed by GitHub
parent c99ca79825
commit c55a5ba40b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 16 deletions

View File

@ -588,13 +588,6 @@ struct ArchetypeComponents {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ArchetypeComponentId(usize); pub struct ArchetypeComponentId(usize);
impl ArchetypeComponentId {
#[inline]
pub(crate) const fn new(index: usize) -> Self {
Self(index)
}
}
impl SparseSetIndex for ArchetypeComponentId { impl SparseSetIndex for ArchetypeComponentId {
#[inline] #[inline]
fn sparse_set_index(&self) -> usize { fn sparse_set_index(&self) -> usize {
@ -614,7 +607,7 @@ impl SparseSetIndex for ArchetypeComponentId {
/// [module level documentation]: crate::archetype /// [module level documentation]: crate::archetype
pub struct Archetypes { pub struct Archetypes {
pub(crate) archetypes: Vec<Archetype>, pub(crate) archetypes: Vec<Archetype>,
pub(crate) archetype_component_count: usize, archetype_component_count: usize,
by_components: bevy_utils::HashMap<ArchetypeComponents, ArchetypeId>, by_components: bevy_utils::HashMap<ArchetypeComponents, ArchetypeId>,
} }
@ -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 {
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 /// Fetches an immutable reference to an [`Archetype`] using its
/// ID. Returns `None` if no corresponding archetype exists. /// ID. Returns `None` if no corresponding archetype exists.
#[inline] #[inline]

View File

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