Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - bevy_reflect: Add as_reflect and as_reflect_mut #4350

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add cast to Reflect
This is most useful for trait objects with Reflect as a supertrait,
allowing it to be upcast to a dyn Reflect
  • Loading branch information
MrGVSV committed Mar 28, 2022
commit 24132502531572e92c6a94e2175cd267e8eadf88
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