diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index ae1582b547..3e0d28df7d 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -439,8 +439,8 @@ impl ComponentDescriptor { #[derive(Debug, Default)] pub struct Components { components: Vec, - indices: TypeIdMap, - resource_indices: TypeIdMap, + indices: TypeIdMap, + resource_indices: TypeIdMap, } 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::()) - }); - 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, 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 { - 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 { - 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.