bevy/crates/bevy_reflect/src
PROMETHIA-27 c27a3cff6d Make Reflect safe to implement (#5010)
# Objective

Currently, `Reflect` is unsafe to implement because of a contract in which `any` and `any_mut` must return `self`, or `downcast` will cause UB. This PR makes `Reflect` safe, makes `downcast` not use unsafe, and eliminates this contract. 

## Solution

This PR adds a method to `Reflect`, `any`. It also renames the old `any` to `as_any`.
`any` now takes a `Box<Self>` and returns a `Box<dyn Any>`. 

---

## Changelog

### Added:
- `any()` method
- `represents()` method

### Changed:
- `Reflect` is now a safe trait
- `downcast()` is now safe
- The old `any` is now called `as_any`, and `any_mut` is now `as_mut_any`

## Migration Guide

- Reflect derives should not have to change anything
- Manual reflect impls will need to remove the `unsafe` keyword, add `any()` implementations, and rename the old `any` and `any_mut` to `as_any` and `as_mut_any`.
- Calls to `any`/`any_mut` must be changed to `as_any`/`as_mut_any`

## Points of discussion:

- Should renaming `any` be avoided and instead name the new method `any_box`?
- ~~Could there be a performance regression from avoiding the unsafe? I doubt it, but this change does seem to introduce redundant checks.~~
- ~~Could/should `is` and `type_id()` be implemented differently? For example, moving `is` onto `Reflect` as an `fn(&self, TypeId) -> bool`~~


Co-authored-by: PROMETHIA-27 <42193387+PROMETHIA-27@users.noreply.github.com>
2022-06-27 16:52:25 +00:00
..
impls Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
serde bevy_reflect: put serialize into external ReflectSerialize type (#4782) 2022-06-20 17:18:58 +00:00
array.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
fields.rs bevy_reflect: Add statically available type info for reflected types (#4042) 2022-06-09 21:18:15 +00:00
lib.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
list.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
map.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
path.rs document more of bevy_reflect (#3655) 2022-01-14 19:09:44 +00:00
reflect.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
std_traits.rs add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
struct_trait.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
tuple_struct.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
tuple.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
type_info.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
type_registry.rs bevy_reflect: put serialize into external ReflectSerialize type (#4782) 2022-06-20 17:18:58 +00:00
type_uuid.rs re-enable #[derive(TypeUuid)] for generics (#4118) 2022-04-26 19:41:25 +00:00
utility.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00