Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: "`FromType` replaced by `CreateTypeData`"
pull_requests: [13723]
---

`FromType<T>` has been replaced by `CreateTypeData<T, Input = ()>`.
This was done to better communicate what the trait was for (i.e. creating type data),
as well as make it possible to pass in additional input when registering type data.

Implementors of `FromType<T>` will need to update their implementation:

```rust
// BEFORE
impl<T> FromType<T> for ReflectMyTrait {
fn from_type() -> Self {
// ...
}
}

// AFTER
impl<T> CreateTypeData<T> for ReflectMyTrait {
fn create_type_data(input: ()) -> Self {
// ...
}
}
```

Additionally, any calls made to `FromType::from_type` will need to be updated as well:

```rust
// BEFORE
<ReflectMyTrait as FromType<Foo>>::from_type()

// AFTER
<ReflectMyTrait as CreateTypeData<Foo>>::create_type_data(())
```
6 changes: 2 additions & 4 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bevy_ecs::{
};
use bevy_platform::collections::HashMap;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::{FromType, Reflect, TypeData, TypePath};
use bevy_reflect::{CreateTypeData, Reflect, TypePath};
use core::{fmt::Debug, num::NonZero, panic::AssertUnwindSafe};
use log::debug;

Expand Down Expand Up @@ -687,9 +687,7 @@ impl App {
///
/// See [`bevy_reflect::TypeRegistry::register_type_data`].
#[cfg(feature = "bevy_reflect")]
pub fn register_type_data<T: Reflect + TypePath, D: TypeData + FromType<T>>(
&mut self,
) -> &mut Self {
pub fn register_type_data<T: Reflect + TypePath, D: CreateTypeData<T>>(&mut self) -> &mut Self {
self.main_mut().register_type_data::<T, D>();
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 @@ -476,7 +476,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 @@ -8,7 +8,7 @@ use uuid::Uuid;
use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, World};
use bevy_reflect::{
serde::{ReflectDeserializerProcessor, ReflectSerializerProcessor},
FromReflect, FromType, PartialReflect, Reflect, TypeRegistry,
CreateTypeData, FromReflect, PartialReflect, Reflect, TypeRegistry,
};

use crate::{
Expand Down Expand Up @@ -156,8 +156,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 @@ -253,8 +253,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/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub const MAX_CHANGE_AGE: u32 = u32::MAX - (2 * CHECK_TICK_THRESHOLD - 1);
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 @@ -341,7 +341,7 @@ mod tests {
ticks,
};

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 @@ -1476,7 +1476,7 @@ mod tests {
use super::*;
use crate::reflect::{AppTypeRegistry, ReflectComponent, ReflectFromWorld};
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 @@ -1617,7 +1617,7 @@ mod tests {
registry
.get_mut(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 @@ -16,7 +16,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 @@ -53,12 +53,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 @@ -148,8 +148,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 @@ -70,7 +70,7 @@ use crate::{
},
};
use alloc::boxed::Box;
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};
use bevy_reflect::{CreateTypeData, FromReflect, PartialReflect, Reflect, TypePath, TypeRegistry};
use bevy_utils::prelude::DebugName;

/// A struct used to operate on reflected [`Component`] trait of a type.
Expand Down Expand Up @@ -139,12 +139,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 @@ -306,8 +306,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 @@ -37,7 +37,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 @@ -125,7 +125,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/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
reflect::from_reflect_with_fallback,
world::{DeferredWorld, World},
};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};
use bevy_reflect::{CreateTypeData, FromReflect, PartialReflect, Reflect, TypePath, TypeRegistry};

/// A struct used to operate on reflected [`Event`] trait of a type.
///
Expand Down Expand Up @@ -52,15 +52,15 @@ pub struct ReflectEventFns {

impl ReflectEventFns {
/// Get the default set of [`ReflectEventFns`] for a specific event type
/// using its [`FromType`] implementation.
/// using its [`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<'a, T: Event + FromReflect + TypePath>() -> Self
where
T::Trigger<'a>: Default,
{
<ReflectEvent as FromType<T>>::from_type().0
<ReflectEvent as CreateTypeData<T>>::create_type_data(()).0
}
}

Expand Down Expand Up @@ -122,11 +122,11 @@ impl ReflectEvent {
}
}

impl<'a, E: Event + Reflect + TypePath> FromType<E> for ReflectEvent
impl<'a, E: Event + Reflect + TypePath> CreateTypeData<E> for ReflectEvent
where
<E as Event>::Trigger<'a>: Default,
{
fn from_type() -> Self {
fn create_type_data(_input: ()) -> Self {
ReflectEvent(ReflectEventFns {
trigger: |world, reflected_event, registry| {
let event = from_reflect_with_fallback::<E>(reflected_event, world, registry);
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 [`component`](`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/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Same as [`component`](`super::component`), but for messages.

use crate::{message::Message, reflect::from_reflect_with_fallback, world::World};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};
use bevy_reflect::{CreateTypeData, FromReflect, PartialReflect, Reflect, TypePath, TypeRegistry};

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

impl ReflectMessageFns {
/// Get the default set of [`ReflectMessageFns`] for a specific event type
/// using its [`FromType`] implementation.
/// using its [`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<M: Message + FromReflect + TypePath>() -> Self {
<ReflectMessage as FromType<M>>::from_type().0
<ReflectMessage as CreateTypeData<M>>::create_type_data(()).0
}
}

Expand Down Expand Up @@ -97,8 +97,8 @@ impl ReflectMessage {
}
}

impl<M: Message + Reflect + TypePath> FromType<M> for ReflectMessage {
fn from_type() -> Self {
impl<M: Message + Reflect + TypePath> CreateTypeData<M> for ReflectMessage {
fn create_type_data(_input: ()) -> Self {
ReflectMessage(ReflectMessageFns {
write_message: |world, reflected_message, registry| {
let message = from_reflect_with_fallback::<M>(reflected_message, world, registry);
Expand Down
Loading
Loading