bevy icon indicating copy to clipboard operation
bevy copied to clipboard

ChildBuilder methods should take IntoIterator, rather than a slice type

Open alice-i-cecile opened this issue 2 years ago • 7 comments

          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 https://github.com/bevyengine/bevy/pull/6035#discussion_r981546329

alice-i-cecile avatar Jan 16 '23 15:01 alice-i-cecile

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.

XaurDesu avatar Jan 18 '23 23:01 XaurDesu

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

adshih avatar Feb 16 '23 04:02 adshih

#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 :)

alice-i-cecile avatar Feb 16 '23 04:02 alice-i-cecile

I started from https://github.com/bevyengine/bevy/pull/7244 and created https://github.com/bevyengine/bevy/pull/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.

emwalker avatar Mar 13 '23 02:03 emwalker

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

alice-i-cecile avatar Mar 13 '23 03:03 alice-i-cecile

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.

emwalker avatar Mar 13 '23 04:03 emwalker

Alright, let's close it out then :)

alice-i-cecile avatar Mar 13 '23 05:03 alice-i-cecile