Skip to content

Commit

Permalink
Allow unspecified bits in the API's bitflags
Browse files Browse the repository at this point in the history
The bitflags_serde_shim treats unspecified bits as errors during deserialization. To make the browser integration easier, we want the unsepcified bits (which can be produced by web content) to turn into validation errors instead of panics during deserialization.

Thankfully the bitflags_serde_shim crate is quite trivial, so this PR inlines the macro with the required modification to not check the bits. In addition, bitflags_serde_shim feature is removed, the Serialize and Deserialize implementations are aways the same and only their presence depend on the trace and replay feature flags.
  • Loading branch information
nical committed Nov 23, 2022
1 parent f41a1c2 commit 80991de
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions wgpu-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 40 additions & 18 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.bits().serialize(serializer)
}
}

#[cfg(feature = "replay")]
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(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.
Expand Down Expand Up @@ -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<Backend> for Backends {
fn from(backend: Backend) -> Self {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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).
///
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 80991de

Please sign in to comment.