Skip to content

ChildBuilder methods should take IntoIterator, rather than a slice type #7229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
alice-i-cecile opened this issue Jan 16, 2023 · 7 comments
Labels
C-Usability A targeted quality-of-life change that makes Bevy easier to use

Comments

@alice-i-cecile
Copy link
Member

          It's generally better to use an iterator type instead of forcing someone to use a `slice`. Since it's not always guaranteed that the children entities will be in an array-like container.
    fn replace_children(&mut self, children: impl IntoIterator<Item = Entity>) -> &mut Self {

Originally posted by @NathanSWard in #6035 (comment)

@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Hierarchy labels Jan 16, 2023
@XaurDesu
Copy link

Given the fact that there's already a pull request concerning updating this, is there anything i could help with here? if not, that's fine, just stumbled upon this project and thought it looked interesting.

@adshih
Copy link
Contributor

adshih commented Feb 16, 2023

Is anyone currently working on this? Thinking about picking it up.

@alice-i-cecile
Copy link
Member Author

#7244 is in progress: at this point I would suggest either adopting the work there (rebase the commits and make your own PR sharing credit) or making PRs to the author's branch and pinging them on the thread :)

@emwalker
Copy link

I started from #7244 and created #7939. But now I'm starting to wonder whether using an iterator that is consumed improves upon the ergonomics of passing an &[Entity] slice around. Two passes on the PR were made, one with impl IntoIterator<Item = Entity> and another with impl IntoIterator<Item = impl Borrow<Entity>>, but neither approach feels quite right.

First, downstream code will want to use the children argument more than once, and since the iterator will be consumed, this leads to collecting it into a vec deeper in the call stack. Using impl IntoIterator<Item = Entity> further consumes the entries in the iterator, which might make sense in some cases, but perhaps not all. An alternative that has been suggested, impl IntoIterator<Item = impl Borrow<Entity>>, has other issues in terms of ergonomics, and still requires collecting an intermediate vec.

My knowledge of Rust is shaky enough that I don't have well formed opinions on these questions. But the current state of the PR does not seem quite right, either. So I'm happy to leave this story for anyone else who wants to take it.

@alice-i-cecile
Copy link
Member Author

That's a reasonable argument, but this is pretty hot code. I'm very reluctant to require additional allocations.

@emwalker
Copy link

That makes sense. If you can think of a way to avoid the intermediate vecs while still passing in an iterator to the trait methods, then perhaps the PR can be salvaged. Otherwise I'm inclined to close the PR.

@alice-i-cecile
Copy link
Member Author

Alright, let's close it out then :)

@alice-i-cecile alice-i-cecile removed the D-Trivial Nice and easy! A great choice to get started with Bevy label Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants