Repository navigation
Clone and IntoIter support for queries #1631
Description
Activity
- addedA-ECSEntities, components, systems, and eventsEntities, components, systems, and eventsC-UsabilityA targeted quality-of-life change that makes Bevy easier to useA targeted quality-of-life change that makes Bevy easier to use
on Mar 12, 2021 Two more interesting use cases:
- Reduce boilerplate by enabling the
for e in query:sugar - Free ordered iteration ala Ordered iteration over Queries #1470.
Reacted by Jean Mertz and guim- Reduce boilerplate by enabling the
Followup conversation with @cart and @TheRawMeatball on this topic. Conclusions:
- IntoIter isn't the only blocker on using iter_tools: we would also need Clone, which isn't feasible for mutable queries
- iter_tools like functionality is very much desired because it's currently very frustrating to work with more than one entity at once in queries, like for collisions
- IntoIter support is desirable anyways
- IntoIter should resolve to iter_mut, because it's supported for both mutable and immutable queries
I'm a relative rust noob, but will this require Generic Associated Types?
AFAICT you want a signature like this:
impl<'w, 's, Q: WorldQuery, F: WorldQuery> IntoIterator for QueryState<Q, F> { type Item = <Q::Fetch as Fetch<'w>>::Item; type IntoIter = QueryIter<'w, 's, Q, F>; fn into_iter(self) -> Self::IntoIter {That leaves w and s unconstrained.
Reacted by Alice Cecile- addedD-ComplexQuite challenging from either a design or technical perspective. Ask for help!Quite challenging from either a design or technical perspective. Ask for help!S-Needs-DesignThis issue requires design work to think about how it would best be accomplishedThis issue requires design work to think about how it would best be accomplished
on Dec 12, 2021 Related to #2042, which would provide building blocks for iter_tools-like functionality.
Closing in favor of directly implementing these methods as use cases come up: the
Clonebound is unworkable.Is the
Clonebound unworkable even onQueryIter?Is the Clone bound unworkable even on QueryIter?
No, this should be quite simple to add and probably reasonably useful. If you'd like it, feel free to make a simple PR for it :)
It'd only be valid if the query in question is read only.
No, this should be quite simple to add and probably reasonably useful. If you'd like it, feel free to make a simple PR for it :)
Gotcha! I’ll definitely PR it if I find a usecase where I need Cartesian product where one of the two sides is immutable, since Itertools Cartesian product requires only one of the two iterators to be
CloneIt'd only be valid if the query in question is read only.
This is in regards to acquiring a
QueryIterfrom a mutable query via.iter()? “Read-only” here being immutable query? Or does that term mean something else?yes, read only as in an immutable query.
Reacted by Mathspy
What problem does this solve or what need does it fill?
Itertools has a great number of fantastic, ergonomic ways to work with iterators, like cartesian product, combinations etc that would be regularly used in games. I would love to be able to use this with my queries, but I can't due to lacking trait impls.
What solution would you like?
impl Clone, IntoIter and any other necessary traits on both Query and QueryIter.
What alternative(s) have you considered?
Write replacements for these methods by hand, either in the engine itself or in each game's code.
Additional context
One of the features of itertools (
exactly_one) was reinvented in #1263 already.Here's a nice code snippet that I would like to be able to compile showing the building block methods: