Skip to content

Commit f2b37c7

Browse files
authored
Deduplicate register_inherited_required_components (#16519)
# Objective - Fixes #16416 ## Solution - Add a intermediate temporary mutable `RequiredComponents` to get avoid of the borrowing issues. ## Testing - I have run `cargo test --package bevy_ecs -- --exact --show-output` and past all the tests.
1 parent 05bad06 commit f2b37c7

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

crates/bevy_ecs/src/component.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,9 +1337,22 @@ impl Components {
13371337
let required_by = unsafe { self.get_required_by_mut(required).debug_checked_unwrap() };
13381338
required_by.insert(requiree);
13391339

1340+
let mut required_components_tmp = RequiredComponents::default();
13401341
// SAFETY: The caller ensures that the `requiree` and `required` components are valid.
1341-
let inherited_requirements =
1342-
unsafe { self.register_inherited_required_components(requiree, required) };
1342+
let inherited_requirements = unsafe {
1343+
self.register_inherited_required_components(
1344+
requiree,
1345+
required,
1346+
&mut required_components_tmp,
1347+
)
1348+
};
1349+
1350+
// SAFETY: The caller ensures that the `requiree` is valid.
1351+
let required_components = unsafe {
1352+
self.get_required_components_mut(requiree)
1353+
.debug_checked_unwrap()
1354+
};
1355+
required_components.0.extend(required_components_tmp.0);
13431356

13441357
// Propagate the new required components up the chain to all components that require the requiree.
13451358
if let Some(required_by) = self.get_required_by(requiree).cloned() {
@@ -1391,6 +1404,7 @@ impl Components {
13911404
&mut self,
13921405
requiree: ComponentId,
13931406
required: ComponentId,
1407+
required_components: &mut RequiredComponents,
13941408
) -> Vec<(ComponentId, RequiredComponent)> {
13951409
// Get required components inherited from the `required` component.
13961410
// SAFETY: The caller ensures that the `required` component is valid.
@@ -1414,12 +1428,6 @@ impl Components {
14141428

14151429
// Register the new required components.
14161430
for (component_id, component) in inherited_requirements.iter().cloned() {
1417-
// SAFETY: The caller ensures that the `requiree` is valid.
1418-
let required_components = unsafe {
1419-
self.get_required_components_mut(requiree)
1420-
.debug_checked_unwrap()
1421-
};
1422-
14231431
// Register the required component for the requiree.
14241432
// SAFETY: Component ID and constructor match the ones on the original requiree.
14251433
unsafe {
@@ -1520,26 +1528,7 @@ impl Components {
15201528
let required_by = unsafe { self.get_required_by_mut(required).debug_checked_unwrap() };
15211529
required_by.insert(requiree);
15221530

1523-
// Register the inherited required components for the requiree.
1524-
let required: Vec<(ComponentId, RequiredComponent)> = self
1525-
.get_info(required)
1526-
.unwrap()
1527-
.required_components()
1528-
.0
1529-
.iter()
1530-
.map(|(id, component)| (*id, component.clone()))
1531-
.collect();
1532-
1533-
for (id, component) in required {
1534-
// Register the inherited required components for the requiree.
1535-
// The inheritance depth is increased by `1` since this is a component required by the original required component.
1536-
required_components.register_dynamic(
1537-
id,
1538-
component.constructor.clone(),
1539-
component.inheritance_depth + 1,
1540-
);
1541-
self.get_required_by_mut(id).unwrap().insert(requiree);
1542-
}
1531+
self.register_inherited_required_components(requiree, required, required_components);
15431532
}
15441533

15451534
#[inline]

0 commit comments

Comments
 (0)