diff --git a/crates/bevy_a11y/Cargo.toml b/crates/bevy_a11y/Cargo.toml index 39628ec046117..481d3a33d9255 100644 --- a/crates/bevy_a11y/Cargo.toml +++ b/crates/bevy_a11y/Cargo.toml @@ -28,15 +28,11 @@ serialize = ["dep:serde", "bevy_ecs/serialize", "accesskit/serde"] ## Allows access to the `std` crate. Enabling this feature will prevent compilation ## on `no_std` targets, but provides access to certain additional features on ## supported platforms. -std = ["bevy_app/std", "bevy_ecs/std", "bevy_reflect/std"] +std = ["bevy_app/std", "bevy_ecs/std"] ## `critical-section` provides the building blocks for synchronization primitives ## on all platforms, including `no_std`. -critical-section = [ - "bevy_app/critical-section", - "bevy_ecs/critical-section", - "bevy_reflect?/critical-section", -] +critical-section = ["bevy_app/critical-section", "bevy_ecs/critical-section"] [dependencies] # bevy diff --git a/crates/bevy_app/Cargo.toml b/crates/bevy_app/Cargo.toml index 6b6120f182a86..6a636464b0d91 100644 --- a/crates/bevy_app/Cargo.toml +++ b/crates/bevy_app/Cargo.toml @@ -43,7 +43,6 @@ error_panic_hook = [] ## on `no_std` targets, but provides access to certain additional features on ## supported platforms. std = [ - "bevy_reflect?/std", "bevy_ecs/std", "dep:ctrlc", "downcast-rs/std", @@ -57,7 +56,6 @@ critical-section = [ "bevy_tasks/critical-section", "bevy_ecs/critical-section", "bevy_platform/critical-section", - "bevy_reflect?/critical-section", ] ## Enables use of browser APIs. @@ -65,7 +63,6 @@ critical-section = [ web = [ "bevy_platform/web", "bevy_tasks/web", - "bevy_reflect?/web", "dep:wasm-bindgen", "dep:web-sys", "dep:console_error_panic_hook", diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index cbb138b0f5d17..7b6bde7161c14 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -84,9 +84,6 @@ bevy_app = { path = "../bevy_app", version = "0.16.0-dev", default-features = fa bevy_tasks = { path = "../bevy_tasks", version = "0.16.0-dev", default-features = false, features = [ "web", ] } -bevy_reflect = { path = "../bevy_reflect", version = "0.16.0-dev", default-features = false, features = [ - "web", -] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] notify-debouncer-full = { version = "0.5.0", default-features = false, optional = true } diff --git a/crates/bevy_audio/Cargo.toml b/crates/bevy_audio/Cargo.toml index aff7f83b37b33..79c9b6e79c000 100644 --- a/crates/bevy_audio/Cargo.toml +++ b/crates/bevy_audio/Cargo.toml @@ -33,9 +33,6 @@ rodio = { version = "0.20", default-features = false, features = [ bevy_app = { path = "../bevy_app", version = "0.16.0-dev", default-features = false, features = [ "web", ] } -bevy_reflect = { path = "../bevy_reflect", version = "0.16.0-dev", default-features = false, features = [ - "web", -] } [features] mp3 = ["rodio/mp3"] diff --git a/crates/bevy_color/Cargo.toml b/crates/bevy_color/Cargo.toml index ca7a7a74f5deb..ea27409cf3ed2 100644 --- a/crates/bevy_color/Cargo.toml +++ b/crates/bevy_color/Cargo.toml @@ -25,20 +25,14 @@ encase = { version = "0.10", default-features = false, optional = true } [features] default = ["std", "bevy_reflect", "encase"] -std = [ - "alloc", - "bevy_math/std", - "serde?/std", - "wgpu-types?/std", - "bevy_reflect?/std", -] +std = ["alloc", "bevy_math/std", "serde?/std", "wgpu-types?/std"] alloc = ["bevy_math/alloc", "serde?/alloc"] serialize = ["serde", "bevy_math/serialize"] bevy_reflect = ["dep:bevy_reflect"] wgpu-types = ["dep:wgpu-types"] encase = ["dep:encase", "std"] libm = ["bevy_math/libm"] -critical-section = ["bevy_reflect?/critical-section"] +critical-section = [] [lints] workspace = true diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index 27498f58bc25c..49a8520ff7fad 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -60,7 +60,6 @@ async_executor = ["std", "bevy_tasks/async_executor"] ## on `no_std` targets, but provides access to certain additional features on ## supported platforms. std = [ - "bevy_reflect?/std", "bevy_tasks/std", "bevy_utils/parallel", "bitflags/std", @@ -80,7 +79,6 @@ std = [ critical-section = [ "bevy_tasks/critical-section", "bevy_platform/critical-section", - "bevy_reflect?/critical-section", ] hotpatching = ["dep:subsecond"] diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index 6b805b83bf387..32a55594b23e5 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -38,20 +38,13 @@ smol_str = ["dep:smol_str", "bevy_reflect/smol_str"] ## Allows access to the `std` crate. Enabling this feature will prevent compilation ## on `no_std` targets, but provides access to certain additional features on ## supported platforms. -std = [ - "bevy_app/std", - "bevy_ecs/std", - "bevy_math/std", - "bevy_reflect/std", - "bevy_platform/std", -] +std = ["bevy_app/std", "bevy_ecs/std", "bevy_math/std", "bevy_platform/std"] ## `critical-section` provides the building blocks for synchronization primitives ## on all platforms, including `no_std`. critical-section = [ "bevy_app/critical-section", "bevy_ecs/critical-section", - "bevy_reflect?/critical-section", "bevy_platform/critical-section", ] diff --git a/crates/bevy_input_focus/Cargo.toml b/crates/bevy_input_focus/Cargo.toml index e7ff3f6fe8c63..eb4578c570143 100644 --- a/crates/bevy_input_focus/Cargo.toml +++ b/crates/bevy_input_focus/Cargo.toml @@ -41,7 +41,6 @@ std = [ "bevy_app/std", "bevy_ecs/std", "bevy_math/std", - "bevy_reflect/std", "bevy_input/std", "bevy_window/std", ] @@ -51,7 +50,6 @@ std = [ critical-section = [ "bevy_app/critical-section", "bevy_ecs/critical-section", - "bevy_reflect?/critical-section", "bevy_input/critical-section", ] diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index 9f78fc009dcb1..87199bc02bcd3 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -292,7 +292,6 @@ std = [ "bevy_input_focus?/std", "bevy_math/std", "bevy_platform/std", - "bevy_reflect/std", "bevy_state?/std", "bevy_time/std", "bevy_transform/std", @@ -310,7 +309,6 @@ critical-section = [ "bevy_input/critical-section", "bevy_input_focus?/critical-section", "bevy_platform/critical-section", - "bevy_reflect/critical-section", "bevy_state?/critical-section", "bevy_time/critical-section", "bevy_tasks/critical-section", @@ -337,12 +335,7 @@ async_executor = [ # Enables use of browser APIs. # Note this is currently only applicable on `wasm32` architectures. -web = [ - "bevy_app/web", - "bevy_platform/web", - "bevy_reflect/web", - "bevy_tasks/web", -] +web = ["bevy_app/web", "bevy_platform/web", "bevy_tasks/web"] hotpatching = ["bevy_app/hotpatching", "bevy_ecs/hotpatching"] diff --git a/crates/bevy_math/Cargo.toml b/crates/bevy_math/Cargo.toml index c420c3fe3ce2d..8797c7e3ad663 100644 --- a/crates/bevy_math/Cargo.toml +++ b/crates/bevy_math/Cargo.toml @@ -50,7 +50,6 @@ std = [ "approx?/std", "rand?/std", "rand_distr?/std", - "bevy_reflect?/std", ] alloc = [ "itertools/use_alloc", diff --git a/crates/bevy_reflect/Cargo.toml b/crates/bevy_reflect/Cargo.toml index 8827fc695bc72..d7b0c01a8ea39 100644 --- a/crates/bevy_reflect/Cargo.toml +++ b/crates/bevy_reflect/Cargo.toml @@ -10,7 +10,7 @@ keywords = ["bevy"] rust-version = "1.85.0" [features] -default = ["std", "smallvec", "debug"] +default = ["smallvec", "debug"] # Features @@ -26,7 +26,7 @@ functions = ["bevy_reflect_derive/functions"] debug = ["debug_stack"] ## When enabled, keeps track of the current serialization/deserialization context for better error messages -debug_stack = ["std"] +debug_stack = ["bevy_platform/std"] # Integrations @@ -37,7 +37,7 @@ glam = ["dep:glam"] hashbrown = ["dep:hashbrown"] ## Adds reflection support to `petgraph` types. -petgraph = ["dep:petgraph", "std"] +petgraph = ["dep:petgraph"] ## Adds reflection support to `smallvec` types. smallvec = ["dep:smallvec"] @@ -48,29 +48,8 @@ uuid = ["dep:uuid"] ## Adds reflection support to `wgpu-types` types. wgpu-types = ["dep:wgpu-types"] -# Platform Compatibility - -## Allows access to the `std` crate. Enabling this feature will prevent compilation -## on `no_std` targets, but provides access to certain additional features on -## supported platforms. -std = [ - "erased-serde/std", - "downcast-rs/std", - "serde/std", - "glam?/std", - "smol_str?/std", - "uuid?/std", - "bevy_platform/std", - "wgpu-types?/std", -] - -## `critical-section` provides the building blocks for synchronization primitives -## on all platforms, including `no_std`. -critical-section = ["bevy_platform/critical-section"] - -## Enables use of browser APIs. -## Note this is currently only applicable on `wasm32` architectures. -web = ["bevy_platform/web", "uuid?/js"] +## Adds reflection support to `smol_str` types. +smol_str = ["dep:smol_str"] [dependencies] # bevy @@ -99,13 +78,15 @@ assert_type_match = "0.1.1" smallvec = { version = "1.11", default-features = false, optional = true } glam = { version = "0.29.3", default-features = false, features = [ "serde", + "nostd-libm", ], optional = true } -petgraph = { version = "0.7", features = ["serde-1"], optional = true } +petgraph = { version = "0.7", features = [ + "serde-1", +], default-features = false, optional = true } smol_str = { version = "0.2.0", default-features = false, features = [ "serde", ], optional = true } uuid = { version = "1.13.1", default-features = false, optional = true, features = [ - "v4", "serde", ] } variadics_please = "1.1" diff --git a/crates/bevy_reflect/src/impls/core/sync.rs b/crates/bevy_reflect/src/impls/core/sync.rs index b4fe8977d5308..d8e457406733e 100644 --- a/crates/bevy_reflect/src/impls/core/sync.rs +++ b/crates/bevy_reflect/src/impls/core/sync.rs @@ -18,8 +18,9 @@ macro_rules! impl_reflect_for_atomic { impl_type_path!($ty); const _: () = { - #[cfg(feature = "functions")] - crate::func::macros::impl_function_traits!($ty); + crate::cfg::functions! { + crate::func::macros::impl_function_traits!($ty); + } impl GetTypeRegistration for $ty where @@ -32,8 +33,7 @@ macro_rules! impl_reflect_for_atomic { registration.insert::(FromType::::from_type()); // Serde only supports atomic types when the "std" feature is enabled - #[cfg(feature = "std")] - { + crate::cfg::std! { registration.insert::(FromType::::from_type()); registration.insert::(FromType::::from_type()); } diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 0f399afd59eee..0d945b756065b 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -559,8 +559,73 @@ #![no_std] -#[cfg(feature = "std")] -extern crate std; +/// Configuration information for this crate. +pub mod cfg { + pub(crate) use bevy_platform::cfg::*; + + pub use bevy_platform::cfg::std; + + define_alias! { + #[cfg(feature = "documentation")] => { + /// When enabled, allows documentation comments to be accessed via reflection + documentation + } + + #[cfg(feature = "functions")] => { + /// Enables function reflection + functions + } + + #[cfg(feature = "debug")] => { + /// Enables features useful for debugging reflection + debug + } + + #[cfg(feature = "debug_stack")] => { + /// When enabled, keeps track of the current serialization/deserialization context for better error messages + debug_stack + } + + #[cfg(feature = "glam")] => { + /// Adds reflection support to `glam` types. + glam + } + + #[cfg(feature = "hashbrown")] => { + /// Adds reflection support to `hashbrown` types. + hashbrown + } + + #[cfg(feature = "petgraph")] => { + /// Adds reflection support to `petgraph` types. + petgraph + } + + #[cfg(feature = "smallvec")] => { + /// Adds reflection support to `smallvec` types. + smallvec + } + + #[cfg(feature = "uuid")] => { + /// Adds reflection support to `uuid` types. + uuid + } + + #[cfg(feature = "wgpu-types")] => { + /// Adds reflection support to `wgpu-types` types. + wgpu_types + } + + #[cfg(feature = "smol_str")] => { + /// Adds reflection support to `smol_str` types. + smol_str + } + } +} + +cfg::std! { + extern crate std; +} extern crate alloc; @@ -593,24 +658,39 @@ mod impls { mod bevy_platform; mod core; mod foldhash; - #[cfg(feature = "hashbrown")] - mod hashbrown; mod macros; - #[cfg(feature = "std")] - mod std; - - #[cfg(feature = "glam")] - mod glam; - #[cfg(feature = "petgraph")] - mod petgraph; - #[cfg(feature = "smallvec")] - mod smallvec; - #[cfg(feature = "smol_str")] - mod smol_str; - #[cfg(feature = "uuid")] - mod uuid; - #[cfg(feature = "wgpu-types")] - mod wgpu_types; + + crate::cfg::std! { + mod std; + } + + crate::cfg::hashbrown! { + mod hashbrown; + } + + crate::cfg::glam! { + mod glam; + } + + crate::cfg::petgraph! { + mod petgraph; + } + + crate::cfg::smallvec! { + mod smallvec; + } + + crate::cfg::smol_str! { + mod smol_str; + } + + crate::cfg::uuid! { + mod uuid; + } + + crate::cfg::wgpu_types! { + mod wgpu_types; + } } pub mod attributes; @@ -2145,8 +2225,7 @@ mod tests { assert!(info.is::()); // List (SmallVec) - #[cfg(feature = "smallvec")] - { + crate::cfg::smallvec! { type MySmallVec = smallvec::SmallVec<[String; 2]>; let info = MySmallVec::type_info().as_list().unwrap(); @@ -2160,7 +2239,7 @@ mod tests { let value: &dyn Reflect = &value; let info = value.reflect_type_info(); assert!(info.is::()); - } + }; // Array type MyArray = [usize; 3]; @@ -2306,10 +2385,7 @@ mod tests { dynamic_array.set_represented_type(Some(type_info)); } - #[cfg(feature = "documentation")] - mod docstrings { - use super::*; - + crate::cfg::documentation! { #[test] fn should_not_contain_docs() { // Regular comments do not count as doc comments, @@ -3366,9 +3442,7 @@ bevy_reflect::tests::Test { ); } - #[cfg(feature = "glam")] - mod glam { - use super::*; + crate::cfg::glam! { use ::glam::{quat, vec3, Quat, Vec3}; #[test] diff --git a/crates/bevy_reflect/src/serde/de/error_utils.rs b/crates/bevy_reflect/src/serde/de/error_utils.rs index 58adcfe920cdd..91af65d235128 100644 --- a/crates/bevy_reflect/src/serde/de/error_utils.rs +++ b/crates/bevy_reflect/src/serde/de/error_utils.rs @@ -1,17 +1,17 @@ use core::fmt::Display; use serde::de::Error; -#[cfg(feature = "debug_stack")] -use std::thread_local; +crate::cfg::debug_stack! { + use std::thread_local; -#[cfg(feature = "debug_stack")] -thread_local! { - /// The thread-local [`TypeInfoStack`] used for debugging. - /// - /// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack - pub(super) static TYPE_INFO_STACK: core::cell::RefCell = const { core::cell::RefCell::new( - crate::type_info_stack::TypeInfoStack::new() - ) }; + thread_local! { + /// The thread-local [`TypeInfoStack`] used for debugging. + /// + /// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack + pub(super) static TYPE_INFO_STACK: core::cell::RefCell = const { core::cell::RefCell::new( + crate::type_info_stack::TypeInfoStack::new() + ) }; + } } /// A helper function for generating a custom deserialization error message. @@ -21,9 +21,12 @@ thread_local! { /// /// [type info stack]: crate::type_info_stack::TypeInfoStack pub(super) fn make_custom_error(msg: impl Display) -> E { - #[cfg(feature = "debug_stack")] - return TYPE_INFO_STACK - .with_borrow(|stack| E::custom(format_args!("{msg} (stack: {stack:?})"))); - #[cfg(not(feature = "debug_stack"))] - return E::custom(msg); + crate::cfg::switch! { + crate::cfg::debug_stack => { + TYPE_INFO_STACK.with_borrow(|stack| E::custom(format_args!("{msg} (stack: {stack:?})"))) + } + _ => { + E::custom(msg) + } + } } diff --git a/crates/bevy_reflect/src/serde/de/mod.rs b/crates/bevy_reflect/src/serde/de/mod.rs index e82b60bcee5c8..2a8c9a1537b40 100644 --- a/crates/bevy_reflect/src/serde/de/mod.rs +++ b/crates/bevy_reflect/src/serde/de/mod.rs @@ -519,10 +519,17 @@ mod tests { let error = reflect_deserializer .deserialize(&mut deserializer) .unwrap_err(); - #[cfg(feature = "debug_stack")] - assert_eq!(error, ron::Error::Message("type `core::ops::RangeInclusive` did not register the `ReflectDeserialize` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `core::ops::RangeInclusive`)".to_string())); - #[cfg(not(feature = "debug_stack"))] - assert_eq!(error, ron::Error::Message("type `core::ops::RangeInclusive` did not register the `ReflectDeserialize` type data. For certain types, this may need to be registered manually using `register_type_data`".to_string())); + + let expected_error = crate::cfg::switch! {{ + crate::cfg::debug_stack => { + "type `core::ops::RangeInclusive` did not register the `ReflectDeserialize` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `core::ops::RangeInclusive`)" + } + _ => { + "type `core::ops::RangeInclusive` did not register the `ReflectDeserialize` type data. For certain types, this may need to be registered manually using `register_type_data`" + } + }}; + + assert_eq!(error, ron::Error::Message(expected_error.to_string())); } #[test] @@ -664,16 +671,16 @@ mod tests { .deserialize(&mut deserializer) .unwrap_err(); - #[cfg(feature = "debug_stack")] - assert_eq!( - error, - ron::Error::Message("my custom deserialize error (stack: `i32`)".to_string()) - ); - #[cfg(not(feature = "debug_stack"))] - assert_eq!( - error, - ron::Error::Message("my custom deserialize error".to_string()) - ); + let expected_error = crate::cfg::switch! {{ + crate::cfg::debug_stack => { + "my custom deserialize error (stack: `i32`)" + } + _ => { + "my custom deserialize error" + } + }}; + + assert_eq!(error, ron::Error::Message(expected_error.to_string())); } #[test] @@ -762,13 +769,11 @@ mod tests { assert!(::from_reflect(dynamic_output.as_partial_reflect()).is_none()); } - #[cfg(feature = "functions")] - mod functions { - use super::*; - use crate::func::DynamicFunction; - + crate::cfg::functions! { #[test] fn should_not_deserialize_function() { + use crate::func::DynamicFunction; + #[derive(Reflect)] #[reflect(from_reflect = false)] struct MyStruct { @@ -779,7 +784,7 @@ mod tests { registry.register::(); let input = r#"{ - "bevy_reflect::serde::de::tests::functions::MyStruct": ( + "bevy_reflect::serde::de::tests::MyStruct": ( func: (), ), }"#; @@ -791,29 +796,20 @@ mod tests { .deserialize(&mut ron_deserializer) .unwrap_err(); - #[cfg(feature = "debug_stack")] - assert_eq!( - error, - ron::Error::Message( - "no registration found for type `bevy_reflect::DynamicFunction` (stack: `bevy_reflect::serde::de::tests::functions::MyStruct`)" - .to_string() - ) - ); + let expected_error = crate::cfg::switch! {{ + crate::cfg::debug_stack => { + "no registration found for type `bevy_reflect::DynamicFunction` (stack: `bevy_reflect::serde::de::tests::MyStruct`)" + } + _ => { + "no registration found for type `bevy_reflect::DynamicFunction`" + } + }}; - #[cfg(not(feature = "debug_stack"))] - assert_eq!( - error, - ron::Error::Message( - "no registration found for type `bevy_reflect::DynamicFunction`".to_string() - ) - ); + assert_eq!(error, ron::Error::Message(expected_error.to_string())); } } - #[cfg(feature = "debug_stack")] - mod debug_stack { - use super::*; - + crate::cfg::debug_stack! { #[test] fn should_report_context_in_errors() { #[derive(Reflect)] @@ -838,7 +834,7 @@ mod tests { registry.register::(); registry.register::>(); - let input = r#"{"bevy_reflect::serde::de::tests::debug_stack::Foo":(bar:(some_other_field:Some(123),baz:(value:[(start:0.0,end:1.0)])))}"#; + let input = r#"{"bevy_reflect::serde::de::tests::Foo":(bar:(some_other_field:Some(123),baz:(value:[(start:0.0,end:1.0)])))}"#; let mut deserializer = ron::de::Deserializer::from_str(input).unwrap(); let reflect_deserializer = ReflectDeserializer::new(®istry); let error = reflect_deserializer @@ -847,7 +843,7 @@ mod tests { assert_eq!( error, ron::Error::Message( - "type `core::ops::RangeInclusive` did not register the `ReflectDeserialize` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `bevy_reflect::serde::de::tests::debug_stack::Foo` -> `bevy_reflect::serde::de::tests::debug_stack::Bar` -> `bevy_reflect::serde::de::tests::debug_stack::Baz` -> `alloc::vec::Vec>` -> `core::ops::RangeInclusive`)".to_string() + "type `core::ops::RangeInclusive` did not register the `ReflectDeserialize` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `bevy_reflect::serde::de::tests::Foo` -> `bevy_reflect::serde::de::tests::Bar` -> `bevy_reflect::serde::de::tests::Baz` -> `alloc::vec::Vec>` -> `core::ops::RangeInclusive`)".to_string() ) ); } diff --git a/crates/bevy_reflect/src/serde/ser/error_utils.rs b/crates/bevy_reflect/src/serde/ser/error_utils.rs index 8f38a0742a7d0..337359fc3dc4d 100644 --- a/crates/bevy_reflect/src/serde/ser/error_utils.rs +++ b/crates/bevy_reflect/src/serde/ser/error_utils.rs @@ -1,17 +1,17 @@ use core::fmt::Display; use serde::ser::Error; -#[cfg(feature = "debug_stack")] -use std::thread_local; +crate::cfg::debug_stack! { + use std::thread_local; -#[cfg(feature = "debug_stack")] -thread_local! { - /// The thread-local [`TypeInfoStack`] used for debugging. - /// - /// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack - pub(super) static TYPE_INFO_STACK: core::cell::RefCell = const { core::cell::RefCell::new( - crate::type_info_stack::TypeInfoStack::new() - ) }; + thread_local! { + /// The thread-local [`TypeInfoStack`] used for debugging. + /// + /// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack + pub(super) static TYPE_INFO_STACK: core::cell::RefCell = const { core::cell::RefCell::new( + crate::type_info_stack::TypeInfoStack::new() + ) }; + } } /// A helper function for generating a custom serialization error message. @@ -21,9 +21,12 @@ thread_local! { /// /// [type info stack]: crate::type_info_stack::TypeInfoStack pub(super) fn make_custom_error(msg: impl Display) -> E { - #[cfg(feature = "debug_stack")] - return TYPE_INFO_STACK - .with_borrow(|stack| E::custom(format_args!("{msg} (stack: {stack:?})"))); - #[cfg(not(feature = "debug_stack"))] - return E::custom(msg); + crate::cfg::switch! { + crate::cfg::debug_stack => { + TYPE_INFO_STACK.with_borrow(|stack| E::custom(format_args!("{msg} (stack: {stack:?})"))) + } + _ => { + E::custom(msg) + } + } } diff --git a/crates/bevy_reflect/src/serde/ser/mod.rs b/crates/bevy_reflect/src/serde/ser/mod.rs index 25399e1d711e5..d13b62f6902d4 100644 --- a/crates/bevy_reflect/src/serde/ser/mod.rs +++ b/crates/bevy_reflect/src/serde/ser/mod.rs @@ -24,8 +24,6 @@ mod tests { serde::{ReflectSerializer, ReflectSerializerProcessor}, PartialReflect, Reflect, ReflectSerialize, Struct, TypeRegistry, }; - #[cfg(feature = "functions")] - use alloc::boxed::Box; use alloc::{ string::{String, ToString}, vec, @@ -442,22 +440,17 @@ mod tests { let serializer = ReflectSerializer::new(&value, ®istry); let error = ron::ser::to_string(&serializer).unwrap_err(); - #[cfg(feature = "debug_stack")] - assert_eq!( - error, - ron::Error::Message( + + let expected_error = crate::cfg::switch! {{ + crate::cfg::debug_stack => { "type `core::ops::RangeInclusive` is not registered in the type registry (stack: `core::ops::RangeInclusive`)" - .to_string(), - ) - ); - #[cfg(not(feature = "debug_stack"))] - assert_eq!( - error, - ron::Error::Message( + } + _ => { "type `core::ops::RangeInclusive` is not registered in the type registry" - .to_string(), - ) - ); + } + }}; + + assert_eq!(error, ron::Error::Message(expected_error.to_string())); } #[test] @@ -468,20 +461,17 @@ mod tests { let serializer = ReflectSerializer::new(&value, ®istry); let error = ron::ser::to_string(&serializer).unwrap_err(); - #[cfg(feature = "debug_stack")] - assert_eq!( - error, - ron::Error::Message( - "type `core::ops::RangeInclusive` did not register the `ReflectSerialize` or `ReflectSerializeWithRegistry` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `core::ops::RangeInclusive`)".to_string() - ) - ); - #[cfg(not(feature = "debug_stack"))] - assert_eq!( - error, - ron::Error::Message( - "type `core::ops::RangeInclusive` did not register the `ReflectSerialize` type data. For certain types, this may need to be registered manually using `register_type_data`".to_string() - ) - ); + + let expected_error = crate::cfg::switch! {{ + crate::cfg::debug_stack => { + "type `core::ops::RangeInclusive` did not register the `ReflectSerialize` or `ReflectSerializeWithRegistry` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `core::ops::RangeInclusive`)" + } + _ => { + "type `core::ops::RangeInclusive` did not register the `ReflectSerialize` type data. For certain types, this may need to be registered manually using `register_type_data`" + } + }}; + + assert_eq!(error, ron::Error::Message(expected_error.to_string())); } #[test] @@ -638,23 +628,21 @@ mod tests { let serializer = ReflectSerializer::with_processor(&value, ®istry, &processor); let error = ron::ser::to_string_pretty(&serializer, PrettyConfig::default()).unwrap_err(); - #[cfg(feature = "debug_stack")] - assert_eq!( - error, - ron::Error::Message("my custom serialize error (stack: `i32`)".to_string()) - ); - #[cfg(not(feature = "debug_stack"))] - assert_eq!( - error, - ron::Error::Message("my custom serialize error".to_string()) - ); + let expected_error = crate::cfg::switch! {{ + crate::cfg::debug_stack => { + "my custom serialize error (stack: `i32`)" + } + _ => { + "my custom serialize error" + } + }}; + + assert_eq!(error, ron::Error::Message(expected_error.to_string())); } - #[cfg(feature = "functions")] - mod functions { - use super::*; + crate::cfg::functions! { use crate::func::{DynamicFunction, IntoFunction}; - use alloc::string::ToString; + use alloc::boxed::Box; #[test] fn should_not_serialize_function() { @@ -673,24 +661,20 @@ mod tests { let error = ron::ser::to_string(&serializer).unwrap_err(); - #[cfg(feature = "debug_stack")] - assert_eq!( - error, - ron::Error::Message("functions cannot be serialized (stack: `bevy_reflect::serde::ser::tests::functions::MyStruct`)".to_string()) - ); + let expected_error = crate::cfg::switch! {{ + crate::cfg::debug_stack => { + "functions cannot be serialized (stack: `bevy_reflect::serde::ser::tests::MyStruct`)" + } + _ => { + "functions cannot be serialized" + } + }}; - #[cfg(not(feature = "debug_stack"))] - assert_eq!( - error, - ron::Error::Message("functions cannot be serialized".to_string()) - ); + assert_eq!(error, ron::Error::Message(expected_error.to_string())); } } - #[cfg(feature = "debug_stack")] - mod debug_stack { - use super::*; - + crate::cfg::debug_stack! { #[test] fn should_report_context_in_errors() { #[derive(Reflect)] @@ -725,7 +709,7 @@ mod tests { assert_eq!( error, ron::Error::Message( - "type `core::ops::RangeInclusive` is not registered in the type registry (stack: `bevy_reflect::serde::ser::tests::debug_stack::Foo` -> `bevy_reflect::serde::ser::tests::debug_stack::Bar` -> `bevy_reflect::serde::ser::tests::debug_stack::Baz` -> `alloc::vec::Vec>` -> `core::ops::RangeInclusive`)".to_string() + "type `core::ops::RangeInclusive` is not registered in the type registry (stack: `bevy_reflect::serde::ser::tests::Foo` -> `bevy_reflect::serde::ser::tests::Bar` -> `bevy_reflect::serde::ser::tests::Baz` -> `alloc::vec::Vec>` -> `core::ops::RangeInclusive`)".to_string() ) ); } diff --git a/crates/bevy_reflect/src/serde/ser/serializer.rs b/crates/bevy_reflect/src/serde/ser/serializer.rs index afe6b56b1dfaf..b41fcaac78ba6 100644 --- a/crates/bevy_reflect/src/serde/ser/serializer.rs +++ b/crates/bevy_reflect/src/serde/ser/serializer.rs @@ -234,12 +234,11 @@ impl Serialize for TypedReflectSerializer<'_, P> where S: Serializer, { - #[cfg(feature = "debug_stack")] - { + crate::cfg::debug_stack! { if let Some(info) = self.value.get_represented_type_info() { TYPE_INFO_STACK.with_borrow_mut(|stack| stack.push(info)); } - } + }; // First, check if our processor wants to serialize this type // This takes priority over any other serialization operations diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 8bdd08099b2ba..0f6931b0e8dcc 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -726,14 +726,13 @@ all_tuples!( P ); -#[cfg(feature = "functions")] -const _: () = { +crate::cfg::functions! { macro_rules! impl_get_ownership_tuple { - ($(#[$meta:meta])* $($name: ident),*) => { - $(#[$meta])* - $crate::func::args::impl_get_ownership!(($($name,)*); <$($name),*>); - }; -} + ($(#[$meta:meta])* $($name: ident),*) => { + $(#[$meta])* + $crate::func::args::impl_get_ownership!(($($name,)*); <$($name),*>); + }; + } all_tuples!( #[doc(fake_variadic)] @@ -744,11 +743,11 @@ const _: () = { ); macro_rules! impl_from_arg_tuple { - ($(#[$meta:meta])* $($name: ident),*) => { - $(#[$meta])* - $crate::func::args::impl_from_arg!(($($name,)*); <$($name: FromReflect + MaybeTyped + TypePath + GetTypeRegistration),*>); - }; -} + ($(#[$meta:meta])* $($name: ident),*) => { + $(#[$meta])* + $crate::func::args::impl_from_arg!(($($name,)*); <$($name: FromReflect + MaybeTyped + TypePath + GetTypeRegistration),*>); + }; + } all_tuples!( #[doc(fake_variadic)] @@ -759,11 +758,11 @@ const _: () = { ); macro_rules! impl_into_return_tuple { - ($(#[$meta:meta])* $($name: ident),+) => { - $(#[$meta])* - $crate::func::impl_into_return!(($($name,)*); <$($name: FromReflect + MaybeTyped + TypePath + GetTypeRegistration),*>); - }; -} + ($(#[$meta:meta])* $($name: ident),+) => { + $(#[$meta])* + $crate::func::impl_into_return!(($($name,)*); <$($name: FromReflect + MaybeTyped + TypePath + GetTypeRegistration),*>); + }; + } // The unit type (i.e. `()`) is special-cased, so we skip implementing it here. all_tuples!( @@ -773,7 +772,7 @@ const _: () = { 12, P ); -}; +} #[cfg(test)] mod tests { diff --git a/crates/bevy_render/Cargo.toml b/crates/bevy_render/Cargo.toml index e844d950be33b..f46f22b25bbea 100644 --- a/crates/bevy_render/Cargo.toml +++ b/crates/bevy_render/Cargo.toml @@ -149,9 +149,6 @@ bevy_tasks = { path = "../bevy_tasks", version = "0.16.0-dev", default-features bevy_platform = { path = "../bevy_platform", version = "0.16.0-dev", default-features = false, features = [ "web", ] } -bevy_reflect = { path = "../bevy_reflect", version = "0.16.0-dev", default-features = false, features = [ - "web", -] } [lints] workspace = true diff --git a/crates/bevy_state/Cargo.toml b/crates/bevy_state/Cargo.toml index fd780fc486925..bcb1f495e2ddd 100644 --- a/crates/bevy_state/Cargo.toml +++ b/crates/bevy_state/Cargo.toml @@ -28,19 +28,13 @@ bevy_app = ["dep:bevy_app"] ## Allows access to the `std` crate. Enabling this feature will prevent compilation ## on `no_std` targets, but provides access to certain additional features on ## supported platforms. -std = [ - "bevy_ecs/std", - "bevy_reflect?/std", - "bevy_app?/std", - "bevy_platform/std", -] +std = ["bevy_ecs/std", "bevy_app?/std", "bevy_platform/std"] ## `critical-section` provides the building blocks for synchronization primitives ## on all platforms, including `no_std`. critical-section = [ "bevy_ecs/critical-section", "bevy_app?/critical-section", - "bevy_reflect?/critical-section", "bevy_platform/critical-section", ] diff --git a/crates/bevy_time/Cargo.toml b/crates/bevy_time/Cargo.toml index 494725b08cc4d..d3f045b58f12c 100644 --- a/crates/bevy_time/Cargo.toml +++ b/crates/bevy_time/Cargo.toml @@ -30,7 +30,6 @@ serialize = ["dep:serde", "bevy_ecs/serialize", "bevy_platform/serialize"] ## supported platforms. std = [ "serde?/std", - "bevy_reflect?/std", "bevy_ecs/std", "bevy_app/std", "bevy_platform/std", @@ -42,7 +41,6 @@ std = [ critical-section = [ "bevy_ecs/critical-section", "bevy_platform/critical-section", - "bevy_reflect?/critical-section", "bevy_app/critical-section", ] diff --git a/crates/bevy_transform/Cargo.toml b/crates/bevy_transform/Cargo.toml index 6801f5734b6c3..fae941ad32558 100644 --- a/crates/bevy_transform/Cargo.toml +++ b/crates/bevy_transform/Cargo.toml @@ -72,7 +72,6 @@ std = [ "bevy_log", "bevy_ecs?/std", "bevy_math/std", - "bevy_reflect?/std", "bevy_tasks/std", "bevy_utils/parallel", "serde?/std", @@ -84,7 +83,6 @@ critical-section = [ "bevy_app?/critical-section", "bevy_ecs?/critical-section", "bevy_tasks/critical-section", - "bevy_reflect?/critical-section", ] ## Allows access to the `alloc` crate. diff --git a/crates/bevy_window/Cargo.toml b/crates/bevy_window/Cargo.toml index 2e3e7edf1882a..973428b38a1ea 100644 --- a/crates/bevy_window/Cargo.toml +++ b/crates/bevy_window/Cargo.toml @@ -39,7 +39,6 @@ std = [ "bevy_ecs/std", "bevy_input/std", "bevy_math/std", - "bevy_reflect?/std", "serde?/std", "raw-window-handle/std", "bevy_platform/std", diff --git a/crates/bevy_winit/Cargo.toml b/crates/bevy_winit/Cargo.toml index 43db87a1d2372..623140814e46a 100644 --- a/crates/bevy_winit/Cargo.toml +++ b/crates/bevy_winit/Cargo.toml @@ -77,9 +77,6 @@ bevy_tasks = { path = "../bevy_tasks", version = "0.16.0-dev", default-features bevy_platform = { path = "../bevy_platform", version = "0.16.0-dev", default-features = false, features = [ "web", ] } -bevy_reflect = { path = "../bevy_reflect", version = "0.16.0-dev", default-features = false, features = [ - "web", -] } [lints] workspace = true