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.
This commit is contained in:
parent
05bad0673f
commit
f2b37c733e
@ -1337,9 +1337,22 @@ impl Components {
|
|||||||
let required_by = unsafe { self.get_required_by_mut(required).debug_checked_unwrap() };
|
let required_by = unsafe { self.get_required_by_mut(required).debug_checked_unwrap() };
|
||||||
required_by.insert(requiree);
|
required_by.insert(requiree);
|
||||||
|
|
||||||
|
let mut required_components_tmp = RequiredComponents::default();
|
||||||
// SAFETY: The caller ensures that the `requiree` and `required` components are valid.
|
// SAFETY: The caller ensures that the `requiree` and `required` components are valid.
|
||||||
let inherited_requirements =
|
let inherited_requirements = unsafe {
|
||||||
unsafe { self.register_inherited_required_components(requiree, required) };
|
self.register_inherited_required_components(
|
||||||
|
requiree,
|
||||||
|
required,
|
||||||
|
&mut required_components_tmp,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
// SAFETY: The caller ensures that the `requiree` is valid.
|
||||||
|
let required_components = unsafe {
|
||||||
|
self.get_required_components_mut(requiree)
|
||||||
|
.debug_checked_unwrap()
|
||||||
|
};
|
||||||
|
required_components.0.extend(required_components_tmp.0);
|
||||||
|
|
||||||
// Propagate the new required components up the chain to all components that require the requiree.
|
// Propagate the new required components up the chain to all components that require the requiree.
|
||||||
if let Some(required_by) = self.get_required_by(requiree).cloned() {
|
if let Some(required_by) = self.get_required_by(requiree).cloned() {
|
||||||
@ -1391,6 +1404,7 @@ impl Components {
|
|||||||
&mut self,
|
&mut self,
|
||||||
requiree: ComponentId,
|
requiree: ComponentId,
|
||||||
required: ComponentId,
|
required: ComponentId,
|
||||||
|
required_components: &mut RequiredComponents,
|
||||||
) -> Vec<(ComponentId, RequiredComponent)> {
|
) -> Vec<(ComponentId, RequiredComponent)> {
|
||||||
// Get required components inherited from the `required` component.
|
// Get required components inherited from the `required` component.
|
||||||
// SAFETY: The caller ensures that the `required` component is valid.
|
// SAFETY: The caller ensures that the `required` component is valid.
|
||||||
@ -1414,12 +1428,6 @@ impl Components {
|
|||||||
|
|
||||||
// Register the new required components.
|
// Register the new required components.
|
||||||
for (component_id, component) in inherited_requirements.iter().cloned() {
|
for (component_id, component) in inherited_requirements.iter().cloned() {
|
||||||
// SAFETY: The caller ensures that the `requiree` is valid.
|
|
||||||
let required_components = unsafe {
|
|
||||||
self.get_required_components_mut(requiree)
|
|
||||||
.debug_checked_unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Register the required component for the requiree.
|
// Register the required component for the requiree.
|
||||||
// SAFETY: Component ID and constructor match the ones on the original requiree.
|
// SAFETY: Component ID and constructor match the ones on the original requiree.
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -1520,26 +1528,7 @@ impl Components {
|
|||||||
let required_by = unsafe { self.get_required_by_mut(required).debug_checked_unwrap() };
|
let required_by = unsafe { self.get_required_by_mut(required).debug_checked_unwrap() };
|
||||||
required_by.insert(requiree);
|
required_by.insert(requiree);
|
||||||
|
|
||||||
// Register the inherited required components for the requiree.
|
self.register_inherited_required_components(requiree, required, required_components);
|
||||||
let required: Vec<(ComponentId, RequiredComponent)> = self
|
|
||||||
.get_info(required)
|
|
||||||
.unwrap()
|
|
||||||
.required_components()
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.map(|(id, component)| (*id, component.clone()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
for (id, component) in required {
|
|
||||||
// Register the inherited required components for the requiree.
|
|
||||||
// The inheritance depth is increased by `1` since this is a component required by the original required component.
|
|
||||||
required_components.register_dynamic(
|
|
||||||
id,
|
|
||||||
component.constructor.clone(),
|
|
||||||
component.inheritance_depth + 1,
|
|
||||||
);
|
|
||||||
self.get_required_by_mut(id).unwrap().insert(requiree);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
Loading…
Reference in New Issue
Block a user