Make ComponentId typed in Components (#10770)

Trying to understand code. Types help.
This commit is contained in:
Stepan Koltsov 2023-12-05 01:54:27 +00:00 committed by GitHub
parent c9368734d2
commit 2653adf5d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 {
@ -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`.
@ -483,8 +482,7 @@ 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]
@ -492,14 +490,14 @@ impl Components {
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.
@ -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`.
@ -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`.
@ -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.