Skip to content

bevy_reflect: Allow parameters to be passed to type data #13723

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ impl App {
#[cfg(feature = "bevy_reflect")]
pub fn register_type_data<
T: bevy_reflect::Reflect + bevy_reflect::TypePath,
D: bevy_reflect::TypeData + bevy_reflect::FromType<T>,
D: bevy_reflect::CreateTypeData<T>,
>(
&mut self,
) -> &mut Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_app/src/sub_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ impl SubApp {
#[cfg(feature = "bevy_reflect")]
pub fn register_type_data<
T: bevy_reflect::Reflect + bevy_reflect::TypePath,
D: bevy_reflect::TypeData + bevy_reflect::FromType<T>,
D: bevy_reflect::CreateTypeData<T>,
>(
&mut self,
) -> &mut Self {
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::boxed::Box;
use core::any::{Any, TypeId};

use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, World};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect};
use bevy_reflect::{CreateTypeData, FromReflect, PartialReflect, Reflect};

use crate::{Asset, AssetId, Assets, Handle, UntypedAssetId, UntypedHandle};

Expand Down Expand Up @@ -132,8 +132,8 @@ impl ReflectAsset {
}
}

impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
fn from_type() -> Self {
impl<A: Asset + FromReflect> CreateTypeData<A> for ReflectAsset {
fn create_type_data(_input: ()) -> Self {
ReflectAsset {
handle_type_id: TypeId::of::<Handle<A>>(),
assets_resource_type_id: TypeId::of::<Assets<A>>(),
Expand Down Expand Up @@ -228,8 +228,8 @@ impl ReflectHandle {
}
}

impl<A: Asset> FromType<Handle<A>> for ReflectHandle {
fn from_type() -> Self {
impl<A: Asset> CreateTypeData<Handle<A>> for ReflectHandle {
fn create_type_data(_input: ()) -> Self {
ReflectHandle {
asset_type_id: TypeId::of::<A>(),
downcast_handle_untyped: |handle: &dyn Any| {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ impl MaybeLocation {
mod tests {
use bevy_ecs_macros::Resource;
use bevy_ptr::PtrMut;
use bevy_reflect::{FromType, ReflectFromPtr};
use bevy_reflect::{CreateTypeData, ReflectFromPtr};
use core::ops::{Deref, DerefMut};

use crate::{
Expand Down Expand Up @@ -1783,7 +1783,7 @@ mod tests {
changed_by: caller.as_mut(),
};

let reflect_from_ptr = <ReflectFromPtr as FromType<i32>>::from_type();
let reflect_from_ptr = <ReflectFromPtr as CreateTypeData<i32>>::create_type_data(());

let mut new = value.map_unchanged(|ptr| {
// SAFETY: The underlying type of `ptr` matches `reflect_from_ptr`.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/entity/clone_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ mod tests {
system::Commands,
};
use alloc::vec;
use bevy_reflect::{std_traits::ReflectDefault, FromType, Reflect, ReflectFromPtr};
use bevy_reflect::{std_traits::ReflectDefault, CreateTypeData, Reflect, ReflectFromPtr};

#[test]
fn clone_entity_using_reflect() {
Expand Down Expand Up @@ -977,7 +977,7 @@ mod tests {
registry
.get_mut(core::any::TypeId::of::<A>())
.unwrap()
.insert(<ReflectFromPtr as FromType<B>>::from_type());
.insert(<ReflectFromPtr as CreateTypeData<B>>::create_type_data(()));
}

let e = world.spawn(A).id();
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/reflect/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
world::{EntityMut, EntityWorldMut},
};
use bevy_reflect::{
FromReflect, FromType, PartialReflect, Reflect, ReflectRef, TypePath, TypeRegistry,
CreateTypeData, FromReflect, PartialReflect, Reflect, ReflectRef, TypePath, TypeRegistry,
};

use super::{from_reflect_with_fallback, ReflectComponent};
Expand Down Expand Up @@ -52,12 +52,12 @@ pub struct ReflectBundleFns {

impl ReflectBundleFns {
/// Get the default set of [`ReflectBundleFns`] for a specific bundle type using its
/// [`FromType`] implementation.
/// [`CreateTypeData`] implementation.
///
/// 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 {
<ReflectBundle as FromType<T>>::from_type().0
<ReflectBundle as CreateTypeData<T>>::create_type_data(()).0
}
}

Expand Down Expand Up @@ -153,8 +153,8 @@ impl ReflectBundle {
}
}

impl<B: Bundle + Reflect + TypePath + BundleFromComponents> FromType<B> for ReflectBundle {
fn from_type() -> Self {
impl<B: Bundle + Reflect + TypePath + BundleFromComponents> CreateTypeData<B> for ReflectBundle {
fn create_type_data(_input: ()) -> Self {
ReflectBundle(ReflectBundleFns {
insert: |entity, reflected_bundle, registry| {
let bundle = entity.world_scope(|world| {
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_ecs/src/reflect/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
//! [`get_type_registration`] method (see the relevant code[^1]).
//!
//! ```
//! # use bevy_reflect::{FromType, Reflect};
//! # use bevy_reflect::{CreateTypeData, Reflect};
//! # use bevy_ecs::prelude::{ReflectComponent, Component};
//! # #[derive(Default, Reflect, Component)]
//! # struct A;
//! # impl A {
//! # fn foo() {
//! # let mut registration = bevy_reflect::TypeRegistration::of::<A>();
//! registration.insert::<ReflectComponent>(FromType::<Self>::from_type());
//! registration.insert::<ReflectComponent>(CreateTypeData::<Self>::create_type_data(()));
//! # }
//! # }
//! ```
Expand All @@ -32,10 +32,10 @@
//! The user can access the `ReflectComponent` for type `T` through the type registry,
//! as per the `trait_reflection.rs` example.
//!
//! The `FromType::<Self>::from_type()` in the previous line calls the `FromType<C>`
//! implementation of `ReflectComponent`.
//! The `CreateTypeData::<Self>::create_type_data(())` in the previous line calls the
//! `CreateTypeData<C>` implementation of `ReflectComponent`.
//!
//! The `FromType<C>` impl creates a function per field of [`ReflectComponentFns`].
//! The `CreateTypeData<C>` impl creates a function per field of [`ReflectComponentFns`].
//! In those functions, we call generic methods on [`World`] and [`EntityWorldMut`].
//!
//! The result is a `ReflectComponent` completely independent of `C`, yet capable
Expand Down Expand Up @@ -69,7 +69,7 @@ use crate::{
FilteredEntityRef, World,
},
};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};
use bevy_reflect::{CreateTypeData, FromReflect, PartialReflect, Reflect, TypePath, TypeRegistry};
use disqualified::ShortName;

/// A struct used to operate on reflected [`Component`] trait of a type.
Expand Down Expand Up @@ -138,12 +138,12 @@ pub struct ReflectComponentFns {

impl ReflectComponentFns {
/// Get the default set of [`ReflectComponentFns`] for a specific component type using its
/// [`FromType`] implementation.
/// [`CreateTypeData`] implementation.
///
/// 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 {
<ReflectComponent as FromType<T>>::from_type().0
<ReflectComponent as CreateTypeData<T>>::create_type_data(()).0
}
}

Expand Down Expand Up @@ -312,8 +312,8 @@ impl ReflectComponent {
}
}

impl<C: Component + Reflect + TypePath> FromType<C> for ReflectComponent {
fn from_type() -> Self {
impl<C: Component + Reflect + TypePath> CreateTypeData<C> for ReflectComponent {
fn create_type_data(_input: ()) -> Self {
// TODO: Currently we panic if a component is immutable and you use
// reflection to mutate it. Perhaps the mutation methods should be fallible?
ReflectComponent(ReflectComponentFns {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/reflect/entity_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait ReflectCommandExt {
///
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::reflect::{ReflectCommandExt, ReflectBundle};
/// # use bevy_reflect::{FromReflect, FromType, Reflect, TypeRegistry};
/// # use bevy_reflect::{FromReflect, CreateTypeData, Reflect, TypeRegistry};
/// // A resource that can hold any component that implements reflect as a boxed reflect component
/// #[derive(Resource)]
/// struct Prefab {
Expand Down Expand Up @@ -126,7 +126,7 @@ pub trait ReflectCommandExt {
///
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::reflect::{ReflectCommandExt, ReflectBundle};
/// # use bevy_reflect::{FromReflect, FromType, Reflect, TypeRegistry};
/// # use bevy_reflect::{FromReflect, CreateTypeData, Reflect, TypeRegistry};
///
/// // A resource that can hold any component or bundle that implements reflect as a boxed reflect
/// #[derive(Resource)]
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/reflect/from_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! Same as [`super::component`], but for [`FromWorld`].

use alloc::boxed::Box;
use bevy_reflect::{FromType, Reflect};
use bevy_reflect::{CreateTypeData, Reflect};

use crate::world::{FromWorld, World};

Expand All @@ -27,12 +27,12 @@ pub struct ReflectFromWorldFns {

impl ReflectFromWorldFns {
/// Get the default set of [`ReflectFromWorldFns`] for a specific type using its
/// [`FromType`] implementation.
/// [`CreateTypeData`] implementation.
///
/// 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: Reflect + FromWorld>() -> Self {
<ReflectFromWorld as FromType<T>>::from_type().0
<ReflectFromWorld as CreateTypeData<T>>::create_type_data(()).0
}
}

Expand Down Expand Up @@ -78,8 +78,8 @@ impl ReflectFromWorld {
}
}

impl<B: Reflect + FromWorld> FromType<B> for ReflectFromWorld {
fn from_type() -> Self {
impl<B: Reflect + FromWorld> CreateTypeData<B> for ReflectFromWorld {
fn create_type_data(_input: ()) -> Self {
ReflectFromWorld(ReflectFromWorldFns {
from_world: |world| Box::new(B::from_world(world)),
})
Expand Down
6 changes: 3 additions & 3 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::{CreateTypeData, FromReflect, PartialReflect};

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

impl<C: FromReflect + MapEntities> FromType<C> for ReflectMapEntities {
fn from_type() -> Self {
impl<C: FromReflect + MapEntities> CreateTypeData<C> for ReflectMapEntities {
fn create_type_data(_input: ()) -> Self {
ReflectMapEntities {
map_entities: |reflected, mut mapper| {
let mut concrete = C::from_reflect(reflected).expect("reflected type should match");
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/reflect/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
resource::Resource,
world::{unsafe_world_cell::UnsafeWorldCell, FilteredResources, FilteredResourcesMut, World},
};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};
use bevy_reflect::{CreateTypeData, FromReflect, PartialReflect, Reflect, TypePath, TypeRegistry};

use super::from_reflect_with_fallback;

Expand Down Expand Up @@ -68,12 +68,12 @@ pub struct ReflectResourceFns {

impl ReflectResourceFns {
/// Get the default set of [`ReflectResourceFns`] for a specific resource type using its
/// [`FromType`] implementation.
/// [`CreateTypeData`] implementation.
///
/// 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 {
<ReflectResource as FromType<T>>::from_type().0
<ReflectResource as CreateTypeData<T>>::create_type_data(()).0
}
}

Expand Down Expand Up @@ -200,8 +200,8 @@ impl ReflectResource {
}
}

impl<R: Resource + FromReflect + TypePath> FromType<R> for ReflectResource {
fn from_type() -> Self {
impl<R: Resource + FromReflect + TypePath> CreateTypeData<R> for ReflectResource {
fn create_type_data(_input: ()) -> Self {
ReflectResource(ReflectResourceFns {
insert: |world, reflected_resource, registry| {
let resource = from_reflect_with_fallback::<R>(reflected_resource, world, registry);
Expand Down
10 changes: 5 additions & 5 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::{CreateTypeData, FromReflect, PartialReflect};

/// For a reflected value, apply an operation to all contained entities.
///
Expand All @@ -17,8 +17,8 @@ impl ReflectVisitEntities {
}
}

impl<C: FromReflect + VisitEntities> FromType<C> for ReflectVisitEntities {
fn from_type() -> Self {
impl<C: FromReflect + VisitEntities> CreateTypeData<C> for ReflectVisitEntities {
fn create_type_data(_input: ()) -> Self {
ReflectVisitEntities {
visit_entities: |component, f| {
let concrete = C::from_reflect(component).unwrap();
Expand Down Expand Up @@ -49,8 +49,8 @@ impl ReflectVisitEntitiesMut {
}
}

impl<C: FromReflect + VisitEntitiesMut> FromType<C> for ReflectVisitEntitiesMut {
fn from_type() -> Self {
impl<C: FromReflect + VisitEntitiesMut> CreateTypeData<C> for ReflectVisitEntitiesMut {
fn create_type_data(_input: ()) -> Self {
ReflectVisitEntitiesMut {
visit_entities_mut: |component, f| {
let mut concrete = C::from_reflect(component).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/system/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ mod tests {
system::{Local, RunSystemOnce},
};
use alloc::vec;
use bevy_reflect::{FromType, Reflect, ReflectRef};
use bevy_reflect::{CreateTypeData, Reflect, ReflectRef};

use super::*;

Expand Down Expand Up @@ -1087,7 +1087,7 @@ mod tests {
}),)
.build_state(&mut world)
.build_system(|res: FilteredResources| {
let reflect_resource = <ReflectResource as FromType<R>>::from_type();
let reflect_resource = <ReflectResource as CreateTypeData<R>>::create_type_data(());
let ReflectRef::Struct(reflect_struct) =
reflect_resource.reflect(res).unwrap().reflect_ref()
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use bevy_reflect::{FromType, Reflect};
use bevy_reflect::{CreateTypeData, Reflect};
use core::marker::PhantomData;

#[derive(Clone)]
struct ReflectMyTrait;

impl<T> FromType<T> for ReflectMyTrait {
fn from_type() -> Self {
impl<T> CreateTypeData<T> for ReflectMyTrait {
fn create_type_data(_input: ()) -> Self {
Self
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//@check-pass
use bevy_reflect::{FromType, Reflect};
use bevy_reflect::{CreateTypeData, Reflect};
use core::marker::PhantomData;

#[derive(Clone)]
struct ReflectMyTrait;

impl<T> FromType<T> for ReflectMyTrait {
fn from_type() -> Self {
impl<T> CreateTypeData<T> for ReflectMyTrait {
fn create_type_data(_input: ()) -> Self {
Self
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@no-rustfix
use bevy_reflect::{CreateTypeData, Reflect};

#[derive(Clone)]
struct ReflectMyTrait;

impl<T> CreateTypeData<T, f32> for ReflectMyTrait {
fn create_type_data(_: f32) -> Self {
todo!()
}
}

#[derive(Reflect)]
#[reflect(MyTrait)]
//~^ ERROR: mismatched types
struct RequiredArgs;

#[derive(Reflect)]
#[reflect(MyTrait(123))]
//~^ ERROR: mismatched types
struct WrongArgs;
Loading
Loading