Skip to content
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
2 changes: 1 addition & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ impl App {
#[cfg(feature = "bevy_reflect")]
pub fn register_type_data<
T: bevy_reflect::Reflect + 'static,
D: bevy_reflect::TypeData + bevy_reflect::FromType<T>,
D: bevy_reflect::BaseTypeData + bevy_reflect::TypeData<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
@@ -1,7 +1,7 @@
use std::any::{Any, TypeId};

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

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

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

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

impl<A: Asset> FromType<Handle<A>> for ReflectHandle {
fn from_type() -> Self {
impl<A: Asset> TypeData<Handle<A>> for ReflectHandle {
fn create_type_data() -> 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 @@ -902,7 +902,7 @@ impl std::fmt::Debug for MutUntyped<'_> {
mod tests {
use bevy_ecs_macros::Resource;
use bevy_ptr::PtrMut;
use bevy_reflect::{FromType, ReflectFromPtr};
use bevy_reflect::{ReflectFromPtr, TypeData};

use crate::{
self as bevy_ecs,
Expand Down Expand Up @@ -1160,7 +1160,7 @@ mod tests {
ticks,
};

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

let mut new = value.map_unchanged(|ptr| {
// SAFETY: The underlying type of `ptr` matches `reflect_from_ptr`.
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 @@ -9,7 +9,7 @@ use crate::{
prelude::Bundle,
world::{EntityWorldMut, FromWorld, World},
};
use bevy_reflect::{FromType, Reflect, ReflectRef, TypeRegistry};
use bevy_reflect::{Reflect, ReflectRef, TypeData, TypeRegistry};

use super::ReflectComponent;

Expand Down Expand Up @@ -39,12 +39,12 @@ pub struct ReflectBundleFns {

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

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

impl<B: Bundle + Reflect + FromWorld> FromType<B> for ReflectBundle {
fn from_type() -> Self {
impl<B: Bundle + Reflect + FromWorld> TypeData<B> for ReflectBundle {
fn create_type_data() -> Self {
ReflectBundle(ReflectBundleFns {
from_world: |world| Box::new(B::from_world(world)),
insert: |entity, reflected_bundle| {
Expand Down
17 changes: 9 additions & 8 deletions crates/bevy_ecs/src/reflect/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
//! [`get_type_registration`] method (see the relevant code[^1]).
//!
//! ```ignore
//! registration.insert::<ReflectComponent>(FromType::<Self>::from_type());
//! registration.register::<Self, ReflectComponent>();
//! ```
//!
//! This line adds a `ReflectComponent` to the registration data for the type in question.
//! 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>`
//! The `.register::<Self, ReflectComponent>()` in the previous line calls the `TypeData<C>`
//! implementation of `ReflectComponent`.
//!
//! The `FromType<C>` impl creates a function per field of [`ReflectComponentFns`].
//! The [`TypeData<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 All @@ -45,14 +45,15 @@
//! [^1]: `crates/bevy_reflect/bevy_reflect_derive/src/registration.rs`
//!
//! [`get_type_registration`]: bevy_reflect::GetTypeRegistration::get_type_registration
//! [`TypeData<C>`]: bevy_reflect::TypeData

use crate::{
change_detection::Mut,
component::Component,
entity::Entity,
world::{unsafe_world_cell::UnsafeEntityCell, EntityRef, EntityWorldMut, FromWorld, World},
};
use bevy_reflect::{FromType, Reflect};
use bevy_reflect::{Reflect, TypeData};

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

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

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

impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
fn from_type() -> Self {
impl<C: Component + Reflect + FromWorld> TypeData<C> for ReflectComponent {
fn create_type_data() -> Self {
ReflectComponent(ReflectComponentFns {
from_world: |world| Box::new(C::from_world(world)),
insert: |entity, reflected_component| {
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 @@ -34,7 +34,7 @@ pub trait ReflectCommandExt {
///
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::reflect::ReflectCommandExt;
/// # use bevy_reflect::{FromReflect, FromType, Reflect, TypeRegistry};
/// # use bevy_reflect::{FromReflect, 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 @@ -104,7 +104,7 @@ pub trait ReflectCommandExt {
///
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::reflect::ReflectCommandExt;
/// # use bevy_reflect::{FromReflect, FromType, Reflect, TypeRegistry};
/// # use bevy_reflect::{FromReflect, Reflect, TypeRegistry};
///
/// // A resource that can hold any component that implements reflect as a boxed reflect component
/// #[derive(Resource)]
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
Expand Up @@ -3,7 +3,7 @@ use crate::{
entity::{Entity, EntityMapper, MapEntities},
world::World,
};
use bevy_reflect::FromType;
use bevy_reflect::TypeData;
use bevy_utils::HashMap;

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

impl<C: Component + MapEntities> FromType<C> for ReflectMapEntities {
fn from_type() -> Self {
impl<C: Component + MapEntities> TypeData<C> for ReflectMapEntities {
fn create_type_data() -> Self {
ReflectMapEntities {
map_entities: |world, entity_mapper, entities| {
for &entity in entities {
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 @@ -9,7 +9,7 @@ use crate::{
system::Resource,
world::{unsafe_world_cell::UnsafeWorldCell, FromWorld, World},
};
use bevy_reflect::{FromType, Reflect};
use bevy_reflect::{Reflect, TypeData};

/// A struct used to operate on reflected [`Resource`] of a type.
///
Expand Down Expand Up @@ -61,12 +61,12 @@ pub struct ReflectResourceFns {

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

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

impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
fn from_type() -> Self {
impl<C: Resource + Reflect + FromWorld> TypeData<C> for ReflectResource {
fn create_type_data() -> Self {
ReflectResource(ReflectResourceFns {
insert: |world, reflected_resource| {
let mut resource = C::from_world(world);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub fn derive_type_uuid(input: TokenStream) -> TokenStream {
/// Because of this, **it can only be used on [object-safe] traits.**
///
/// For a trait named `MyTrait`, this will generate the struct `ReflectMyTrait`.
/// The generated struct can be created using `FromType` with any type that implements the trait.
/// The generated struct can be created using `TypeData` with any type that implements the trait.
/// The creation and registration of this generated struct as type data can be automatically handled
/// by [`#[derive(Reflect)]`](Reflect).
///
Expand All @@ -324,7 +324,7 @@ pub fn derive_type_uuid(input: TokenStream) -> TokenStream {
/// }
///
/// // We can create the type data manually if we wanted:
/// let my_trait: ReflectMyTrait = FromType::<SomeStruct>::from_type();
/// let my_trait: ReflectMyTrait = TypeData::<SomeStruct>::create_type_data();
///
/// // Or we can simply get it from the registry:
/// let mut registry = TypeRegistry::default();
Expand Down
20 changes: 11 additions & 9 deletions crates/bevy_reflect/bevy_reflect_derive/src/registration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Contains code related specifically to Bevy's type registration.

use crate::derive_data::ReflectMeta;
use crate::fq_std::FQOption;
use crate::utility::{extend_where_clause, WhereClauseOptions};
use bit_set::BitSet;
use quote::quote;
Expand All @@ -20,7 +21,7 @@ pub(crate) fn impl_get_type_registration(

let from_reflect_data = if meta.from_reflect().should_auto_derive() {
Some(quote! {
registration.insert::<#bevy_reflect_path::ReflectFromReflect>(#bevy_reflect_path::FromType::<Self>::from_type());
.register::<Self, #bevy_reflect_path::ReflectFromReflect>()
})
} else {
None
Expand All @@ -29,21 +30,22 @@ pub(crate) fn impl_get_type_registration(
let serialization_data = serialization_denylist.map(|denylist| {
let denylist = denylist.into_iter();
quote! {
let ignored_indices = ::core::iter::IntoIterator::into_iter([#(#denylist),*]);
registration.insert::<#bevy_reflect_path::serde::SerializationData>(#bevy_reflect_path::serde::SerializationData::new(ignored_indices));
.insert(#bevy_reflect_path::serde::SerializationData::new({
let ignored_indices = ::core::iter::IntoIterator::into_iter([#(#denylist),*]);
ignored_indices
}), #FQOption::None)
}
});

quote! {
#[allow(unused_mut)]
impl #impl_generics #bevy_reflect_path::GetTypeRegistration for #type_path #ty_generics #where_reflect_clause {
fn get_type_registration() -> #bevy_reflect_path::TypeRegistration {
let mut registration = #bevy_reflect_path::TypeRegistration::of::<Self>();
registration.insert::<#bevy_reflect_path::ReflectFromPtr>(#bevy_reflect_path::FromType::<Self>::from_type());
#from_reflect_data
#serialization_data
#(registration.insert::<#registration_data>(#bevy_reflect_path::FromType::<Self>::from_type());)*
registration
#bevy_reflect_path::TypeRegistration::of::<Self>()
.register::<Self, #bevy_reflect_path::ReflectFromPtr>()
#from_reflect_data
#serialization_data
#(.register::<Self, #registration_data>())*
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub(crate) fn reflect_trait(_args: &TokenStream, input: TokenStream) -> TokenStr
}
}

impl<T: #trait_ident + #bevy_reflect_path::Reflect> #bevy_reflect_path::FromType<T> for #reflect_trait_ident {
fn from_type() -> Self {
impl<T: #trait_ident + #bevy_reflect_path::Reflect> #bevy_reflect_path::TypeData<T> for #reflect_trait_ident {
fn create_type_data() -> Self {
Self {
get_func: |reflect_value| {
<dyn #bevy_reflect_path::Reflect>::downcast_ref::<T>(reflect_value).map(|value| value as &dyn #trait_ident)
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/src/from_reflect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FromType, Reflect};
use crate::{Reflect, TypeData};

/// A trait that enables types to be dynamically constructed from reflected data.
///
Expand Down Expand Up @@ -111,8 +111,8 @@ impl ReflectFromReflect {
}
}

impl<T: FromReflect> FromType<T> for ReflectFromReflect {
fn from_type() -> Self {
impl<T: FromReflect> TypeData<T> for ReflectFromReflect {
fn create_type_data() -> Self {
Self {
from_reflect: |reflect_value| {
T::from_reflect(reflect_value).map(|value| Box::new(value) as Box<dyn Reflect>)
Expand Down
10 changes: 4 additions & 6 deletions crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::any::Any;

use crate::utility::GenericTypeInfoCell;
use crate::{
self as bevy_reflect, FromReflect, FromType, GetTypeRegistration, List, ListInfo, ListIter,
Reflect, ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypePath,
TypeRegistration, Typed,
self as bevy_reflect, FromReflect, GetTypeRegistration, List, ListInfo, ListIter, Reflect,
ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypePath, TypeRegistration,
Typed,
};

impl<T: smallvec::Array + TypePath + Send + Sync> List for SmallVec<T>
Expand Down Expand Up @@ -176,8 +176,6 @@ where
T::Item: FromReflect,
{
fn get_type_registration() -> TypeRegistration {
let mut registration = TypeRegistration::of::<SmallVec<T>>();
registration.insert::<ReflectFromPtr>(FromType::<SmallVec<T>>::from_type());
registration
TypeRegistration::of::<SmallVec<T>>().register::<Self, ReflectFromPtr>()
}
}
Loading