Skip to content

Commit

Permalink
bevy_reflect: Add as_reflect and as_reflect_mut (#4350)
Browse files Browse the repository at this point in the history
# Objective

Trait objects that have `Reflect` as a supertrait cannot be upcast to a `dyn Reflect`.

Attempting something like:

```rust
trait MyTrait: Reflect {
  // ...
}

fn foo(value: &dyn MyTrait) {
  let reflected = value as &dyn Reflect; // Error!
  // ...
}
```

Results in `error[E0658]: trait upcasting coercion is experimental`.

The reason this is important is that a lot of `bevy_reflect` methods require a `&dyn Reflect`. This is trivial with concrete types, but if we don't know the concrete type (we only have the trait object), we can't use these methods. For example, we couldn't create a `ReflectSerializer` for the type since it expects a `&dyn Reflect` value— even though we should be able to.

## Solution

Add `as_reflect` and `as_reflect_mut` to `Reflect` to allow upcasting to a `dyn Reflect`:

```rust
trait MyTrait: Reflect {
  // ...
}

fn foo(value: &dyn MyTrait) {
  let reflected = value.as_reflect();
  // ...
}
```

## Alternatives

We could defer this type of logic to the crate/user. They can add these methods to their trait in the same exact way we do here. The main benefit of doing it ourselves is it makes things convenient for them (especially when using the derive macro).

We could also create an `AsReflect` trait with a blanket impl over all reflected types, however, I could not get that to work for trait objects since they aren't sized.

---

## Changelog

- Added trait method `Reflect::as_reflect(&self)`
- Added trait method `Reflect::as_reflect_mut(&mut self)`

## Migration Guide

- Manual implementors of `Reflect` will need to add implementations for the methods above (this should be pretty easy as most cases just need to return `self`)
  • Loading branch information
MrGVSV committed Apr 25, 2022
1 parent 7a7f097 commit 5047e1f
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ fn impl_struct(
fn any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

#[inline]
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn #bevy_reflect_path::Reflect {
self
}

#[inline]
fn clone_value(&self) -> Box<dyn #bevy_reflect_path::Reflect> {
use #bevy_reflect_path::Struct;
Expand Down Expand Up @@ -396,6 +407,17 @@ fn impl_tuple_struct(
fn any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

#[inline]
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn #bevy_reflect_path::Reflect {
self
}

#[inline]
fn clone_value(&self) -> Box<dyn #bevy_reflect_path::Reflect> {
use #bevy_reflect_path::TupleStruct;
Expand Down Expand Up @@ -474,6 +496,16 @@ fn impl_value(
self
}

#[inline]
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn #bevy_reflect_path::Reflect {
self
}

#[inline]
fn clone_value(&self) -> Box<dyn #bevy_reflect_path::Reflect> {
Box::new(self.clone())
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ where
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}

fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
crate::list_apply(self, value);
}
Expand Down
24 changes: 24 additions & 0 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ unsafe impl<T: FromReflect> Reflect for Vec<T> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}

fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
crate::list_apply(self, value);
}
Expand Down Expand Up @@ -220,6 +228,14 @@ unsafe impl<K: Reflect + Eq + Hash, V: Reflect> Reflect for HashMap<K, V> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}

fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
if let ReflectRef::Map(map_value) = value.reflect_ref() {
for (key, value) in map_value.iter() {
Expand Down Expand Up @@ -304,6 +320,14 @@ unsafe impl Reflect for Cow<'static, str> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}

fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
let value = value.any();
if let Some(value) = value.downcast_ref::<Self>() {
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,19 @@ mod tests {
std::any::type_name::<TestTupleStruct>()
);
}

#[test]
fn as_reflect() {
trait TestTrait: Reflect {}

#[derive(Reflect)]
struct TestStruct;

impl TestTrait for TestStruct {}

let trait_object: Box<dyn TestTrait> = Box::new(TestStruct);

// Should compile:
let _ = trait_object.as_reflect();
}
}
10 changes: 10 additions & 0 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ unsafe impl Reflect for DynamicList {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
list_apply(self, value);
}
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ unsafe impl Reflect for DynamicMap {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
if let ReflectRef::Map(map_value) = value.reflect_ref() {
for (key, value) in map_value.iter() {
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_reflect/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ pub unsafe trait Reflect: Any + Send + Sync {
/// Returns the value as a [`&mut dyn Any`][std::any::Any].
fn any_mut(&mut self) -> &mut dyn Any;

/// Casts this type to a reflected value
fn as_reflect(&self) -> &dyn Reflect;

/// Casts this type to a mutable reflected value
fn as_reflect_mut(&mut self) -> &mut dyn Reflect;

/// Applies a reflected value to this value.
///
/// If a type implements a subtrait of `Reflect`, then the semantics of this
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_reflect/src/struct_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ unsafe impl Reflect for DynamicStruct {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
Expand Down
18 changes: 18 additions & 0 deletions crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ unsafe impl Reflect for DynamicTuple {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
Expand Down Expand Up @@ -366,6 +376,14 @@ macro_rules! impl_reflect_tuple {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}

fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
crate::tuple_apply(self, value);
}
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_reflect/src/tuple_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ unsafe impl Reflect for DynamicTupleStruct {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
}

#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
Expand Down

0 comments on commit 5047e1f

Please sign in to comment.