Make ComponentId typed in Components (#10770)
Trying to understand code. Types help.
This commit is contained in:
parent
c9368734d2
commit
2653adf5d8
@ -439,8 +439,8 @@ impl ComponentDescriptor {
|
|||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Components {
|
pub struct Components {
|
||||||
components: Vec<ComponentInfo>,
|
components: Vec<ComponentInfo>,
|
||||||
indices: TypeIdMap<usize>,
|
indices: TypeIdMap<ComponentId>,
|
||||||
resource_indices: TypeIdMap<usize>,
|
resource_indices: TypeIdMap<ComponentId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Components {
|
impl Components {
|
||||||
@ -461,10 +461,9 @@ impl Components {
|
|||||||
components,
|
components,
|
||||||
..
|
..
|
||||||
} = self;
|
} = 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>())
|
Components::init_component_inner(components, storages, ComponentDescriptor::new::<T>())
|
||||||
});
|
})
|
||||||
ComponentId(*index)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initializes a component described by `descriptor`.
|
/// Initializes a component described by `descriptor`.
|
||||||
@ -483,8 +482,7 @@ impl Components {
|
|||||||
storages: &mut Storages,
|
storages: &mut Storages,
|
||||||
descriptor: ComponentDescriptor,
|
descriptor: ComponentDescriptor,
|
||||||
) -> ComponentId {
|
) -> ComponentId {
|
||||||
let index = Components::init_component_inner(&mut self.components, storages, descriptor);
|
Components::init_component_inner(&mut self.components, storages, descriptor)
|
||||||
ComponentId(index)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -492,14 +490,14 @@ impl Components {
|
|||||||
components: &mut Vec<ComponentInfo>,
|
components: &mut Vec<ComponentInfo>,
|
||||||
storages: &mut Storages,
|
storages: &mut Storages,
|
||||||
descriptor: ComponentDescriptor,
|
descriptor: ComponentDescriptor,
|
||||||
) -> usize {
|
) -> ComponentId {
|
||||||
let index = components.len();
|
let component_id = ComponentId(components.len());
|
||||||
let info = ComponentInfo::new(ComponentId(index), descriptor);
|
let info = ComponentInfo::new(component_id, descriptor);
|
||||||
if info.descriptor.storage_type == StorageType::SparseSet {
|
if info.descriptor.storage_type == StorageType::SparseSet {
|
||||||
storages.sparse_sets.get_or_insert(&info);
|
storages.sparse_sets.get_or_insert(&info);
|
||||||
}
|
}
|
||||||
components.push(info);
|
components.push(info);
|
||||||
index
|
component_id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of components registered with this instance.
|
/// Returns the number of components registered with this instance.
|
||||||
@ -543,7 +541,7 @@ impl Components {
|
|||||||
/// Type-erased equivalent of [`Components::component_id()`].
|
/// Type-erased equivalent of [`Components::component_id()`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_id(&self, type_id: TypeId) -> Option<ComponentId> {
|
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`.
|
/// Returns the [`ComponentId`] of the given [`Component`] type `T`.
|
||||||
@ -581,9 +579,7 @@ impl Components {
|
|||||||
/// Type-erased equivalent of [`Components::resource_id()`].
|
/// Type-erased equivalent of [`Components::resource_id()`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_resource_id(&self, type_id: TypeId) -> Option<ComponentId> {
|
pub fn get_resource_id(&self, type_id: TypeId) -> Option<ComponentId> {
|
||||||
self.resource_indices
|
self.resource_indices.get(&type_id).copied()
|
||||||
.get(&type_id)
|
|
||||||
.map(|index| ComponentId(*index))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`ComponentId`] of the given [`Resource`] type `T`.
|
/// Returns the [`ComponentId`] of the given [`Resource`] type `T`.
|
||||||
@ -657,14 +653,12 @@ impl Components {
|
|||||||
func: impl FnOnce() -> ComponentDescriptor,
|
func: impl FnOnce() -> ComponentDescriptor,
|
||||||
) -> ComponentId {
|
) -> ComponentId {
|
||||||
let components = &mut self.components;
|
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 descriptor = func();
|
||||||
let index = components.len();
|
let component_id = ComponentId(components.len());
|
||||||
components.push(ComponentInfo::new(ComponentId(index), descriptor));
|
components.push(ComponentInfo::new(component_id, descriptor));
|
||||||
index
|
component_id
|
||||||
});
|
})
|
||||||
|
|
||||||
ComponentId(*index)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets an iterator over all components registered with this instance.
|
/// Gets an iterator over all components registered with this instance.
|
||||||
|
Loading…
Reference in New Issue
Block a user