diff --git a/Cargo.lock b/Cargo.lock index feb64e2729..1e2fba89bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,16 +173,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bitflags_serde_shim" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25c3d626f0280ec39b33a6fc5c6c1067432b4c41e94aee40ded197a6649bf025" -dependencies = [ - "bitflags", - "serde", -] - [[package]] name = "block" version = "0.1.6" @@ -2898,7 +2888,6 @@ name = "wgpu-types" version = "0.14.0" dependencies = [ "bitflags", - "bitflags_serde_shim", "serde", "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 211736a5fa..784390f8ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,7 +46,6 @@ version = "0.10" arrayvec = "0.7" async-executor = "1.0" bitflags = "1" -bitflags_serde_shim = "0.2" bit-vec = "0.6" bytemuck = "1.4" cargo-run-wasm = "0.2.0" diff --git a/wgpu-types/Cargo.toml b/wgpu-types/Cargo.toml index 44b046e37c..0748296a81 100644 --- a/wgpu-types/Cargo.toml +++ b/wgpu-types/Cargo.toml @@ -16,13 +16,12 @@ rustdoc-args = ["--cfg", "docsrs"] [lib] [features] -trace = ["serde", "bitflags_serde_shim"] -replay = ["serde", "bitflags_serde_shim"] +trace = ["serde"] +replay = ["serde"] [dependencies] bitflags.workspace = true serde = { workspace = true, features = ["serde_derive"], optional = true } -bitflags_serde_shim = { workspace = true, optional = true } [dev-dependencies] serde_json.workspace = true diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 59710afeca..8c6b61a82b 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -14,6 +14,37 @@ use serde::{Deserialize, Serialize}; use std::hash::{Hash, Hasher}; use std::{num::NonZeroU32, ops::Range}; +// Use this macro instead of the one provided by the bitflags_serde_shim crate +// because the latter produces an error when deserializing bits that are not +// specified in the bitflags, while we want deserialization to succeed and +// and unspecified bits to lead to errors handled in wgpu-core. +// Note that plainly deriving Serialize and Deserialized would have a similar +// behavior to this macro (unspecified bit do not produce an error). +macro_rules! impl_serde_for_bitflags { + ($name:ident) => { + #[cfg(feature = "trace")] + impl serde::Serialize for $name { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.bits().serialize(serializer) + } + } + + #[cfg(feature = "replay")] + impl<'de> serde::Deserialize<'de> for $name { + fn deserialize(deserializer: D) -> Result<$name, D::Error> + where + D: serde::Deserializer<'de>, + { + let value = <_ as serde::Deserialize<'de>>::deserialize(deserializer)?; + Ok(unsafe { $name::from_bits_unchecked(value) }) + } + } + }; +} + /// Integral type used for buffer offsets. pub type BufferAddress = u64; /// Integral type used for buffer slice sizes. @@ -115,8 +146,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(Backends); +impl_serde_for_bitflags!(Backends); impl From for Backends { fn from(backend: Backend) -> Self { @@ -633,8 +663,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(Features); +impl_serde_for_bitflags!(Features); impl Features { /// Mask of all features which are part of the upstream WebGPU standard. @@ -1090,8 +1119,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(DownlevelFlags); +impl_serde_for_bitflags!(DownlevelFlags); impl DownlevelFlags { /// All flags that indicate if the backend is WebGPU compliant @@ -1213,8 +1241,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(ShaderStages); +impl_serde_for_bitflags!(ShaderStages); /// Dimensions of a particular texture view. /// @@ -1666,8 +1693,7 @@ impl TextureFormatFeatureFlags { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(TextureFormatFeatureFlags); +impl_serde_for_bitflags!(TextureFormatFeatureFlags); /// Features supported by a given texture format /// @@ -3085,8 +3111,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(ColorWrites); +impl_serde_for_bitflags!(ColorWrites); impl Default for ColorWrites { fn default() -> Self { @@ -3644,8 +3669,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(BufferUsages); +impl_serde_for_bitflags!(BufferUsages); /// Describes a [`Buffer`](../wgpu/struct.Buffer.html). /// @@ -3846,8 +3870,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(TextureUsages); +impl_serde_for_bitflags!(TextureUsages); /// Configures a [`Surface`] for presentation. /// @@ -5046,8 +5069,7 @@ bitflags::bitflags! { } } -#[cfg(feature = "bitflags_serde_shim")] -bitflags_serde_shim::impl_serde_for_bitflags!(PipelineStatisticsTypes); +impl_serde_for_bitflags!(PipelineStatisticsTypes); /// Argument buffer layout for draw_indirect commands. #[repr(C)]