bevy/crates
Zachary Harrold 57a175f546
Ensure consistency between Un/Typed AssetId and Handle (#10628)
# Objective

- Fixes #10629
- `UntypedAssetId` and `AssetId` (along with `UntypedHandle` and
`Handle`) do not hash to the same values when pointing to the same
`Asset`. Additionally, comparison and conversion between these types
does not follow idiomatic Rust standards.

## Solution

- Added new unit tests to validate/document expected behaviour
- Added trait implementations to make working with Un/Typed values more
ergonomic
- Ensured hashing and comparison between Un/Typed values is consistent
- Removed `From` trait implementations that panic, and replaced them
with `TryFrom`

---

## Changelog

- Ensured `Handle::<A>::hash` and `UntypedHandle::hash` will produce the
same value for the same `Asset`
- Added non-panicing `Handle::<A>::try_typed`
- Added `PartialOrd` to `UntypedHandle` to match `Handle<A>` (this will
return `None` for `UntypedHandles` for differing `Asset` types)
- Added `TryFrom<UntypedHandle>` for `Handle<A>`
- Added `From<Handle<A>>` for `UntypedHandle`
- Removed panicing `From<Untyped...>` implementations. These are
currently unused within the Bevy codebase, and shouldn't be used
externally, hence removal.
- Added cross-implementations of `PartialEq` and `PartialOrd` for
`UntypedHandle` and `Handle<A>` allowing direct comparison when
`TypeId`'s match.
- Near-identical changes to `AssetId` and `UntypedAssetId`

## Migration Guide

If you relied on any of the panicing `From<Untyped...>` implementations,
simply call the existing `typed` methods instead. Alternatively, use the
new `TryFrom` implementation instead to directly expose possible
mistakes.

## Notes

I've made these changes since `Handle` is such a fundamental type to the
entire `Asset` ecosystem within Bevy, and yet it had pretty unclear
behaviour with no direct testing. The fact that hashing untyped vs typed
versions of the same handle would produce different values is something
I expect to cause a very subtle bug for someone else one day.

I haven't included it in this PR to avoid any controversy, but I also
believe the `typed_unchecked` methods should be removed from these
types, or marked as `unsafe`. The `texture_atlas` example currently uses
it, and I believe it is a bad choice. The performance gained by not
type-checking before conversion would be entirely dwarfed by the act of
actually loading an asset and creating its handle anyway. If an end user
is in a tight loop repeatedly calling `typed_unchecked` on an
`UntypedHandle` for the entire runtime of their application, I think the
small performance drop caused by that safety check is ~~a form of cosmic
justice~~ reasonable.
2023-11-21 01:13:36 +00:00
..
bevy_a11y Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_animation Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_app Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_asset Ensure consistency between Un/Typed AssetId and Handle (#10628) 2023-11-21 01:13:36 +00:00
bevy_audio Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_core Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_core_pipeline Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_derive Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_diagnostic Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_dylib Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_dynamic_plugin Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_ecs Allow #[derive(Bundle)] on tuple structs (take 3) (#10561) 2023-11-21 01:09:16 +00:00
bevy_ecs_compile_fail_tests Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_encase_derive Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_gilrs Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_gizmos Move Circle Gizmos to Their Own File (#10631) 2023-11-20 09:47:50 +00:00
bevy_gltf Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_hierarchy bevy_hierarchy: add some docs (#10598) 2023-11-21 01:11:20 +00:00
bevy_input Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_internal Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_log Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_macro_utils Allow #[derive(Bundle)] on tuple structs (take 3) (#10561) 2023-11-21 01:09:16 +00:00
bevy_macros_compile_fail_tests Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_math Add winding order for Triangle2d (#10620) 2023-11-20 09:47:05 +00:00
bevy_mikktspace Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_pbr Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_ptr Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_reflect Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_reflect_compile_fail_tests Improve TypeUuid's derive macro error messages (#9315) 2023-10-02 12:42:01 +00:00
bevy_render Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_scene Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_sprite Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_tasks Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_text Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_time Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_transform Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_ui Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_utils Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_window Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_winit Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00