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] - Add reflect_owned #6494

Closed
wants to merge 2 commits 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
4 changes: 4 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 @@ -264,6 +264,10 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
#bevy_reflect_path::ReflectMut::Enum(self)
}

fn reflect_owned(self: Box<Self>) -> #bevy_reflect_path::ReflectOwned {
#bevy_reflect_path::ReflectOwned::Enum(self)
}

#hash_fn

#partial_eq_fn
Expand Down
4 changes: 4 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 @@ -223,6 +223,10 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
#bevy_reflect_path::ReflectMut::Struct(self)
}

fn reflect_owned(self: Box<Self>) -> #bevy_reflect_path::ReflectOwned {
#bevy_reflect_path::ReflectOwned::Struct(self)
}

#hash_fn

#partial_eq_fn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
#bevy_reflect_path::ReflectMut::TupleStruct(self)
}

fn reflect_owned(self: Box<Self>) -> #bevy_reflect_path::ReflectOwned {
#bevy_reflect_path::ReflectOwned::TupleStruct(self)
}

#hash_fn

#partial_eq_fn
Expand Down
4 changes: 4 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 @@ -103,6 +103,10 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream {
#bevy_reflect_path::ReflectMut::Value(self)
}

fn reflect_owned(self: Box<Self>) -> #bevy_reflect_path::ReflectOwned {
#bevy_reflect_path::ReflectOwned::Value(self)
}

#hash_fn

#partial_eq_fn
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
utility::NonGenericTypeInfoCell, DynamicInfo, Reflect, ReflectMut, ReflectRef, TypeInfo, Typed,
utility::NonGenericTypeInfoCell, DynamicInfo, Reflect, ReflectMut, ReflectOwned, ReflectRef,
TypeInfo, Typed,
};
use std::{
any::{Any, TypeId},
Expand Down Expand Up @@ -226,6 +227,11 @@ impl Reflect for DynamicArray {
ReflectMut::Array(self)
}

#[inline]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Array(self)
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_reflect/src/enums/dynamic_enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::utility::NonGenericTypeInfoCell;
use crate::{
enum_debug, enum_hash, enum_partial_eq, DynamicInfo, DynamicStruct, DynamicTuple, Enum,
Reflect, ReflectMut, ReflectRef, Struct, Tuple, TypeInfo, Typed, VariantFieldIter, VariantType,
Reflect, ReflectMut, ReflectOwned, ReflectRef, Struct, Tuple, TypeInfo, Typed,
VariantFieldIter, VariantType,
};
use std::any::Any;
use std::fmt::Formatter;
Expand Down Expand Up @@ -390,6 +391,11 @@ impl Reflect for DynamicEnum {
ReflectMut::Enum(self)
}

#[inline]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Enum(self)
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::any::Any;
use crate::utility::GenericTypeInfoCell;
use crate::{
Array, ArrayIter, FromReflect, FromType, GetTypeRegistration, List, ListInfo, Reflect,
ReflectFromPtr, ReflectMut, ReflectRef, TypeInfo, TypeRegistration, Typed,
ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypeRegistration, Typed,
};

impl<T: smallvec::Array + Send + Sync + 'static> Array for SmallVec<T>
Expand Down Expand Up @@ -115,6 +115,10 @@ where
ReflectMut::List(self)
}

fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::List(self)
}

fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(List::clone_dynamic(self))
}
Expand Down
23 changes: 22 additions & 1 deletion crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::std_traits::ReflectDefault;
use crate::{self as bevy_reflect, ReflectFromPtr};
use crate::{self as bevy_reflect, ReflectFromPtr, ReflectOwned};
use crate::{
map_apply, map_partial_eq, Array, ArrayInfo, ArrayIter, DynamicEnum, DynamicMap, Enum,
EnumInfo, FromReflect, FromType, GetTypeRegistration, List, ListInfo, Map, MapInfo, MapIter,
Expand Down Expand Up @@ -270,6 +270,10 @@ impl<T: FromReflect> Reflect for Vec<T> {
ReflectMut::List(self)
}

fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::List(self)
}

fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(List::clone_dynamic(self))
}
Expand Down Expand Up @@ -434,6 +438,10 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Reflect for HashMap<K, V> {
ReflectMut::Map(self)
}

fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Map(self)
}

fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
}
Expand Down Expand Up @@ -566,6 +574,11 @@ impl<T: Reflect, const N: usize> Reflect for [T; N] {
ReflectMut::Array(self)
}

#[inline]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Array(self)
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
Expand Down Expand Up @@ -678,6 +691,10 @@ impl Reflect for Cow<'static, str> {
ReflectMut::Value(self)
}

fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Value(self)
}

fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone())
}
Expand Down Expand Up @@ -868,6 +885,10 @@ impl<T: FromReflect> Reflect for Option<T> {
ReflectMut::Enum(self)
}

fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Enum(self)
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(Enum::clone_dynamic(self))
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fmt::{Debug, Formatter};

use crate::utility::NonGenericTypeInfoCell;
use crate::{
Array, ArrayIter, DynamicArray, DynamicInfo, FromReflect, Reflect, ReflectMut, ReflectRef,
TypeInfo, Typed,
Array, ArrayIter, DynamicArray, DynamicInfo, FromReflect, Reflect, ReflectMut, ReflectOwned,
ReflectRef, TypeInfo, Typed,
};

/// An ordered, mutable list of [Reflect] items. This corresponds to types like [`std::vec::Vec`].
Expand Down Expand Up @@ -246,6 +246,11 @@ impl Reflect for DynamicList {
ReflectMut::List(self)
}

#[inline]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::List(self)
}

#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(List::clone_dynamic(self))
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::hash::Hash;
use bevy_utils::{Entry, HashMap};

use crate::utility::NonGenericTypeInfoCell;
use crate::{DynamicInfo, Reflect, ReflectMut, ReflectRef, TypeInfo, Typed};
use crate::{DynamicInfo, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, Typed};

/// An ordered mapping between [`Reflect`] values.
///
Expand Down Expand Up @@ -300,6 +300,10 @@ impl Reflect for DynamicMap {
ReflectMut::Map(self)
}

fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Map(self)
}

fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
}
Expand Down
22 changes: 22 additions & 0 deletions crates/bevy_reflect/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ pub enum ReflectMut<'a> {
Value(&'a mut dyn Reflect),
}

/// An owned enumeration of "kinds" of reflected type.
///
/// Each variant contains a trait object with methods specific to a kind of
/// type.
///
/// A `ReflectOwned` is obtained via [`Reflect::reflect_owned`].
pub enum ReflectOwned {
Struct(Box<dyn Struct>),
TupleStruct(Box<dyn TupleStruct>),
Tuple(Box<dyn Tuple>),
List(Box<dyn List>),
Array(Box<dyn Array>),
Map(Box<dyn Map>),
Enum(Box<dyn Enum>),
Value(Box<dyn Reflect>),
}

/// A reflected Rust type.
///
/// Methods for working with particular kinds of Rust type are available using the [`Array`], [`List`],
Expand Down Expand Up @@ -139,6 +156,11 @@ pub trait Reflect: Any + Send + Sync {
/// See [`ReflectMut`].
fn reflect_mut(&mut self) -> ReflectMut;

/// Returns an owned enumeration of "kinds" of type.
///
/// See [`ReflectOwned`].
fn reflect_owned(self: Box<Self>) -> ReflectOwned;

/// Clones the value as a `Reflect` trait object.
///
/// When deriving `Reflect` for a struct, tuple struct or enum, the value is
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_reflect/src/struct_trait.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::utility::NonGenericTypeInfoCell;
use crate::{DynamicInfo, NamedField, Reflect, ReflectMut, ReflectRef, TypeInfo, Typed};
use crate::{
DynamicInfo, NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, Typed,
};
use bevy_utils::{Entry, HashMap};
use std::fmt::{Debug, Formatter};
use std::{
Expand Down Expand Up @@ -427,6 +429,11 @@ impl Reflect for DynamicStruct {
ReflectMut::Struct(self)
}

#[inline]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Struct(self)
}

fn apply(&mut self, value: &dyn Reflect) {
if let ReflectRef::Struct(struct_value) = value.reflect_ref() {
for (i, value) in struct_value.iter_fields().enumerate() {
Expand Down
13 changes: 11 additions & 2 deletions crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utility::NonGenericTypeInfoCell;
use crate::{
DynamicInfo, FromReflect, GetTypeRegistration, Reflect, ReflectMut, ReflectRef, TypeInfo,
TypeRegistration, Typed, UnnamedField,
DynamicInfo, FromReflect, GetTypeRegistration, Reflect, ReflectMut, ReflectOwned, ReflectRef,
TypeInfo, TypeRegistration, Typed, UnnamedField,
};
use std::any::{Any, TypeId};
use std::fmt::{Debug, Formatter};
Expand Down Expand Up @@ -341,6 +341,11 @@ impl Reflect for DynamicTuple {
ReflectMut::Tuple(self)
}

#[inline]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Tuple(self)
}

fn apply(&mut self, value: &dyn Reflect) {
tuple_apply(self, value);
}
Expand Down Expand Up @@ -540,6 +545,10 @@ macro_rules! impl_reflect_tuple {
ReflectMut::Tuple(self)
}

fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Tuple(self)
}

fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone_dynamic())
}
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_reflect/src/tuple_struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::utility::NonGenericTypeInfoCell;
use crate::{DynamicInfo, Reflect, ReflectMut, ReflectRef, TypeInfo, Typed, UnnamedField};
use crate::{
DynamicInfo, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, Typed, UnnamedField,
};
use std::any::{Any, TypeId};
use std::fmt::{Debug, Formatter};
use std::slice::Iter;
Expand Down Expand Up @@ -330,6 +332,11 @@ impl Reflect for DynamicTupleStruct {
ReflectMut::TupleStruct(self)
}

#[inline]
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::TupleStruct(self)
}

fn apply(&mut self, value: &dyn Reflect) {
if let ReflectRef::TupleStruct(tuple_struct) = value.reflect_ref() {
for (i, value) in tuple_struct.iter_fields().enumerate() {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_reflect/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::any::{Any, TypeId};
///
/// ```
/// # use std::any::Any;
/// # use bevy_reflect::{NamedField, Reflect, ReflectMut, ReflectRef, StructInfo, TypeInfo, ValueInfo};
/// # use bevy_reflect::{NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, StructInfo, TypeInfo, ValueInfo};
/// # use bevy_reflect::utility::NonGenericTypeInfoCell;
/// use bevy_reflect::Typed;
///
Expand Down Expand Up @@ -59,6 +59,7 @@ use std::any::{Any, TypeId};
/// # fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> { todo!() }
/// # fn reflect_ref(&self) -> ReflectRef { todo!() }
/// # fn reflect_mut(&mut self) -> ReflectMut { todo!() }
/// # fn reflect_owned(self: Box<Self>) -> ReflectOwned { todo!() }
/// # fn clone_value(&self) -> Box<dyn Reflect> { todo!() }
/// # }
/// ```
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_reflect/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::any::{Any, TypeId};
///
/// ```
/// # use std::any::Any;
/// # use bevy_reflect::{NamedField, Reflect, ReflectMut, ReflectRef, StructInfo, Typed, TypeInfo};
/// # use bevy_reflect::{NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, StructInfo, Typed, TypeInfo};
/// use bevy_reflect::utility::NonGenericTypeInfoCell;
///
/// struct Foo {
Expand Down Expand Up @@ -46,6 +46,7 @@ use std::any::{Any, TypeId};
/// # fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> { todo!() }
/// # fn reflect_ref(&self) -> ReflectRef { todo!() }
/// # fn reflect_mut(&mut self) -> ReflectMut { todo!() }
/// # fn reflect_owned(self: Box<Self>) -> ReflectOwned { todo!() }
/// # fn clone_value(&self) -> Box<dyn Reflect> { todo!() }
/// # }
/// ```
Expand Down Expand Up @@ -79,7 +80,7 @@ impl NonGenericTypeInfoCell {
///
/// ```
/// # use std::any::Any;
/// # use bevy_reflect::{Reflect, ReflectMut, ReflectRef, TupleStructInfo, Typed, TypeInfo, UnnamedField};
/// # use bevy_reflect::{Reflect, ReflectMut, ReflectOwned, ReflectRef, TupleStructInfo, Typed, TypeInfo, UnnamedField};
/// use bevy_reflect::utility::GenericTypeInfoCell;
///
/// struct Foo<T: Reflect>(T);
Expand Down Expand Up @@ -107,6 +108,7 @@ impl NonGenericTypeInfoCell {
/// # fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> { todo!() }
/// # fn reflect_ref(&self) -> ReflectRef { todo!() }
/// # fn reflect_mut(&mut self) -> ReflectMut { todo!() }
/// # fn reflect_owned(self: Box<Self>) -> ReflectOwned { todo!() }
/// # fn clone_value(&self) -> Box<dyn Reflect> { todo!() }
/// # }
/// ```
Expand Down