bevy/crates/bevy_reflect/bevy_reflect_derive/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
..
container_attributes.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
derive_data.rs bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00
field_attributes.rs bevy_reflect: Add #[reflect(default)] attribute for FromReflect (#4140) 2022-05-30 19:06:25 +00:00
from_reflect.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
impls.rs Make Reflect safe to implement (#5010) 2022-06-27 16:52:25 +00:00
lib.rs bevy_reflect: Add #[reflect(default)] attribute for FromReflect (#4140) 2022-05-30 19:06:25 +00:00
reflect_value.rs bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00
registration.rs bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00
trait_reflection.rs bevy_reflect: Added get_boxed method to reflect_trait (#4120) 2022-05-20 13:31:49 +00:00
type_uuid.rs Clippy improvements (#4665) 2022-05-31 01:38:07 +00:00
utility.rs bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00