Skip to content

Commit

Permalink
Move ser/de under a feature flag (#354)
Browse files Browse the repository at this point in the history
Move the `Serialize` and `Deserialize` derives, based on `typetag`,
under a new `serde` feature flag. This makes it possible to skip
building with `typetag`, which for example is not compatible with `wasm`
so would be a blocker for wasm support.:
  • Loading branch information
djeedai authored Jul 30, 2024
1 parent 07570fe commit 2705b2a
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 49 deletions.
25 changes: 13 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ env:
CARGO_TERM_COLOR: always

jobs:

build:
strategy:
matrix:
Expand Down Expand Up @@ -52,36 +53,36 @@ jobs:
with:
components: rustfmt, clippy
- name: Build & run lib tests (${{ matrix.dimensions }} no GPU)
run: cargo test --lib --no-default-features --features ${{ matrix.dimensions }}
run: cargo test --lib --no-default-features --features ${{ matrix.dimensions }} --features serde
env:
CARGO_INCREMENTAL: 0
if: runner.os == 'linux' && matrix.dimensions != 'all'
- name: Build & run lib tests (${{ matrix.dimensions }} DX12)
shell: bash
run: WGPU_BACKEND=dx12 cargo test --lib --no-default-features --features ${{ matrix.dimensions }} --features gpu_tests
run: WGPU_BACKEND=dx12 cargo test --lib --no-default-features --features ${{ matrix.dimensions }} --features serde,gpu_tests
env:
CARGO_INCREMENTAL: 0
if: runner.os == 'windows' && matrix.dimensions != 'all'
- name: Build & run all tests (${{ matrix.dimensions }} METAL)
shell: bash
run: WGPU_BACKEND=metal cargo test --no-default-features --features ${{ matrix.dimensions }} --features gpu_tests
run: WGPU_BACKEND=metal cargo test --no-default-features --features ${{ matrix.dimensions }} --features serde,gpu_tests
env:
CARGO_INCREMENTAL: 0
if: runner.os == 'macos' && matrix.dimensions != 'all'
- name: Build & run lib tests (all no GPU)
run: cargo test --lib --no-default-features --features "2d 3d"
run: cargo test --lib --no-default-features --features "2d 3d serde"
env:
CARGO_INCREMENTAL: 0
if: runner.os == 'linux' && matrix.dimensions == 'all'
- name: Build & run lib tests (all DX12)
shell: bash
run: WGPU_BACKEND=dx12 cargo test --lib --no-default-features --features "2d 3d gpu_tests"
run: WGPU_BACKEND=dx12 cargo test --lib --no-default-features --features "2d 3d serde gpu_tests"
env:
CARGO_INCREMENTAL: 0
if: runner.os == 'windows' && matrix.dimensions == 'all'
- name: Build & run all tests (all METAL)
shell: bash
run: WGPU_BACKEND=metal cargo test --no-default-features --features "2d 3d gpu_tests"
run: WGPU_BACKEND=metal cargo test --no-default-features --features "2d 3d serde gpu_tests"
env:
CARGO_INCREMENTAL: 0
if: runner.os == 'macos' && matrix.dimensions == 'all'
Expand Down Expand Up @@ -123,19 +124,19 @@ jobs:
for example in .github/example-run/3d/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d bevy/bevy_ci_testing"
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d serde bevy/bevy_ci_testing"
sleep 10
done
for example in .github/example-run/3dpng/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font bevy/png 3d bevy/bevy_ci_testing"
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font bevy/png 3d serde bevy/bevy_ci_testing"
sleep 10
done
for example in .github/example-run/2d/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_sprite bevy/bevy_ui bevy/default_font 2d bevy/bevy_ci_testing"
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_sprite bevy/bevy_ui bevy/default_font 2d serde bevy/bevy_ci_testing"
sleep 10
done
env:
Expand All @@ -147,19 +148,19 @@ jobs:
for example in .github/example-run/3d/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time WGPU_BACKEND=dx12 CI_TESTING_CONFIG=$example cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d bevy/bevy_ci_testing"
time WGPU_BACKEND=dx12 CI_TESTING_CONFIG=$example cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d serde bevy/bevy_ci_testing"
sleep 10
done
for example in .github/example-run/3dpng/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time WGPU_BACKEND=dx12 CI_TESTING_CONFIG=$example cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font bevy/png 3d bevy/bevy_ci_testing"
time WGPU_BACKEND=dx12 CI_TESTING_CONFIG=$example cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font bevy/png 3d serde bevy/bevy_ci_testing"
sleep 10
done
for example in .github/example-run/2d/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time WGPU_BACKEND=dx12 CI_TESTING_CONFIG=$example cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_sprite bevy/bevy_ui bevy/default_font 2d bevy/bevy_ci_testing"
time WGPU_BACKEND=dx12 CI_TESTING_CONFIG=$example cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_sprite bevy/bevy_ui bevy/default_font 2d serde bevy/bevy_ci_testing"
sleep 10
done
env:
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ exclude = ["docs/*.svg", "docs/*.png", "examples/*.gif", "examples/*.png", ".git
autoexamples = false

[features]
default = ["2d", "3d", "gpu_tests", "examples_world_inspector"]
default = ["2d", "3d", "serde", "gpu_tests", "examples_world_inspector"]

# Enable support for rendering through a 2D camera (Camera2dBundle)
2d = []

# Enable support for rendering through a 3D camera (Camera3dBundle)
3d = []

# Enable serializing and deserializing of assets.
serde = ["typetag"]

# Enable tracing annotations
trace = []

Expand All @@ -48,7 +51,7 @@ serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0"
ron = "0.8"
bitflags = "2.3"
typetag = "0.2"
typetag = { version = "0.2", optional = true }
thiserror = "1.0"
# Same versions as Bevy 0.14 (bevy_render)
naga = "0.20"
Expand Down
17 changes: 14 additions & 3 deletions src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use std::ops::Deref;

use bevy::{
asset::{io::Reader, Asset, AssetLoader, AsyncReadExt, LoadContext},
asset::Asset,
reflect::Reflect,
utils::{default, HashSet},
};
use serde::{Deserialize, Serialize};

#[cfg(feature = "serde")]
use bevy::asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext};
#[cfg(feature = "serde")]
use thiserror::Error;

use serde::{Deserialize, Serialize};

use crate::{
modifier::{Modifier, RenderModifier},
ExprHandle, GroupedModifier, ModifierContext, Module, ParticleGroupSet, ParticleLayout,
Expand Down Expand Up @@ -202,7 +207,8 @@ pub enum AlphaMode {
///
/// [`ParticleEffect`]: crate::ParticleEffect
/// [`ParticleEffectBundle`]: crate::ParticleEffectBundle
#[derive(Asset, Default, Clone, Reflect, Serialize, Deserialize)]
#[derive(Asset, Default, Clone, Reflect)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[reflect(from_reflect = false)]
pub struct EffectAsset {
/// Display name of the effect.
Expand Down Expand Up @@ -709,10 +715,12 @@ impl EffectAsset {
/// Asset loader for [`EffectAsset`].
///
/// Effet assets take the `.effect` extension.
#[cfg(feature = "serde")]
#[derive(Default)]
pub struct EffectAssetLoader;

/// Error for the [`EffectAssetLoader`] loading an [`EffectAsset`].
#[cfg(feature = "serde")]
#[derive(Error, Debug)]
pub enum EffectAssetLoaderError {
/// I/O error reading the asset source.
Expand All @@ -724,6 +732,7 @@ pub enum EffectAssetLoaderError {
Ron(#[from] ron::error::SpannedError),
}

#[cfg(feature = "serde")]
impl AssetLoader for EffectAssetLoader {
type Asset = EffectAsset;

Expand All @@ -750,6 +759,7 @@ impl AssetLoader for EffectAssetLoader {

#[cfg(test)]
mod tests {
#[cfg(feature = "serde")]
use ron::ser::PrettyConfig;

use super::*;
Expand Down Expand Up @@ -861,6 +871,7 @@ mod tests {
// assert_eq!(effect.render_layout, render_layout);
}

#[cfg(feature = "serde")]
#[test]
fn test_serde_ron() {
let w = ExprWriter::new();
Expand Down
6 changes: 3 additions & 3 deletions src/modifier/accel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl AccelModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for AccelModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down Expand Up @@ -147,7 +147,7 @@ impl RadialAccelModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for RadialAccelModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down Expand Up @@ -267,7 +267,7 @@ impl TangentAccelModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for TangentAccelModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down
2 changes: 1 addition & 1 deletion src/modifier/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl SetAttributeModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for SetAttributeModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Init | ModifierContext::Update
Expand Down
2 changes: 1 addition & 1 deletion src/modifier/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct CloneModifier {
pub destination_group: u32,
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for CloneModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down
4 changes: 2 additions & 2 deletions src/modifier/force.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl ConformToSphereModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for ConformToSphereModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down Expand Up @@ -269,7 +269,7 @@ impl LinearDragModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for LinearDragModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down
4 changes: 2 additions & 2 deletions src/modifier/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl KillSphereModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for KillSphereModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down Expand Up @@ -141,7 +141,7 @@ impl KillAabbModifier {
}
}

#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl Modifier for KillAabbModifier {
fn context(&self) -> ModifierContext {
ModifierContext::Update
Expand Down
10 changes: 6 additions & 4 deletions src/modifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl std::fmt::Display for ModifierContext {
}

/// Trait describing a modifier customizing an effect pipeline.
#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
pub trait Modifier: Reflect + Send + Sync + 'static {
/// Get the context this modifier applies to.
fn context(&self) -> ModifierContext;
Expand Down Expand Up @@ -209,7 +209,8 @@ impl ParticleGroupSet {
}

/// A [`Modifier`] that affects to one or more groups.
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct GroupedModifier {
/// The modifier.
pub modifier: BoxedModifier,
Expand Down Expand Up @@ -535,7 +536,7 @@ impl<'a> EvalContext for RenderContext<'a> {
}

/// Trait to customize the rendering of alive particles each frame.
#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
pub trait RenderModifier: Modifier {
/// Apply the rendering code.
fn apply_render(&self, module: &mut Module, context: &mut RenderContext);
Expand All @@ -556,7 +557,7 @@ impl Clone for Box<dyn RenderModifier> {
/// Macro to implement the [`Modifier`] trait for a render modifier.
macro_rules! impl_mod_render {
($t:ty, $attrs:expr) => {
#[typetag::serde]
#[cfg_attr(feature = "serde", typetag::serde)]
impl $crate::Modifier for $t {
fn context(&self) -> $crate::ModifierContext {
$crate::ModifierContext::Render
Expand Down Expand Up @@ -648,6 +649,7 @@ mod tests {
assert_eq!(*m_reflect, m);
}

#[cfg(feature = "serde")]
#[test]
fn serde() {
let m = make_test_modifier();
Expand Down
Loading

0 comments on commit 2705b2a

Please sign in to comment.