Skip to content

Commit

Permalink
Add Reflect::into_reflect
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Nov 7, 2022
1 parent efc111c commit 2091d79
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 2 deletions.
5 changes: 5 additions & 0 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
self
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
self
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
self
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ impl Reflect for DynamicArray {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/src/enums/dynamic_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ impl Reflect for DynamicEnum {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ where
self
}

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}
Expand Down
23 changes: 23 additions & 0 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ impl<T: FromReflect> Reflect for Vec<T> {
self
}

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}
Expand Down Expand Up @@ -413,6 +417,11 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Reflect for HashMap<K, V> {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}
Expand Down Expand Up @@ -543,6 +552,11 @@ impl<T: Reflect, const N: usize> Reflect for [T; N] {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down Expand Up @@ -661,6 +675,10 @@ impl Reflect for Cow<'static, str> {
self
}

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}
Expand Down Expand Up @@ -819,6 +837,11 @@ impl<T: FromReflect> Reflect for Option<T> {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
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 @@ -989,6 +989,21 @@ mod tests {
}
}

#[test]
fn into_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.into_reflect();
}

#[test]
fn as_reflect() {
trait TestTrait: Reflect {}
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ impl Reflect for DynamicList {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ impl Reflect for DynamicMap {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_reflect/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ pub trait Reflect: Any + Send + Sync {
/// Returns the value as a [`&mut dyn Any`][std::any::Any].
fn as_any_mut(&mut self) -> &mut dyn Any;

/// Casts this type to a reflected value
/// Casts this type to a boxed reflected value.
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>;

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

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

/// Applies a reflected value to this value.
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/src/struct_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ impl Reflect for DynamicStruct {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ impl Reflect for DynamicTuple {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down Expand Up @@ -520,6 +525,10 @@ macro_rules! impl_reflect_tuple {
self
}

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_reflect/src/tuple_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ impl Reflect for DynamicTupleStruct {
self
}

#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_reflect/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use std::any::{Any, TypeId};
/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
/// # fn as_any(&self) -> &dyn Any { todo!() }
/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
/// # fn apply(&mut self, value: &dyn Reflect) { todo!() }
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_reflect/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use std::any::{Any, TypeId};
/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
/// # fn as_any(&self) -> &dyn Any { todo!() }
/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
/// # fn apply(&mut self, value: &dyn Reflect) { todo!() }
Expand Down Expand Up @@ -102,6 +103,7 @@ impl NonGenericTypeInfoCell {
/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
/// # fn as_any(&self) -> &dyn Any { todo!() }
/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
/// # fn apply(&mut self, value: &dyn Reflect) { todo!() }
Expand Down

0 comments on commit 2091d79

Please sign in to comment.