Repository: bevyengine/bevy
File: crates/bevy_ecs/src/relationship/related_methods.rs (Rust)

Code before:
                world
                    .entity_mut(*new_relation)
                    .modify_or_insert_relation_with_relationship_hook_mode::<R>(
                        this,
                        RelationshipHookMode::Skip,
                    );
            }
        });

        self
    }

    /// Relates the given entity to this with the relation `R`.
    ///
    /// See [`add_related`](Self::add_related) if you want to relate more than one entity.
    pub fn add_one_related<R: Relationship>(&mut self, entity: Entity) -> &mut Self {
        self.add_related::<R>(&[entity])
    }

    /// Despawns entities that relate to this one via the given [`RelationshipTarget`].
    /// This entity will not be despawned.
    pub fn despawn_related<S: RelationshipTarget>(&mut self) -> &mut Self {
        if let Some(sources) = self.get::<S>() {
            let sources = sources.iter().collect::<Vec<_>>();
            self.world_scope(|world| {
                for entity in sources {
                    if let Ok(entity_mut) = world.get_entity_mut(entity) {
                        entity_mut.despawn();
                    };
                }
            });
        }
        self
    }

    /// Despawns the children of this entity.
    /// This entity will not be despawned.
    ///
    /// This is a specialization of [`despawn_related`](EntityWorldMut::despawn_related), a more general method for despawning via relationships.
    pub fn despawn_children(&mut self) -> &mut Self {
        self.despawn_related::<Children>();
        self
    }

    /// Inserts a component or bundle of components into the entity and all related entities,
    /// traversing the relationship tracked in `S` in a breadth-first manner.
    ///
    /// # Warning
    ///
    /// This method should only be called on relationships that form a tree-like structure.
    /// Any cycles will cause this method to loop infinitely.

Review comment (suggestion):
```suggestion
            // We have to collect here to defer removal, allowing observers and hooks to see this data
            // before it is finally removed.
            let sources = sources.iter().collect::<Vec<_>>();
```

Code after:
                world
                    .entity_mut(*new_relation)
                    .modify_or_insert_relation_with_relationship_hook_mode::<R>(
                        this,
                        RelationshipHookMode::Skip,
                    );
            }
        });

        self
    }

    /// Relates the given entity to this with the relation `R`.
    ///
    /// See [`add_related`](Self::add_related) if you want to relate more than one entity.
    pub fn add_one_related<R: Relationship>(&mut self, entity: Entity) -> &mut Self {
        self.add_related::<R>(&[entity])
    }

    /// Despawns entities that relate to this one via the given [`RelationshipTarget`].
    /// This entity will not be despawned.
    pub fn despawn_related<S: RelationshipTarget>(&mut self) -> &mut Self {
        if let Some(sources) = self.get::<S>() {
            // We have to collect here to defer removal, allowing observers and hooks to see this data
            // before it is finally removed.
            let sources = sources.iter().collect::<Vec<_>>();
            self.world_scope(|world| {
                for entity in sources {
                    if let Ok(entity_mut) = world.get_entity_mut(entity) {
                        entity_mut.despawn();
                    };
                }
            });
        }
        self
    }

    /// Despawns the children of this entity.
    /// This entity will not be despawned.
    ///
    /// This is a specialization of [`despawn_related`](EntityWorldMut::despawn_related), a more general method for despawning via relationships.
    pub fn despawn_children(&mut self) -> &mut Self {
        self.despawn_related::<Children>();
        self
    }

    /// Inserts a component or bundle of components into the entity and all related entities,
    /// traversing the relationship tracked in `S` in a breadth-first manner.
    ///
    /// # Warning
    ///