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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ ios_simulator = ["bevy_internal/ios_simulator"]
# Enable built in global state machines
bevy_state = ["bevy_internal/bevy_state"]

# Enable function reflection
reflect_functions = ["bevy_internal/reflect_functions"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on naming? I originally had function_reflection, but I switched it to reflect_functions since it sounds a bit more like the bevy_reflect/functions.

I'm also trying to think about potential future features. For example, we have a bevy_reflect/documentation feature. Using this naming strategy, it could be reflect_documentation. And so on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like this naming.


[dependencies]
bevy_internal = { path = "crates/bevy_internal", version = "0.14.0-dev", default-features = false }

Expand Down Expand Up @@ -2158,6 +2161,7 @@ wasm = false
name = "function_reflection"
path = "examples/reflection/function_reflection.rs"
doc-scrape-examples = true
required-features = ["reflect_functions"]

[package.metadata.example.function_reflection]
name = "Function Reflection"
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ ios_simulator = ["bevy_pbr?/ios_simulator", "bevy_render?/ios_simulator"]
# Enable built in global state machines
bevy_state = ["dep:bevy_state"]

# Enable function reflection
reflect_functions = ["bevy_reflect/functions"]

[dependencies]
# bevy
bevy_a11y = { path = "../bevy_a11y", version = "0.14.0-dev" }
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ smallvec = ["dep:smallvec"]
uuid = ["dep:uuid"]
# When enabled, allows documentation comments to be accessed via reflection
documentation = ["bevy_reflect_derive/documentation"]
# Enables function reflection
functions = ["bevy_reflect_derive/functions"]

[dependencies]
# bevy
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_reflect/compile_fail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
publish = false

[dependencies]
bevy_reflect = { path = "../" }
bevy_reflect = { path = "../", features = ["functions"] }

[dev-dependencies]
compile_fail_utils = { path = "../../../tools/compile_fail_utils" }
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_reflect/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ proc-macro = true
default = []
# When enabled, allows documentation comments to be processed by the reflection macros
documentation = []
# Enables macro logic related to function reflection
functions = []

[dependencies]
bevy_macro_utils = { path = "../../bevy_macro_utils", version = "0.14.0-dev" }
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_reflect/derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::derive_data::{EnumVariantFields, ReflectEnum, StructField};
use crate::enum_utility::{EnumVariantOutputData, TryApplyVariantBuilder, VariantBuilder};
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
use crate::impls::{impl_type_path, impl_typed};
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQOption, FQResult};
use proc_macro2::{Ident, Span};
use quote::quote;
Expand Down Expand Up @@ -65,7 +65,11 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream

let type_path_impl = impl_type_path(reflect_enum.meta());

let function_impls = impl_function_traits(reflect_enum.meta(), &where_clause_options);
#[cfg(not(feature = "functions"))]
let function_impls = None::<proc_macro2::TokenStream>;
#[cfg(feature = "functions")]
let function_impls =
crate::impls::impl_function_traits(reflect_enum.meta(), &where_clause_options);

let get_type_registration_impl = reflect_enum.get_type_registration(&where_clause_options);

Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_reflect/derive/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod enums;
#[cfg(feature = "functions")]
mod func;
mod structs;
mod tuple_structs;
mod typed;
mod values;

pub(crate) use enums::impl_enum;
#[cfg(feature = "functions")]
pub(crate) use func::impl_function_traits;
pub(crate) use structs::impl_struct;
pub(crate) use tuple_structs::impl_tuple_struct;
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_reflect/derive/src/impls/structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
use crate::impls::{impl_type_path, impl_typed};
use crate::utility::ident_or_index;
use crate::ReflectStruct;
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult};
Expand Down Expand Up @@ -54,7 +54,11 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS

let type_path_impl = impl_type_path(reflect_struct.meta());

let function_impls = impl_function_traits(reflect_struct.meta(), &where_clause_options);
#[cfg(not(feature = "functions"))]
let function_impls = None::<proc_macro2::TokenStream>;
#[cfg(feature = "functions")]
let function_impls =
crate::impls::impl_function_traits(reflect_struct.meta(), &where_clause_options);

let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);

Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_reflect/derive/src/impls/tuple_structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
use crate::impls::{impl_type_path, impl_typed};
use crate::ReflectStruct;
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult};
use quote::{quote, ToTokens};
Expand Down Expand Up @@ -46,7 +46,11 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::

let type_path_impl = impl_type_path(reflect_struct.meta());

let function_impls = impl_function_traits(reflect_struct.meta(), &where_clause_options);
#[cfg(not(feature = "functions"))]
let function_impls = None::<proc_macro2::TokenStream>;
#[cfg(feature = "functions")]
let function_impls =
crate::impls::impl_function_traits(reflect_struct.meta(), &where_clause_options);

let (impl_generics, ty_generics, where_clause) = reflect_struct
.meta()
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_reflect/derive/src/impls/values.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
use crate::impls::{impl_type_path, impl_typed};
use crate::utility::WhereClauseOptions;
use crate::ReflectMeta;
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQClone, FQOption, FQResult};
Expand Down Expand Up @@ -33,7 +33,10 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {

let type_path_impl = impl_type_path(meta);

let function_impls = impl_function_traits(meta, &where_clause_options);
#[cfg(not(feature = "functions"))]
let function_impls = None::<proc_macro2::TokenStream>;
#[cfg(feature = "functions")]
let function_impls = crate::impls::impl_function_traits(meta, &where_clause_options);

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);
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::func::macros::impl_function_traits;
use crate::{
self as bevy_reflect, utility::reflect_hasher, ApplyError, Reflect, ReflectKind, ReflectMut,
ReflectOwned, ReflectRef, TypeInfo, TypePath, TypePathTable,
Expand Down Expand Up @@ -359,7 +358,9 @@ impl Array for DynamicArray {
}

impl_type_path!((in bevy_reflect) DynamicArray);
impl_function_traits!(DynamicArray);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(DynamicArray);

/// An iterator over an [`Array`].
pub struct ArrayIter<'a> {
array: &'a dyn Array,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/enums/dynamic_enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bevy_reflect_derive::impl_type_path;

use crate::func::macros::impl_function_traits;
use crate::{
self as bevy_reflect, enum_debug, enum_hash, enum_partial_eq, ApplyError, DynamicStruct,
DynamicTuple, Enum, Reflect, ReflectKind, ReflectMut, ReflectOwned, ReflectRef, Struct, Tuple,
Expand Down Expand Up @@ -428,4 +427,5 @@ impl Reflect for DynamicEnum {
}

impl_type_path!((in bevy_reflect) DynamicEnum);
impl_function_traits!(DynamicEnum);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(DynamicEnum);
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use smallvec::{Array as SmallArray, SmallVec};

use std::any::Any;

use crate::func::macros::impl_function_traits;
use crate::utility::GenericTypeInfoCell;
use crate::{
self as bevy_reflect, ApplyError, FromReflect, FromType, GetTypeRegistration, List, ListInfo,
Expand Down Expand Up @@ -188,4 +187,5 @@ where
}
}

impl_function_traits!(SmallVec<T>; <T: SmallArray + TypePath + Send + Sync> where T::Item: FromReflect + TypePath);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(SmallVec<T>; <T: SmallArray + TypePath + Send + Sync> where T::Item: FromReflect + TypePath);
34 changes: 22 additions & 12 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::func::macros::impl_function_traits;
use crate::std_traits::ReflectDefault;
use crate::utility::{
reflect_hasher, GenericTypeInfoCell, GenericTypePathCell, NonGenericTypeInfoCell,
Expand Down Expand Up @@ -398,7 +397,8 @@ impl_reflect_for_veclike!(
Vec::pop,
[T]
);
impl_function_traits!(Vec<T>; <T: FromReflect + TypePath + GetTypeRegistration>);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(Vec<T>; <T: FromReflect + TypePath + GetTypeRegistration>);

impl_reflect_for_veclike!(
::alloc::collections::VecDeque<T>,
Expand All @@ -408,7 +408,8 @@ impl_reflect_for_veclike!(
VecDeque::pop_back,
VecDeque::<T>
);
impl_function_traits!(VecDeque<T>; <T: FromReflect + TypePath + GetTypeRegistration>);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(VecDeque<T>; <T: FromReflect + TypePath + GetTypeRegistration>);

macro_rules! impl_reflect_for_hashmap {
($ty:path) => {
Expand Down Expand Up @@ -638,7 +639,8 @@ macro_rules! impl_reflect_for_hashmap {
impl_reflect_for_hashmap!(::std::collections::HashMap<K, V, S>);
impl_type_path!(::std::collections::hash_map::RandomState);
impl_type_path!(::std::collections::HashMap<K, V, S>);
impl_function_traits!(::std::collections::HashMap<K, V, S>;
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(::std::collections::HashMap<K, V, S>;
<
K: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
V: FromReflect + TypePath + GetTypeRegistration,
Expand All @@ -649,7 +651,8 @@ impl_function_traits!(::std::collections::HashMap<K, V, S>;
impl_reflect_for_hashmap!(bevy_utils::hashbrown::HashMap<K, V, S>);
impl_type_path!(::bevy_utils::hashbrown::hash_map::DefaultHashBuilder);
impl_type_path!(::bevy_utils::hashbrown::HashMap<K, V, S>);
impl_function_traits!(::bevy_utils::hashbrown::HashMap<K, V, S>;
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(::bevy_utils::hashbrown::HashMap<K, V, S>;
<
K: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
V: FromReflect + TypePath + GetTypeRegistration,
Expand Down Expand Up @@ -869,7 +872,8 @@ where
}

impl_type_path!(::std::collections::BTreeMap<K, V>);
impl_function_traits!(::std::collections::BTreeMap<K, V>;
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(::std::collections::BTreeMap<K, V>;
<
K: FromReflect + TypePath + GetTypeRegistration + Eq + Ord,
V: FromReflect + TypePath + GetTypeRegistration
Expand Down Expand Up @@ -1035,7 +1039,8 @@ impl<T: Reflect + TypePath + GetTypeRegistration, const N: usize> GetTypeRegistr
}
}

impl_function_traits!([T; N]; <T: Reflect + TypePath + GetTypeRegistration> [const N: usize]);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!([T; N]; <T: Reflect + TypePath + GetTypeRegistration> [const N: usize]);

impl_reflect! {
#[type_path = "core::option"]
Expand Down Expand Up @@ -1194,7 +1199,8 @@ impl FromReflect for Cow<'static, str> {
}
}

impl_function_traits!(Cow<'static, str>);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(Cow<'static, str>);

impl<T: TypePath> TypePath for [T]
where
Expand Down Expand Up @@ -1374,7 +1380,8 @@ impl<T: FromReflect + Clone + TypePath + GetTypeRegistration> FromReflect for Co
}
}

impl_function_traits!(Cow<'static, [T]>; <T: FromReflect + Clone + TypePath + GetTypeRegistration>);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(Cow<'static, [T]>; <T: FromReflect + Clone + TypePath + GetTypeRegistration>);

impl Reflect for &'static str {
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
Expand Down Expand Up @@ -1482,7 +1489,8 @@ impl FromReflect for &'static str {
}
}

impl_function_traits!(&'static str);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(&'static str);

impl Reflect for &'static Path {
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
Expand Down Expand Up @@ -1589,7 +1597,8 @@ impl FromReflect for &'static Path {
}
}

impl_function_traits!(&'static Path);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(&'static Path);

impl Reflect for Cow<'static, Path> {
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
Expand Down Expand Up @@ -1706,7 +1715,8 @@ impl GetTypeRegistration for Cow<'static, Path> {
}
}

impl_function_traits!(Cow<'static, Path>);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(Cow<'static, Path>);

#[cfg(test)]
mod tests {
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
mod array;
mod fields;
mod from_reflect;
#[cfg(feature = "functions")]
pub mod func;
mod list;
mod map;
Expand Down Expand Up @@ -517,6 +518,9 @@ pub mod prelude {
ReflectDeserialize, ReflectFromReflect, ReflectPath, ReflectSerialize, Struct, TupleStruct,
TypePath,
};

#[cfg(feature = "functions")]
pub use crate::func::IntoFunction;
Comment on lines +522 to +523
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I was at it, I also included a prelude export for IntoFunction because I feel it would be useful to not have to import it every time. If we don't want to include it in the prelude, though, let me know and I can revert this!

}

pub use array::*;
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::hash::{Hash, Hasher};

use bevy_reflect_derive::impl_type_path;

use crate::func::macros::impl_function_traits;
use crate::utility::reflect_hasher;
use crate::{
self as bevy_reflect, ApplyError, FromReflect, Reflect, ReflectKind, ReflectMut, ReflectOwned,
Expand Down Expand Up @@ -370,7 +369,8 @@ impl Reflect for DynamicList {
}

impl_type_path!((in bevy_reflect) DynamicList);
impl_function_traits!(DynamicList);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(DynamicList);

impl Debug for DynamicList {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::fmt::{Debug, Formatter};
use bevy_reflect_derive::impl_type_path;
use bevy_utils::{Entry, HashMap};

use crate::func::macros::impl_function_traits;
use crate::{
self as bevy_reflect, ApplyError, Reflect, ReflectKind, ReflectMut, ReflectOwned, ReflectRef,
TypeInfo, TypePath, TypePathTable,
Expand Down Expand Up @@ -418,7 +417,8 @@ impl Reflect for DynamicMap {
}

impl_type_path!((in bevy_reflect) DynamicMap);
impl_function_traits!(DynamicMap);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(DynamicMap);

impl Debug for DynamicMap {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/src/struct_trait.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::attributes::{impl_custom_attribute_methods, CustomAttributes};
use crate::func::macros::impl_function_traits;
use crate::{
self as bevy_reflect, ApplyError, NamedField, Reflect, ReflectKind, ReflectMut, ReflectOwned,
ReflectRef, TypeInfo, TypePath, TypePathTable,
Expand Down Expand Up @@ -500,7 +499,8 @@ impl Reflect for DynamicStruct {
}

impl_type_path!((in bevy_reflect) DynamicStruct);
impl_function_traits!(DynamicStruct);
#[cfg(feature = "functions")]
crate::func::macros::impl_function_traits!(DynamicStruct);

impl Debug for DynamicStruct {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down
Loading