Skip to content

Commit 99c9218

Browse files
authored
bevy_reflect: Feature-gate function reflection (#14174)
# Objective Function reflection requires a lot of macro code generation in the form of several `all_tuples!` invocations, as well as impls generated in the `Reflect` derive macro. Seeing as function reflection is currently a bit more niche, it makes sense to gate it all behind a feature. ## Solution Add a `functions` feature to `bevy_reflect`, which can be enabled in Bevy using the `reflect_functions` feature. ## Testing You can test locally by running: ``` cargo test --package bevy_reflect ``` That should ensure that everything still works with the feature disabled. To test with the feature on, you can run: ``` cargo test --package bevy_reflect --features functions ``` --- ## Changelog - Moved function reflection behind a Cargo feature (`bevy/reflect_functions` and `bevy_reflect/functions`) - Add `IntoFunction` export in `bevy_reflect::prelude` ## Internal Migration Guide > [!important] > Function reflection was introduced as part of the 0.15 dev cycle. This migration guide was written for developers relying on `main` during this cycle, and is not a breaking change coming from 0.14. Function reflection is now gated behind a feature. To use function reflection, enable the feature: - If using `bevy_reflect` directly, enable the `functions` feature - If using `bevy`, enable the `reflect_functions` feature
1 parent 57d0592 commit 99c9218

File tree

21 files changed

+89
-42
lines changed

21 files changed

+89
-42
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ ios_simulator = ["bevy_internal/ios_simulator"]
348348
# Enable built in global state machines
349349
bevy_state = ["bevy_internal/bevy_state"]
350350

351+
# Enable function reflection
352+
reflect_functions = ["bevy_internal/reflect_functions"]
353+
351354
[dependencies]
352355
bevy_internal = { path = "crates/bevy_internal", version = "0.15.0-dev", default-features = false }
353356

@@ -2158,6 +2161,7 @@ wasm = false
21582161
name = "function_reflection"
21592162
path = "examples/reflection/function_reflection.rs"
21602163
doc-scrape-examples = true
2164+
required-features = ["reflect_functions"]
21612165

21622166
[package.metadata.example.function_reflection]
21632167
name = "Function Reflection"

crates/bevy_internal/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ ios_simulator = ["bevy_pbr?/ios_simulator", "bevy_render?/ios_simulator"]
195195
# Enable built in global state machines
196196
bevy_state = ["dep:bevy_state"]
197197

198+
# Enable function reflection
199+
reflect_functions = ["bevy_reflect/functions"]
200+
198201
[dependencies]
199202
# bevy
200203
bevy_a11y = { path = "../bevy_a11y", version = "0.15.0-dev" }

crates/bevy_reflect/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ smallvec = ["dep:smallvec"]
1919
uuid = ["dep:uuid"]
2020
# When enabled, allows documentation comments to be accessed via reflection
2121
documentation = ["bevy_reflect_derive/documentation"]
22+
# Enables function reflection
23+
functions = ["bevy_reflect_derive/functions"]
2224

2325
[dependencies]
2426
# bevy

crates/bevy_reflect/compile_fail/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
88
publish = false
99

1010
[dependencies]
11-
bevy_reflect = { path = "../" }
11+
bevy_reflect = { path = "../", features = ["functions"] }
1212

1313
[dev-dependencies]
1414
compile_fail_utils = { path = "../../../tools/compile_fail_utils" }

crates/bevy_reflect/derive/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ proc-macro = true
1515
default = []
1616
# When enabled, allows documentation comments to be processed by the reflection macros
1717
documentation = []
18+
# Enables macro logic related to function reflection
19+
functions = []
1820

1921
[dependencies]
2022
bevy_macro_utils = { path = "../../bevy_macro_utils", version = "0.15.0-dev" }

crates/bevy_reflect/derive/src/impls/enums.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::derive_data::{EnumVariantFields, ReflectEnum, StructField};
22
use crate::enum_utility::{EnumVariantOutputData, TryApplyVariantBuilder, VariantBuilder};
3-
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
3+
use crate::impls::{impl_type_path, impl_typed};
44
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQOption, FQResult};
55
use proc_macro2::{Ident, Span};
66
use quote::quote;
@@ -65,7 +65,11 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream
6565

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

68-
let function_impls = impl_function_traits(reflect_enum.meta(), &where_clause_options);
68+
#[cfg(not(feature = "functions"))]
69+
let function_impls = None::<proc_macro2::TokenStream>;
70+
#[cfg(feature = "functions")]
71+
let function_impls =
72+
crate::impls::impl_function_traits(reflect_enum.meta(), &where_clause_options);
6973

7074
let get_type_registration_impl = reflect_enum.get_type_registration(&where_clause_options);
7175

crates/bevy_reflect/derive/src/impls/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
mod enums;
2+
#[cfg(feature = "functions")]
23
mod func;
34
mod structs;
45
mod tuple_structs;
56
mod typed;
67
mod values;
78

89
pub(crate) use enums::impl_enum;
10+
#[cfg(feature = "functions")]
911
pub(crate) use func::impl_function_traits;
1012
pub(crate) use structs::impl_struct;
1113
pub(crate) use tuple_structs::impl_tuple_struct;

crates/bevy_reflect/derive/src/impls/structs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
1+
use crate::impls::{impl_type_path, impl_typed};
22
use crate::utility::ident_or_index;
33
use crate::ReflectStruct;
44
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult};
@@ -54,7 +54,11 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS
5454

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

57-
let function_impls = impl_function_traits(reflect_struct.meta(), &where_clause_options);
57+
#[cfg(not(feature = "functions"))]
58+
let function_impls = None::<proc_macro2::TokenStream>;
59+
#[cfg(feature = "functions")]
60+
let function_impls =
61+
crate::impls::impl_function_traits(reflect_struct.meta(), &where_clause_options);
5862

5963
let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);
6064

crates/bevy_reflect/derive/src/impls/tuple_structs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
1+
use crate::impls::{impl_type_path, impl_typed};
22
use crate::ReflectStruct;
33
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult};
44
use quote::{quote, ToTokens};
@@ -46,7 +46,11 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::
4646

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

49-
let function_impls = impl_function_traits(reflect_struct.meta(), &where_clause_options);
49+
#[cfg(not(feature = "functions"))]
50+
let function_impls = None::<proc_macro2::TokenStream>;
51+
#[cfg(feature = "functions")]
52+
let function_impls =
53+
crate::impls::impl_function_traits(reflect_struct.meta(), &where_clause_options);
5054

5155
let (impl_generics, ty_generics, where_clause) = reflect_struct
5256
.meta()

crates/bevy_reflect/derive/src/impls/values.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::impls::{impl_function_traits, impl_type_path, impl_typed};
1+
use crate::impls::{impl_type_path, impl_typed};
22
use crate::utility::WhereClauseOptions;
33
use crate::ReflectMeta;
44
use bevy_macro_utils::fq_std::{FQAny, FQBox, FQClone, FQOption, FQResult};
@@ -33,7 +33,10 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {
3333

3434
let type_path_impl = impl_type_path(meta);
3535

36-
let function_impls = impl_function_traits(meta, &where_clause_options);
36+
#[cfg(not(feature = "functions"))]
37+
let function_impls = None::<proc_macro2::TokenStream>;
38+
#[cfg(feature = "functions")]
39+
let function_impls = crate::impls::impl_function_traits(meta, &where_clause_options);
3740

3841
let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();
3942
let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);

0 commit comments

Comments
 (0)