From 936dd9bc679c3847cd176b9ab2ab3a366cd6f233 Mon Sep 17 00:00:00 2001 From: Gino Valente Date: Mon, 22 Aug 2022 23:06:31 -0700 Subject: [PATCH 1/4] Added Reflectable trait --- crates/bevy_reflect/src/lib.rs | 8 ++++--- crates/bevy_reflect/src/reflectable.rs | 33 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 crates/bevy_reflect/src/reflectable.rs diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 61479625e3bc0..5505bad824277 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -546,6 +546,7 @@ mod list; mod map; mod path; mod reflect; +mod reflectable; mod remote; mod set; mod struct_trait; @@ -602,6 +603,7 @@ pub use list::*; pub use map::*; pub use path::*; pub use reflect::*; +pub use reflectable::*; pub use remote::*; pub use set::*; pub use struct_trait::*; @@ -2756,7 +2758,7 @@ bevy_reflect::tests::Test { } #[reflect_remote(external_crate::TheirOuter)] - struct MyOuter { + struct MyOuter { #[reflect(remote = MyInner)] pub a: external_crate::TheirInner, #[reflect(remote = MyInner)] @@ -2804,7 +2806,7 @@ bevy_reflect::tests::Test { #[reflect_remote(external_crate::TheirOuter)] #[derive(Debug)] - enum MyOuter { + enum MyOuter { Unit, Tuple(#[reflect(remote = MyInner)] external_crate::TheirInner), Struct { @@ -2917,7 +2919,7 @@ bevy_reflect::tests::Test { } #[reflect_remote(external_crate::TheirOuter)] - struct MyOuter { + struct MyOuter { #[reflect(remote = MyInner)] pub inner: external_crate::TheirInner, } diff --git a/crates/bevy_reflect/src/reflectable.rs b/crates/bevy_reflect/src/reflectable.rs new file mode 100644 index 0000000000000..a28561117f7bb --- /dev/null +++ b/crates/bevy_reflect/src/reflectable.rs @@ -0,0 +1,33 @@ +use crate::{GetTypeRegistration, Reflect, TypePath, Typed}; + +/// A catch-all trait that is bound by the core reflection traits, +/// useful for simpler reflection-based generic type bounds. +/// +/// > __Note:__ You do _not_ need to implement this trait manually. +/// > It is automatically implemented for all types that implement its supertraits. +/// > And these supertraits are all automatically derived with the [`Reflect` derive]. +/// +/// This should namely be used to bound generic arguments to the necessary traits for reflection. +/// Doing this has the added benefit of reducing migration costs, as a change to the required traits +/// is automatically handled by this trait. +/// +/// For now, the supertraits of this trait includes: +/// * [`Reflect`] +/// * [`GetTypeRegistration`] +/// * [`Typed`] +/// * [`TypePath`] +/// +/// ## Example +/// +/// ``` +/// # use bevy_reflect::{Reflect, Reflectable}; +/// #[derive(Reflect)] +/// struct MyStruct { +/// value: T +/// } +/// ``` +/// +/// [`Reflect` derive]: bevy_reflect_derive::Reflect +pub trait Reflectable: Reflect + GetTypeRegistration + Typed + TypePath {} + +impl Reflectable for T {} From 88cd4a24901285c8f571924d4ea69e246bc52846 Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Tue, 30 Aug 2022 21:15:00 -0700 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Alice Cecile --- crates/bevy_reflect/src/reflectable.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_reflect/src/reflectable.rs b/crates/bevy_reflect/src/reflectable.rs index a28561117f7bb..98aa58e3a8f1c 100644 --- a/crates/bevy_reflect/src/reflectable.rs +++ b/crates/bevy_reflect/src/reflectable.rs @@ -3,9 +3,9 @@ use crate::{GetTypeRegistration, Reflect, TypePath, Typed}; /// A catch-all trait that is bound by the core reflection traits, /// useful for simpler reflection-based generic type bounds. /// -/// > __Note:__ You do _not_ need to implement this trait manually. -/// > It is automatically implemented for all types that implement its supertraits. -/// > And these supertraits are all automatically derived with the [`Reflect` derive]. +/// You do _not_ need to implement this trait manually. +/// It is automatically implemented for all types that implement its supertraits. +/// And these supertraits are all automatically derived with the [`Reflect` derive macro]. /// /// This should namely be used to bound generic arguments to the necessary traits for reflection. /// Doing this has the added benefit of reducing migration costs, as a change to the required traits @@ -27,7 +27,7 @@ use crate::{GetTypeRegistration, Reflect, TypePath, Typed}; /// } /// ``` /// -/// [`Reflect` derive]: bevy_reflect_derive::Reflect +/// [`Reflect` derive macro]: bevy_reflect_derive::Reflect pub trait Reflectable: Reflect + GetTypeRegistration + Typed + TypePath {} impl Reflectable for T {} From d64ecb8cc155e3f9a1ca0843e09f4592cd6d5f03 Mon Sep 17 00:00:00 2001 From: Gino Valente Date: Tue, 30 Aug 2022 21:46:30 -0700 Subject: [PATCH 3/4] Update documentation for Reflectable --- crates/bevy_reflect/src/reflect.rs | 4 ++++ crates/bevy_reflect/src/type_info.rs | 4 ++++ crates/bevy_reflect/src/type_registry.rs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/crates/bevy_reflect/src/reflect.rs b/crates/bevy_reflect/src/reflect.rs index 9efa868e6cfc0..6538d55c29a3e 100644 --- a/crates/bevy_reflect/src/reflect.rs +++ b/crates/bevy_reflect/src/reflect.rs @@ -399,10 +399,14 @@ where /// Doing so will automatically implement this trait, [`PartialReflect`], and many other useful traits for reflection, /// including one of the appropriate subtraits: [`Struct`], [`TupleStruct`] or [`Enum`]. /// +/// If you need to use this trait as a generic bound along with other reflection traits, +/// for your convenience, consider using [`Reflectable`] instead. +/// /// See the [crate-level documentation] to see how this trait can be used. /// /// [`bevy_reflect`]: crate /// [the derive macro]: bevy_reflect_derive::Reflect +/// [`Reflectable`]: crate::Reflectable /// [crate-level documentation]: crate #[diagnostic::on_unimplemented( message = "`{Self}` does not implement `Reflect` so cannot be fully reflected", diff --git a/crates/bevy_reflect/src/type_info.rs b/crates/bevy_reflect/src/type_info.rs index bc1ef1ef12e8b..fdd59396daea8 100644 --- a/crates/bevy_reflect/src/type_info.rs +++ b/crates/bevy_reflect/src/type_info.rs @@ -14,6 +14,9 @@ use thiserror::Error; /// This trait is automatically implemented by the [`#[derive(Reflect)]`](derive@crate::Reflect) macro /// and allows type information to be processed without an instance of that type. /// +/// If you need to use this trait as a generic bound along with other reflection traits, +/// for your convenience, consider using [`Reflectable`] instead. +/// /// # Implementing /// /// While it is recommended to leave implementing this trait to the `#[derive(Reflect)]` macro, @@ -81,6 +84,7 @@ use thiserror::Error; /// # } /// ``` /// +/// [`Reflectable`]: crate::Reflectable /// [utility]: crate::utility #[diagnostic::on_unimplemented( message = "`{Self}` does not implement `Typed` so cannot provide static type information", diff --git a/crates/bevy_reflect/src/type_registry.rs b/crates/bevy_reflect/src/type_registry.rs index 1fe7208956ec5..fa74e51950570 100644 --- a/crates/bevy_reflect/src/type_registry.rs +++ b/crates/bevy_reflect/src/type_registry.rs @@ -53,8 +53,12 @@ impl Debug for TypeRegistryArc { /// This trait is automatically implemented for items using [`#[derive(Reflect)]`](derive@crate::Reflect). /// The macro also allows [`TypeData`] to be more easily registered. /// +/// If you need to use this trait as a generic bound along with other reflection traits, +/// for your convenience, consider using [`Reflectable`] instead. +/// /// See the [crate-level documentation] for more information on type registration. /// +/// [`Reflectable`]: crate::Reflectable /// [crate-level documentation]: crate #[diagnostic::on_unimplemented( message = "`{Self}` does not implement `GetTypeRegistration` so cannot provide type registration information", From 2d179e1ac483a57c178f86382a453fab5af18561 Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:24:32 -0700 Subject: [PATCH 4/4] Update crates/bevy_reflect/src/reflectable.rs Co-authored-by: Alice Cecile --- crates/bevy_reflect/src/reflectable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_reflect/src/reflectable.rs b/crates/bevy_reflect/src/reflectable.rs index 98aa58e3a8f1c..08226bc214747 100644 --- a/crates/bevy_reflect/src/reflectable.rs +++ b/crates/bevy_reflect/src/reflectable.rs @@ -1,7 +1,7 @@ use crate::{GetTypeRegistration, Reflect, TypePath, Typed}; /// A catch-all trait that is bound by the core reflection traits, -/// useful for simpler reflection-based generic type bounds. +/// useful to simplify reflection-based generic type bounds. /// /// You do _not_ need to implement this trait manually. /// It is automatically implemented for all types that implement its supertraits.