You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Objective
Recently the `u32` `Entity::generation` was replaced with the new
`EntityGeneration` in #19121.
This made meanings a lot more clear, and prevented accidental misuse.
One common misuse was assuming that `u32`s that were greater than others
came after those others.
Wrapping makes this assumption false.
When `EntityGeneration` was created, it retained the `u32` ordering,
which was useless at best and wrong at worst.
This pr fixes the ordering implementation, so new generations are
greater than older generations.
Some users were already accounting for this ordering issue (which was
still present in 0.16 and before) by manually accessing the `u32`
representation. This made migrating difficult for avian physics; see
[here](https://discord.com/channels/691052431525675048/749335865876021248/1377431569228103780).
I am generally of the opinion that this type should be kept opaque to
prevent accidental misuse.
As we find issues like this, the functionality should be added to
`EntityGeneration` directly.
## Solution
Fix the ordering implementation through `Ord`.
Alternatively, we could keep `Ord` the same and make a `cmp_age` method,
but I think this is better, even though sorting entity ids may be
*marginally* slower now (but more correct). This is a tradeoff.
## Testing
I improved documentation for aliasing and ordering, adding some doc
tests.
/// Once an entity is despawned, it ceases to exist.
297
+
/// However, its [`Entity`] id is still present, and may still be contained in some data.
298
+
/// This becomes problematic because it is possible for a later entity to be spawned at the exact same id!
299
+
/// If this happens, which is rare but very possible, it will be logged.
300
+
///
301
+
/// Aliasing can happen without warning.
302
+
/// Holding onto a [`Entity`] id corresponding to an entity well after that entity was despawned can cause un-intuitive behavior for both ordering, and comparing in general.
303
+
/// To prevent these bugs, it is generally best practice to stop holding an [`Entity`] or [`EntityGeneration`] value as soon as you know it has been despawned.
304
+
/// If you must do otherwise, do not assume the [`Entity`] corresponds to the same conceptual entity it originally did.
305
+
/// See [`EntityGeneration`]'s docs for more information about aliasing and why it occurs.
306
+
///
241
307
/// # Stability warning
242
308
/// For all intents and purposes, `Entity` should be treated as an opaque identifier. The internal bit
243
309
/// representation is liable to change from release to release as are the behaviors or performance
0 commit comments