From 8b25ef33283dcef79f67c55b3eb494042334701c Mon Sep 17 00:00:00 2001 From: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com> Date: Sun, 16 Jun 2024 17:47:30 +0200 Subject: [PATCH] Allow bevy_color use without bevy_reflect support (#13870) # Objective Allow the use of color definitions from Bevy in other contexts than pure Bevy apps, e.g. outside ECS use. In those cases it's nice to not have more dependencies than you need. ## Solution Hide use of reflection behind a feature flag. Defaults to on. ## Points to consider 1. This was straightforward _except_ for the `crates/bevy_color/src/lib.rs` change where I removed `Reflect` as a bound. That is awkward to have feature gated since features should be additive. If the bound was added as part of the feature flag, the result would be _more_ restrictive, and _disable_ impls which did not have the impl. On the other hand having the reflect bound there unconditionally would defeat the purpose of the PR. I opted to remove the bound since it seems overly restrictive anyway. 2. It's possible to hide `encase` and `bytemuck` behind the new feature flag too (or a separate one). I'm thinking if `bevy-support` is not desired then it's unlikely that the user has need of those. --------- Signed-off-by: Torstein Grindvik Co-authored-by: Torstein Grindvik --- crates/bevy_color/Cargo.toml | 5 +++-- crates/bevy_color/src/color.rs | 9 +++++---- crates/bevy_color/src/hsla.rs | 9 +++++---- crates/bevy_color/src/hsva.rs | 9 +++++---- crates/bevy_color/src/hwba.rs | 9 +++++---- crates/bevy_color/src/laba.rs | 9 +++++---- crates/bevy_color/src/lcha.rs | 9 +++++---- crates/bevy_color/src/lib.rs | 1 - crates/bevy_color/src/linear_rgba.rs | 9 +++++---- crates/bevy_color/src/oklaba.rs | 9 +++++---- crates/bevy_color/src/oklcha.rs | 9 +++++---- crates/bevy_color/src/srgba.rs | 9 +++++---- crates/bevy_color/src/xyza.rs | 9 +++++---- 13 files changed, 58 insertions(+), 47 deletions(-) diff --git a/crates/bevy_color/Cargo.toml b/crates/bevy_color/Cargo.toml index 3d1efedcbe..d02f5efed1 100644 --- a/crates/bevy_color/Cargo.toml +++ b/crates/bevy_color/Cargo.toml @@ -10,10 +10,10 @@ keywords = ["bevy", "color"] rust-version = "1.76.0" [dependencies] -bevy_math = { path = "../bevy_math", version = "0.14.0-dev" } +bevy_math = { path = "../bevy_math", version = "0.14.0-dev", default-features = false } bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [ "bevy", -] } +], optional = true } bytemuck = { version = "1", features = ["derive"] } serde = { version = "1.0", features = ["derive"], optional = true } thiserror = "1.0" @@ -21,6 +21,7 @@ wgpu-types = { version = "0.20", default-features = false, optional = true } encase = { version = "0.8", default-features = false } [features] +default = ["bevy_reflect"] serialize = ["serde"] [lints] diff --git a/crates/bevy_color/src/color.rs b/crates/bevy_color/src/color.rs index 385c3582aa..0700f59cc2 100644 --- a/crates/bevy_color/src/color.rs +++ b/crates/bevy_color/src/color.rs @@ -2,6 +2,7 @@ use crate::{ color_difference::EuclideanDistance, Alpha, Hsla, Hsva, Hue, Hwba, Laba, Lcha, LinearRgba, Luminance, Mix, Oklaba, Oklcha, Srgba, StandardColor, Xyza, }; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// An enumerated type that can represent any of the color types in this crate. @@ -39,11 +40,11 @@ use bevy_reflect::prelude::*; /// due to its perceptual uniformity and broad support for Bevy's color operations. /// To avoid the cost of repeated conversion, and ensure consistent results where that is desired, /// first convert this [`Color`] into your desired color space. -#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub enum Color { diff --git a/crates/bevy_color/src/hsla.rs b/crates/bevy_color/src/hsla.rs index 1f49e48759..6b26fbff8d 100644 --- a/crates/bevy_color/src/hsla.rs +++ b/crates/bevy_color/src/hsla.rs @@ -3,6 +3,7 @@ use crate::{ StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// Color in Hue-Saturation-Lightness (HSL) color space with alpha. @@ -11,11 +12,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Hsla { diff --git a/crates/bevy_color/src/hsva.rs b/crates/bevy_color/src/hsva.rs index cf89ea1013..3e6fefce05 100644 --- a/crates/bevy_color/src/hsva.rs +++ b/crates/bevy_color/src/hsva.rs @@ -2,6 +2,7 @@ use crate::{ Alpha, ColorToComponents, Gray, Hue, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// Color in Hue-Saturation-Value (HSV) color space with alpha. @@ -10,11 +11,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Hsva { diff --git a/crates/bevy_color/src/hwba.rs b/crates/bevy_color/src/hwba.rs index 094b363f5b..078f56cfb7 100644 --- a/crates/bevy_color/src/hwba.rs +++ b/crates/bevy_color/src/hwba.rs @@ -6,6 +6,7 @@ use crate::{ Alpha, ColorToComponents, Gray, Hue, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// Color in Hue-Whiteness-Blackness (HWB) color space with alpha. @@ -14,11 +15,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Hwba { diff --git a/crates/bevy_color/src/laba.rs b/crates/bevy_color/src/laba.rs index 35943aa3b9..391bb9b4fe 100644 --- a/crates/bevy_color/src/laba.rs +++ b/crates/bevy_color/src/laba.rs @@ -3,6 +3,7 @@ use crate::{ Luminance, Mix, Oklaba, Srgba, StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// Color in LAB color space, with alpha @@ -10,11 +11,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Laba { diff --git a/crates/bevy_color/src/lcha.rs b/crates/bevy_color/src/lcha.rs index ddb039f194..b1b63c91ac 100644 --- a/crates/bevy_color/src/lcha.rs +++ b/crates/bevy_color/src/lcha.rs @@ -3,6 +3,7 @@ use crate::{ Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// Color in LCH color space, with alpha @@ -10,11 +11,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Lcha { diff --git a/crates/bevy_color/src/lib.rs b/crates/bevy_color/src/lib.rs index 923a603c0c..95c51a494d 100644 --- a/crates/bevy_color/src/lib.rs +++ b/crates/bevy_color/src/lib.rs @@ -147,7 +147,6 @@ where Self: core::fmt::Debug, Self: Clone + Copy, Self: PartialEq, - Self: bevy_reflect::Reflect, Self: Default, Self: From + Into, Self: From + Into, diff --git a/crates/bevy_color/src/linear_rgba.rs b/crates/bevy_color/src/linear_rgba.rs index 2dafa15eed..3a9a0fd5d2 100644 --- a/crates/bevy_color/src/linear_rgba.rs +++ b/crates/bevy_color/src/linear_rgba.rs @@ -3,6 +3,7 @@ use crate::{ ColorToPacked, Gray, Luminance, Mix, StandardColor, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; use bytemuck::{Pod, Zeroable}; @@ -11,11 +12,11 @@ use bytemuck::{Pod, Zeroable}; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect, Pod, Zeroable)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] #[repr(C)] diff --git a/crates/bevy_color/src/oklaba.rs b/crates/bevy_color/src/oklaba.rs index ec725adbdf..bf0ffdba16 100644 --- a/crates/bevy_color/src/oklaba.rs +++ b/crates/bevy_color/src/oklaba.rs @@ -3,6 +3,7 @@ use crate::{ Gray, Hsla, Hsva, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba, StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// Color in Oklab color space, with alpha @@ -10,11 +11,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Oklaba { diff --git a/crates/bevy_color/src/oklcha.rs b/crates/bevy_color/src/oklcha.rs index 72b178fdc9..08f3fac908 100644 --- a/crates/bevy_color/src/oklcha.rs +++ b/crates/bevy_color/src/oklcha.rs @@ -3,6 +3,7 @@ use crate::{ Laba, Lcha, LinearRgba, Luminance, Mix, Oklaba, Srgba, StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// Color in Oklch color space, with alpha @@ -10,11 +11,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Oklcha { diff --git a/crates/bevy_color/src/srgba.rs b/crates/bevy_color/src/srgba.rs index f8396a9394..100d35632d 100644 --- a/crates/bevy_color/src/srgba.rs +++ b/crates/bevy_color/src/srgba.rs @@ -4,6 +4,7 @@ use crate::{ Luminance, Mix, StandardColor, Xyza, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; use thiserror::Error; @@ -12,11 +13,11 @@ use thiserror::Error; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Srgba { diff --git a/crates/bevy_color/src/xyza.rs b/crates/bevy_color/src/xyza.rs index ea20a2ee72..a9fb422bef 100644 --- a/crates/bevy_color/src/xyza.rs +++ b/crates/bevy_color/src/xyza.rs @@ -3,6 +3,7 @@ use crate::{ StandardColor, }; use bevy_math::{Vec3, Vec4}; +#[cfg(feature = "bevy_reflect")] use bevy_reflect::prelude::*; /// [CIE 1931](https://en.wikipedia.org/wiki/CIE_1931_color_space) color space, also known as XYZ, with an alpha channel. @@ -10,11 +11,11 @@ use bevy_reflect::prelude::*; ///
#[doc = include_str!("../docs/diagrams/model_graph.svg")] ///
-#[derive(Debug, Clone, Copy, PartialEq, Reflect)] -#[reflect(PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(PartialEq, Default))] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), + all(feature = "serialize", feature = "bevy_reflect"), reflect(Serialize, Deserialize) )] pub struct Xyza {