Skip to content

Commit

Permalink
Make ComponentId typed in Components (#10770)
Browse files Browse the repository at this point in the history
Trying to understand code. Types help.
  • Loading branch information
stepancheg committed Dec 5, 2023
1 parent c936873 commit 2653adf
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ impl ComponentDescriptor {
#[derive(Debug, Default)]
pub struct Components {
components: Vec<ComponentInfo>,
indices: TypeIdMap<usize>,
resource_indices: TypeIdMap<usize>,
indices: TypeIdMap<ComponentId>,
resource_indices: TypeIdMap<ComponentId>,
}

impl Components {
Expand All @@ -461,10 +461,9 @@ impl Components {
components,
..
} = self;
let index = indices.entry(type_id).or_insert_with(|| {
*indices.entry(type_id).or_insert_with(|| {
Components::init_component_inner(components, storages, ComponentDescriptor::new::<T>())
});
ComponentId(*index)
})
}

/// Initializes a component described by `descriptor`.
Expand All @@ -483,23 +482,22 @@ impl Components {
storages: &mut Storages,
descriptor: ComponentDescriptor,
) -> ComponentId {
let index = Components::init_component_inner(&mut self.components, storages, descriptor);
ComponentId(index)
Components::init_component_inner(&mut self.components, storages, descriptor)
}

#[inline]
fn init_component_inner(
components: &mut Vec<ComponentInfo>,
storages: &mut Storages,
descriptor: ComponentDescriptor,
) -> usize {
let index = components.len();
let info = ComponentInfo::new(ComponentId(index), descriptor);
) -> ComponentId {
let component_id = ComponentId(components.len());
let info = ComponentInfo::new(component_id, descriptor);
if info.descriptor.storage_type == StorageType::SparseSet {
storages.sparse_sets.get_or_insert(&info);
}
components.push(info);
index
component_id
}

/// Returns the number of components registered with this instance.
Expand Down Expand Up @@ -543,7 +541,7 @@ impl Components {
/// Type-erased equivalent of [`Components::component_id()`].
#[inline]
pub fn get_id(&self, type_id: TypeId) -> Option<ComponentId> {
self.indices.get(&type_id).map(|index| ComponentId(*index))
self.indices.get(&type_id).copied()
}

/// Returns the [`ComponentId`] of the given [`Component`] type `T`.
Expand Down Expand Up @@ -581,9 +579,7 @@ impl Components {
/// Type-erased equivalent of [`Components::resource_id()`].
#[inline]
pub fn get_resource_id(&self, type_id: TypeId) -> Option<ComponentId> {
self.resource_indices
.get(&type_id)
.map(|index| ComponentId(*index))
self.resource_indices.get(&type_id).copied()
}

/// Returns the [`ComponentId`] of the given [`Resource`] type `T`.
Expand Down Expand Up @@ -657,14 +653,12 @@ impl Components {
func: impl FnOnce() -> ComponentDescriptor,
) -> ComponentId {
let components = &mut self.components;
let index = self.resource_indices.entry(type_id).or_insert_with(|| {
*self.resource_indices.entry(type_id).or_insert_with(|| {
let descriptor = func();
let index = components.len();
components.push(ComponentInfo::new(ComponentId(index), descriptor));
index
});

ComponentId(*index)
let component_id = ComponentId(components.len());
components.push(ComponentInfo::new(component_id, descriptor));
component_id
})
}

/// Gets an iterator over all components registered with this instance.
Expand Down

0 comments on commit 2653adf

Please sign in to comment.