bevy/crates
Zachary Harrold 72adf2ae2a
Reduced TableRow as Casting (#10811)
# Objective

- Fixes #10806

## Solution

Replaced `new` and `index` methods for both `TableRow` and `TableId`
with `from_*` and `as_*` methods. These remove the need to perform
casting at call sites, reducing the total number of casts in the Bevy
codebase. Within these methods, an appropriate `debug_assertion` ensures
the cast will behave in an expected manner (no wrapping, etc.). I am
using a `debug_assertion` instead of an `assert` to reduce any possible
runtime overhead, however minimal. This choice is something I am open to
changing (or leaving up to another PR) if anyone has any strong
arguments for it.

---

## Changelog

- `ComponentSparseSet::sparse` stores a `TableRow` instead of a `u32`
(private change)
- Replaced `TableRow::new` and `TableRow::index` methods with
`TableRow::from_*` and `TableRow::as_*`, with `debug_assertions`
protecting any internal casting.
- Replaced `TableId::new` and `TableId::index` methods with
`TableId::from_*` and `TableId::as_*`, with `debug_assertions`
protecting any internal casting.
- All `TableId` methods are now `const`

## Migration Guide

- `TableRow::new` -> `TableRow::from_usize`
- `TableRow::index` -> `TableRow::as_usize`
- `TableId::new` -> `TableId::from_usize`
- `TableId::index` -> `TableId::as_usize`

---

## Notes

I have chosen to remove the `index` and `new` methods for the following
chain of reasoning:

- Across the codebase, `new` was called with a mixture of `u32` and
`usize` values. Likewise for `index`.
- Choosing `new` to either be `usize` or `u32` would break half of these
call-sites, requiring `as` casting at the site.
- Adding a second method `new_u32` or `new_usize` avoids the above, bu
looks visually inconsistent.
- Therefore, they should be replaced with `from_*` and `as_*` methods
instead.

Worth noting is that by updating `ComponentSparseSet`, there are now
zero instances of interacting with the inner value of `TableRow` as a
`u32`, it is exclusively used as a `usize` value (due to interactions
with methods like `len` and slice indexing). I have left the `as_u32`
and `from_u32` methods as the "proper" constructors/getters.
2023-12-05 02:44:33 +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 Enable clippy::undocumented_unsafe_blocks warning across the workspace (#10646) 2023-11-21 02:06:24 +00:00
bevy_app Remove identity map calls (#10848) 2023-12-04 20:44:53 +00:00
bevy_asset Allow removing and reloading assets with live handles (#10785) 2023-11-29 23:32:13 +00:00
bevy_audio Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_core Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_core_pipeline Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +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 Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +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 Reduced TableRow as Casting (#10811) 2023-12-05 02:44:33 +00:00
bevy_ecs_compile_fail_tests Override QueryIter::fold to port Query::for_each perf gains to select Iterator combinators (#6773) 2023-12-01 09:09:55 +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 Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_gizmos Bind group layout entries (#10224) 2023-11-28 04:00:49 +00:00
bevy_gltf Add GltfLoaderSettings (#10804) 2023-11-30 00:34:45 +00:00
bevy_hierarchy Split WorldQuery into WorldQueryData and WorldQueryFilter (#9918) 2023-11-28 03:56:07 +00:00
bevy_input Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_internal Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_log Add helper macro's for logging only once (#10808) 2023-12-05 01:56:40 +00:00
bevy_macro_utils Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_macros_compile_fail_tests Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_math Remove From implementations from the direction types (#10857) 2023-12-05 02:44:27 +00:00
bevy_mikktspace Enable clippy::undocumented_unsafe_blocks warning across the workspace (#10646) 2023-11-21 02:06:24 +00:00
bevy_pbr Fix prepass binding issues causing crashes when not all prepass bindings are used (#10788) 2023-11-29 23:11:12 +00:00
bevy_ptr Enable clippy::undocumented_unsafe_blocks warning across the workspace (#10646) 2023-11-21 02:06:24 +00:00
bevy_reflect Fix nested generics in Reflect derive (#10791) 2023-11-29 01:46:09 +00:00
bevy_reflect_compile_fail_tests Fix nested generics in Reflect derive (#10791) 2023-11-29 01:46:09 +00:00
bevy_render Substitute get(0) with first() (#10847) 2023-12-02 22:13:42 +00:00
bevy_scene Mention DynamicSceneBuilder in doc comment (#10780) 2023-11-28 23:45:00 +00:00
bevy_sprite try_insert Aabbs (#10801) 2023-11-29 15:00:50 +00:00
bevy_tasks Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_text Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_time Wait until FixedUpdate can see events before dropping them (#10077) 2023-11-26 23:04:41 +00:00
bevy_transform Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_ui Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_utils Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_window Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_winit Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00