bevy/crates/bevy_ecs/src
Gino Valente da4e7769ad
bevy_ecs: Special-case Entity::PLACEHOLDER formatting (#15839)
# Objective

Oftentimes, users will store an entity on a component or resource. To
make this component/resource `Default`-able, they might initialize it
with `Entity::PLACEHOLDER`. This is sometimes done to avoid the need for
an `Option<Entity>`, especially if it complicates other logic.

For example, it's used in this `Selection` resource to denote "no
selection":

```rust
#[derive(Resource, Debug)]
struct Selection(Entity);

impl Default for Selection {
    fn default() -> Self {
        Self(Entity::PLACEHOLDER)
    }
}
```

The problem is that if we try to `Debug` the current `Selection`, we get
back: `4294967295v1#8589934591`. It's not immediately obvious whether or
not the entity is an actual entity or the placeholder.

Now while it doesn't take long to realize that this is in fact just the
value of `Entity::PLACEHOLDER`, it would be a lot clearer if this was
made explicit, especially for these particular use cases.

## Solution

This PR makes the `Debug` and `Display` impls for `Entity` return
`PLACEHOLDER` for the `Entity::PLACEHOLDER` constant.

~~Feel free to bikeshed the actual value returned here. I think
`PLACEHOLDER` on its own could work too.~~ Swapped to `PLACEHOLDER` from
`Entity::PLACEHOLDER`.

## Testing

You can test locally by running:

```
cargo test --package bevy_ecs
```

---

## Migration Guide

The `Debug` and `Display` impls for `Entity` now return `PLACEHOLDER`
for the `Entity::PLACEHOLDER` constant. If you had any code relying on
these values, you may need to account for this change.
2024-10-11 03:12:01 +00:00
..
entity bevy_ecs: Special-case Entity::PLACEHOLDER formatting (#15839) 2024-10-11 03:12:01 +00:00
event Add World::get_resource_or_init as an alternative to World::get_resource_or_insert_with (#15758) 2024-10-09 20:56:26 +00:00
identifier Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
observer Rename App/World::observe to add_observer, EntityWorldMut::observe_entity to observe. (#15754) 2024-10-09 15:39:29 +00:00
query Remove thiserror from bevy_ecs (#15774) 2024-10-09 14:20:58 +00:00
reflect Allow World::entity family of functions to take multiple entities and get multiple references back (#15614) 2024-10-07 15:21:40 +00:00
schedule Add World::get_resource_or_init as an alternative to World::get_resource_or_insert_with (#15758) 2024-10-09 20:56:26 +00:00
storage Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
system Text rework (#15591) 2024-10-09 18:35:36 +00:00
world Add World::get_resource_or_init as an alternative to World::get_resource_or_insert_with (#15758) 2024-10-09 20:56:26 +00:00
archetype.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
batching.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bundle.rs Migrate bevy_sprite to required components (#15489) 2024-10-09 16:17:26 +00:00
change_detection.rs QuerySingle family of system params (#15476) 2024-09-28 19:35:27 +00:00
component.rs Remove thiserror from bevy_ecs (#15774) 2024-10-09 14:20:58 +00:00
intern.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
label.rs Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
lib.rs Deprecate get_or_spawn (#15652) 2024-10-07 16:08:22 +00:00
removal_detection.rs Remove thiserror from bevy_ecs (#15774) 2024-10-09 14:20:58 +00:00
traversal.rs Bubbling observers traversal should use query data (#15385) 2024-09-23 18:08:36 +00:00