Skip to content

Commit e259145

Browse files
authored
Improve WithRelated ergonomics (#20423)
# Objective Recommending users to use `WithRelated([child2, child3].into_iter())` feels a bit weird. ## Solution Add `WithRelated::new`, which uses `IntoIterator` to do the conversion more cleanly and succinctly. The old way is still left as an option for those wanting it. ## Testing Updated the test to use the `new` function.
1 parent 7f82aa1 commit e259145

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

crates/bevy_ecs/src/spawn.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,19 @@ impl<R: Relationship, F: FnOnce(&mut RelatedSpawner<R>) + Send + Sync + 'static>
153153
/// Children::spawn((
154154
/// Spawn(Name::new("Child1")),
155155
/// // This adds the already existing entities as children of Root.
156-
/// WithRelated([child2, child3].into_iter()),
156+
/// WithRelated::new([child2, child3]),
157157
/// )),
158158
/// ));
159159
/// ```
160160
pub struct WithRelated<I>(pub I);
161161

162+
impl<I> WithRelated<I> {
163+
/// Creates a new [`WithRelated`] from a collection of entities.
164+
pub fn new(iter: impl IntoIterator<IntoIter = I>) -> Self {
165+
Self(iter.into_iter())
166+
}
167+
}
168+
162169
impl<R: Relationship, I: Iterator<Item = Entity>> SpawnableList<R> for WithRelated<I> {
163170
fn spawn(self, world: &mut World, entity: Entity) {
164171
world
@@ -653,7 +660,7 @@ mod tests {
653660
let parent = world
654661
.spawn((
655662
Name::new("Parent"),
656-
Children::spawn(WithRelated([child1, child2].into_iter())),
663+
Children::spawn(WithRelated::new([child1, child2])),
657664
))
658665
.id();
659666

0 commit comments

Comments
 (0)