Skip to content

How to sort arbitrary queries by component hash? Getting confused by complex types and lifetime around WorldQuery, Item, etc #6884

Answered by SkiFire13
bcolloran asked this question in Q&A
Discussion options

You must be logged in to vote

Looking at the definition of Query::iter_mut in the docs you can see that it returns a QueryIter<'_, 's, Q, F>. '_ means an "inferred" lifetime, and since the only generic lifetime in the input is the one of the &mut self, it means that they're the same. Then, looking at the docs for QueryIter<'w, 's, Q, F> you can see that its Iterator implementation yields <Q as WorldQuery>::Item<'w>, thus the lifetime of the elements is the first one in the QueryIter type, and tracing back to the .iter_mut call the first lifetime was the lifetime of &mut self. Thus that's the only lifetime you need to care about and use. Here's the result (with the bonus of using sort_by_key instead of sort_by):

fn sor…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bcolloran
Comment options

Answer selected by bcolloran
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants