Skip to content

implement UniqueEntityArray #17954

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

Merged
merged 2 commits into from
Feb 24, 2025

Conversation

Victoronz
Copy link
Contributor

@Victoronz Victoronz commented Feb 20, 2025

Objective

Continuation of #17589 and #16547.

get_many is last of the many methods with a missing unique counterpart.
It both takes and returns arrays, thus necessitates a matching UniqueEntityArray type!
Plus, some slice methods involve returning arrays, which are currently missing from UniqueEntitySlice.

Solution

Add the type, the related methods and trait impls.

Note that for this PR, we abstain from some methods/trait impls that create &mut UniqueEntityArray, because it can be successfully mem-swapped. This can potentially invalidate a larger slice, which is the same reason we punted on some mutable slice methods in #17589. We can follow-up on all of these together in a following PR.

The new unique_array module is not glob-exported, because the trait alias unique_array::IntoIter would conflict with unique_vec::IntoIter.
The solution for this is to make the various unique_* modules public, which I intend to do in yet another PR.

@Victoronz Victoronz added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 20, 2025
@Victoronz
Copy link
Contributor Author

Victoronz commented Feb 21, 2025

There are also supposed to be some From<UniqueEntityArray<T, N>> implementations for some tuples, but I am currently trying to figure out whether it is possible to use the all_tuples_with_size! macro with just one T, and to then mention that T within UniqueEntityArray<T, N>.

@Victoronz
Copy link
Contributor Author

Gave up on that, just wrote them out manually.

Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_many is last of the many methods with a missing unique counterpart.
It both takes and returns arrays, thus necessitates a matching UniqueEntityArray type!

My impression is that get_many is mainly intended to be used with two- or three-element array literals, where the syntactic overhead of converting to this type won't be worth it to save one to three integer comparisons.

It still seems fine to build this! I just don't expect a get_many_unique to actually get much use.

@Victoronz
Copy link
Contributor Author

get_many is last of the many methods with a missing unique counterpart.
It both takes and returns arrays, thus necessitates a matching UniqueEntityArray type!

My impression is that get_many is mainly intended to be used with two- or three-element array literals, where the syntactic overhead of converting to this type won't be worth it to save one to three integer comparisons.

It is a bit of a chicken and egg type situation, it is intended to be used for low N because higher N would be quite expensive to validate. That doesn't mean that people won't want to do it, or that even using this for low N isn't worth it.
Arrays are also not that common in "splitting" logic just yet, but there are several unstable methods that divide slices into arrays instead of further slices, which will in turn make this counterpart more reachable.

It still seems fine to build this! I just don't expect a get_many_unique to actually get much use.

I'm not so sure about that, I've seen people ask for something like this before. The creator of avian wanted some form of this, f.e.!

@chescock
Copy link
Contributor

Oh, interesting! I had thought the only advantage of the array version over iter_many was that you could destructure the result into local variables, which you wouldn't want to do for large N because you wouldn't want that many variables. It'll be fun to see what folks do with these features!

@Victoronz Victoronz added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 24, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 24, 2025
Merged via the queue into bevyengine:main with commit a171733 Feb 24, 2025
30 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Mar 16, 2025
# Objective

Continuation to #16547 and #17954.

The `get_many` family are the last methods on `Query`/`QueryState` for
which we're still missing a `unique` version.

## Solution

Offer `get_many_unique`/`get_many_unique_mut` and
`get_many_unique_inner`!

Their implementation is the same as `get_many`, the difference lies in
their guaranteed-to-be unique inputs, meaning we never do any aliasing
checks.

To reduce confusion, we also rename `get_many_readonly` into
`get_many_inner` and the current `get_many_inner` into
`get_many_mut_inner` to clarify their purposes.

## Testing

Doc examples.

## Migration Guide

`get_many_inner` is now called `get_many_mut_inner`.
`get_many_readonly` is now called `get_many_inner`.
github-merge-queue bot pushed a commit that referenced this pull request Mar 17, 2025
# Objective

Continuation of #17449.

#17449 implemented the wrapper types around `IndexMap`/`Set` and co.,
however punted on the slice types.
They are needed to support creating `EntitySetIterator`s from their
slices, not just the base maps and sets.

## Solution

Add the wrappers, in the same vein as #17449 and #17589 before.

The `Index`/`IndexMut` implementations take up a lot of space, however
they cannot be merged because we'd then get overlaps.

They are simply named `Slice` to match the `indexmap` naming scheme, but
this means they cannot be differentiated properly until their modules
are made public, which is already a follow-up mentioned in #17954.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes D-Unsafe Touches with unsafe code in some way S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants