Skip to content
Open
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
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl ReflectAsset {
}
}

impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
impl<A: Asset + FromReflect + Reflect> FromType<A> for ReflectAsset {
fn from_type() -> Self {
ReflectAsset {
handle_type_id: TypeId::of::<Handle<A>>(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/reflect/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ReflectBundleFns {
///
/// This is useful if you want to start with the default implementation before overriding some
/// of the functions to create a custom implementation.
pub fn new<T: Bundle + FromReflect + TypePath + BundleFromComponents>() -> Self {
pub fn new<T: Bundle + FromReflect + Reflect + TypePath + BundleFromComponents>() -> Self {
<ReflectBundle as FromType<T>>::from_type().0
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/reflect/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl ReflectComponentFns {
///
/// This is useful if you want to start with the default implementation before overriding some
/// of the functions to create a custom implementation.
pub fn new<T: Component + FromReflect + TypePath>() -> Self {
pub fn new<T: Component + FromReflect + Reflect + TypePath>() -> Self {
<ReflectComponent as FromType<T>>::from_type().0
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/reflect/map_entities.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::entity::{EntityMapper, MapEntities};
use bevy_reflect::{FromReflect, FromType, PartialReflect};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect};

/// For a specific type of value, this maps any fields with values of type [`Entity`] to a new world.
///
Expand All @@ -25,7 +25,7 @@ impl ReflectMapEntities {
}
}

impl<C: FromReflect + MapEntities> FromType<C> for ReflectMapEntities {
impl<C: FromReflect + Reflect + MapEntities> FromType<C> for ReflectMapEntities {
fn from_type() -> Self {
ReflectMapEntities {
map_entities: |reflected, mut mapper| {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/reflect/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ReflectResourceFns {
///
/// This is useful if you want to start with the default implementation before overriding some
/// of the functions to create a custom implementation.
pub fn new<T: Resource + FromReflect + TypePath>() -> Self {
pub fn new<T: Resource + FromReflect + Reflect + TypePath>() -> Self {
<ReflectResource as FromType<T>>::from_type().0
}
}
Expand Down Expand Up @@ -200,7 +200,7 @@ impl ReflectResource {
}
}

impl<R: Resource + FromReflect + TypePath> FromType<R> for ReflectResource {
impl<R: Resource + FromReflect + Reflect + TypePath> FromType<R> for ReflectResource {
fn from_type() -> Self {
ReflectResource(ReflectResourceFns {
insert: |world, reflected_resource, registry| {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/reflect/visit_entities.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::entity::{Entity, VisitEntities, VisitEntitiesMut};
use bevy_reflect::{FromReflect, FromType, PartialReflect};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect};

/// For a reflected value, apply an operation to all contained entities.
///
Expand Down Expand Up @@ -49,7 +49,7 @@ impl ReflectVisitEntitiesMut {
}
}

impl<C: FromReflect + VisitEntitiesMut> FromType<C> for ReflectVisitEntitiesMut {
impl<C: FromReflect + Reflect + VisitEntitiesMut> FromType<C> for ReflectVisitEntitiesMut {
fn from_type() -> Self {
ReflectVisitEntitiesMut {
visit_entities_mut: |component, f| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() {
let mut foo: Box<dyn Struct> = Box::new(Foo::<NoReflect> { a: NoReflect(42.0) });
//~^ ERROR: `NoReflect` does not implement `GetTypeRegistration` so cannot provide type registration information
//~| ERROR: `NoReflect` does not implement `Typed` so cannot provide static type information
//~| ERROR: `NoReflect` does not implement `CastPartialReflect` so cannot be cast to `dyn PartialReflect`

// foo doesn't implement Reflect because NoReflect doesn't implement Reflect
foo.get_field::<NoReflect>("a").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ mod missing_attribute {
}

mod incorrect_inner_type {
use bevy_reflect::{FromReflect, GetTypeRegistration, reflect_remote};
use bevy_reflect::{FromReflect, GetTypeRegistration, Reflect, reflect_remote};

#[reflect_remote(super::external_crate::TheirOuter<T>)]
//~^ ERROR: `TheirInner<T>` does not implement `PartialReflect` so cannot be introspected
//~| ERROR: `TheirInner<T>` does not implement `PartialReflect` so cannot be introspected
//~| ERROR: `TheirInner<T>` does not implement `PartialReflect` so cannot be introspected
//~| ERROR: `TheirInner<T>` does not implement `TypePath` so cannot provide dynamic type path information
//~^ ERROR: `TheirInner<T>` does not implement `CastPartialReflect` so cannot be cast to `dyn PartialReflect`
//~| ERROR: `?` operator has incompatible types
struct MyOuter<T: FromReflect + GetTypeRegistration> {
struct MyOuter<T: FromReflect + Reflect + GetTypeRegistration> {
// Reason: Should not use `MyInner<T>` directly
pub inner: MyInner<T>,
//~^ ERROR: mismatched types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod external_crate {
}

#[reflect_remote(external_crate::TheirOuter<T>)]
struct MyOuter<T: FromReflect + Typed + GetTypeRegistration> {
struct MyOuter<T: FromReflect + Reflect + Typed + GetTypeRegistration> {
#[reflect(remote = MyInner<T>)]
pub inner: external_crate::TheirInner<T>,
}
Expand Down
51 changes: 51 additions & 0 deletions crates/bevy_reflect/derive/src/impls/casting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::derive_data::ReflectMeta;
use crate::where_clause_options::WhereClauseOptions;
use proc_macro2::TokenStream;
use quote::quote;

/// Generates impls for the `CastPartialReflect` and `CastReflect` traits.
pub(crate) fn impl_casting_traits(
meta: &ReflectMeta,
where_clause_options: &WhereClauseOptions,
) -> TokenStream {
let bevy_reflect_path = meta.bevy_reflect_path();
let type_path = meta.type_path();
let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();
let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);

quote! {
impl #impl_generics #bevy_reflect_path::cast::CastPartialReflect for #type_path #ty_generics #where_reflect_clause {
#[inline]
fn as_partial_reflect(&self) -> &dyn #bevy_reflect_path::PartialReflect {
self
}

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

#[inline]
fn into_partial_reflect(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::__macro_exports::alloc_utils::Box<dyn #bevy_reflect_path::PartialReflect> {
self
}
}

impl #impl_generics #bevy_reflect_path::cast::CastReflect for #type_path #ty_generics #where_reflect_clause {
#[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 into_reflect(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::__macro_exports::alloc_utils::Box<dyn #bevy_reflect_path::Reflect> {
self
}
}
}
}
35 changes: 5 additions & 30 deletions crates/bevy_reflect/derive/src/impls/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy_macro_utils::fq_std::{FQAny, FQOption, FQResult};

use quote::quote;

use crate::impls::casting::impl_casting_traits;
use crate::{derive_data::ReflectMeta, where_clause_options::WhereClauseOptions};

pub fn impl_full_reflect(
Expand Down Expand Up @@ -50,25 +51,12 @@ pub fn impl_full_reflect(
}
};

let casting_impls = impl_casting_traits(meta, where_clause_options);

quote! {
impl #impl_generics #bevy_reflect_path::Reflect for #type_path #ty_generics #where_reflect_clause {
#any_impls

#[inline]
fn into_reflect(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::__macro_exports::alloc_utils::Box<dyn #bevy_reflect_path::Reflect> {
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 set(
&mut self,
Expand All @@ -78,6 +66,8 @@ pub fn impl_full_reflect(
#FQResult::Ok(())
}
}

#casting_impls
}
}

Expand Down Expand Up @@ -134,21 +124,6 @@ pub fn common_partial_reflect_methods(
#FQOption::Some(self)
}

#[inline]
fn into_partial_reflect(self: #bevy_reflect_path::__macro_exports::alloc_utils::Box<Self>) -> #bevy_reflect_path::__macro_exports::alloc_utils::Box<dyn #bevy_reflect_path::PartialReflect> {
self
}

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

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

#hash_fn

#partial_eq_fn
Expand Down
13 changes: 10 additions & 3 deletions crates/bevy_reflect/derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,25 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
is_mutable: bool,
bevy_reflect_path: &Path,
) -> proc_macro2::TokenStream {
let method = if is_mutable {
let remote_method = if is_mutable {
quote!(as_wrapper_mut)
} else {
quote!(as_wrapper)
};
let cast_method = if is_mutable {
quote!(#bevy_reflect_path::cast::CastPartialReflect::as_partial_reflect_mut)
} else {
quote!(#bevy_reflect_path::cast::CastPartialReflect::as_partial_reflect)
};

field
.attrs
.remote
.as_ref()
.map(|ty| quote!(<#ty as #bevy_reflect_path::ReflectRemote>::#method(#ident)))
.unwrap_or_else(|| quote!(#ident))
.map(
|ty| quote!(<#ty as #bevy_reflect_path::ReflectRemote>::#remote_method(#ident)),
)
.unwrap_or_else(|| quote!(#cast_method(#ident)))
}

match &variant.fields {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_reflect/derive/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod assertions;
mod casting;
mod common;
mod enums;
#[cfg(feature = "functions")]
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_reflect/derive/src/struct_utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ impl FieldAccessors {
<#wrapper_ty as #bevy_reflect_path::ReflectRemote>::as_wrapper(&#accessor)
}
}
None => quote!(& #accessor),
None => {
quote!(#bevy_reflect_path::cast::CastPartialReflect::as_partial_reflect(&#accessor))
}
}
});
let fields_mut = Self::get_fields(reflect_struct, |field, accessor| {
Expand All @@ -38,7 +40,9 @@ impl FieldAccessors {
<#wrapper_ty as #bevy_reflect_path::ReflectRemote>::as_wrapper_mut(&mut #accessor)
}
}
None => quote!(&mut #accessor),
None => {
quote!(#bevy_reflect_path::cast::CastPartialReflect::as_partial_reflect_mut(&mut #accessor))
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/derive/src/where_clause_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ impl<'a, 'b> WhereClauseOptions<'a, 'b> {
}
}

/// The `PartialReflect` or `FromReflect` bound to use based on `#[reflect(from_reflect = false)]`.
/// The `CastPartialReflect` or `FromReflect` bound to use based on `#[reflect(from_reflect = false)]`.
fn reflect_bound(&self) -> TokenStream {
let bevy_reflect_path = self.meta.bevy_reflect_path();

if self.meta.from_reflect().should_auto_derive() {
quote!(#bevy_reflect_path::FromReflect)
} else {
quote!(#bevy_reflect_path::PartialReflect)
quote!(#bevy_reflect_path::cast::CastPartialReflect)
}
}

Expand Down
24 changes: 5 additions & 19 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::generics::impl_generic_info_methods;
use crate::{
type_info::impl_type_methods, utility::reflect_hasher, ApplyError, Generics, MaybeTyped,
PartialReflect, Reflect, ReflectKind, ReflectMut, ReflectOwned, ReflectRef, Type, TypeInfo,
TypePath,
cast::impl_cast_partial_reflect, type_info::impl_type_methods, utility::reflect_hasher,
ApplyError, Generics, MaybeTyped, PartialReflect, Reflect, ReflectKind, ReflectMut,
ReflectOwned, ReflectRef, Type, TypeInfo, TypePath,
};
use alloc::{boxed::Box, vec::Vec};
use bevy_reflect_derive::impl_type_path;
Expand Down Expand Up @@ -201,21 +201,6 @@ impl PartialReflect for DynamicArray {
self.represented_type
}

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

#[inline]
fn as_partial_reflect(&self) -> &dyn PartialReflect {
self
}

#[inline]
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
self
}

fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
Err(self)
}
Expand Down Expand Up @@ -358,6 +343,7 @@ impl<'a> IntoIterator for &'a DynamicArray {
}

impl_type_path!((in bevy_reflect) DynamicArray);
impl_cast_partial_reflect!(for DynamicArray);

/// An iterator over an [`Array`].
pub struct ArrayIter<'a> {
Expand Down Expand Up @@ -519,7 +505,7 @@ mod tests {
usize::MAX
};

let b = Box::new([(); SIZE]).into_reflect();
let b = (Box::new([(); SIZE]) as Box<dyn Reflect>).into_reflect();

let array = b.reflect_ref().as_array().unwrap();

Expand Down
Loading