bevy_core: make bevy_reflect optional (#14179)

# Objective

Allow use of `bevy_core` types without needing `bevy_reflect`.

## Solution

Make `bevy_reflect` within `bevy_core` optional. It's compiled in by
default.
Turn on reflect in dependencies as well when this feature is on.

## Testing

- Did you test these changes? If so, how?

I did a `cargo hack -p bevy_core--each-feature build`.


Similar PR: https://github.com/bevyengine/bevy/pull/14167

Discord context starts here:
https://discord.com/channels/691052431525675048/768253008416342076/1258814534651482163

Signed-off-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
Co-authored-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
This commit is contained in:
Torstein Grindvik 2024-07-08 03:09:04 +02:00 committed by GitHub
parent 2d34226043
commit faf3c175b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 12 deletions

View File

@ -10,15 +10,11 @@ keywords = ["bevy"]
[dependencies] [dependencies]
# bevy # bevy
bevy_app = { path = "../bevy_app", version = "0.14.0-dev", features = [ bevy_app = { path = "../bevy_app", version = "0.14.0-dev", default-features = false }
"bevy_reflect", bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev", default-features = false }
] }
bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev", features = [
"bevy_reflect",
] }
bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [ bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
"bevy", "bevy",
] } ], optional = true }
bevy_tasks = { path = "../bevy_tasks", version = "0.14.0-dev" } bevy_tasks = { path = "../bevy_tasks", version = "0.14.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.14.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.14.0-dev" }
@ -27,6 +23,12 @@ serde = { version = "1.0", optional = true }
uuid = "1.0" uuid = "1.0"
[features] [features]
default = ["bevy_reflect"]
bevy_reflect = [
"dep:bevy_reflect",
"bevy_app/bevy_reflect",
"bevy_ecs/bevy_reflect",
]
serialize = ["dep:serde"] serialize = ["dep:serde"]
[dev-dependencies] [dev-dependencies]

View File

@ -36,7 +36,9 @@ use bevy_tasks::tick_global_task_pools_on_main_thread;
pub struct TypeRegistrationPlugin; pub struct TypeRegistrationPlugin;
impl Plugin for TypeRegistrationPlugin { impl Plugin for TypeRegistrationPlugin {
#[cfg_attr(not(feature = "bevy_reflect"), allow(unused_variables))]
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
#[cfg(feature = "bevy_reflect")]
app.register_type::<Name>(); app.register_type::<Name>();
} }
} }

View File

@ -1,7 +1,11 @@
use bevy_ecs::query::QueryData; use bevy_ecs::query::QueryData;
use bevy_ecs::{component::Component, entity::Entity, reflect::ReflectComponent}; #[cfg(feature = "bevy_reflect")]
use bevy_ecs::reflect::ReflectComponent;
use bevy_ecs::{component::Component, entity::Entity};
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::std_traits::ReflectDefault; use bevy_reflect::std_traits::ReflectDefault;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect; use bevy_reflect::Reflect;
use bevy_utils::AHasher; use bevy_utils::AHasher;
use std::{ use std::{
@ -10,7 +14,7 @@ use std::{
ops::Deref, ops::Deref,
}; };
#[cfg(feature = "serialize")] #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// Component used to identify an entity. Stores a hash for faster comparisons. /// Component used to identify an entity. Stores a hash for faster comparisons.
@ -20,9 +24,16 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// [`Name`] should not be treated as a globally unique identifier for entities, /// [`Name`] should not be treated as a globally unique identifier for entities,
/// as multiple entities can have the same name. [`Entity`] should be /// as multiple entities can have the same name. [`Entity`] should be
/// used instead as the default unique identifier. /// used instead as the default unique identifier.
#[derive(Reflect, Component, Clone)] #[derive(Component, Clone)]
#[reflect(Component, Default, Debug)] #[cfg_attr(
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] feature = "bevy_reflect",
derive(Reflect),
reflect(Component, Default, Debug)
)]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Deserialize, Serialize)
)]
pub struct Name { pub struct Name {
hash: u64, // Won't be serialized (see: `bevy_core::serde` module) hash: u64, // Won't be serialized (see: `bevy_core::serde` module)
name: Cow<'static, str>, name: Cow<'static, str>,