From 225fcc798d93e80ccb501ae10c4a7485730f5655 Mon Sep 17 00:00:00 2001 From: i509VCB Date: Fri, 9 Dec 2022 17:52:31 -0600 Subject: [PATCH] move strict_asserts macro to wgpu-types --- .cargo/config.toml | 1 - CHANGELOG.md | 1 + wgpu-core/Cargo.toml | 2 +- wgpu-core/src/lib.rs | 3 -- wgpu-core/src/track/buffer.rs | 1 + wgpu-core/src/track/metadata.rs | 1 + wgpu-core/src/track/mod.rs | 1 + wgpu-core/src/track/texture.rs | 1 + wgpu-types/Cargo.toml | 1 + wgpu-types/src/assertions.rs | 66 +++++++++++++++++++++++++++++++++ wgpu-types/src/lib.rs | 2 + wgpu/Cargo.toml | 2 +- wgpu/src/assertions.rs | 50 ------------------------- wgpu/src/context.rs | 6 +-- wgpu/src/lib.rs | 2 - 15 files changed, 79 insertions(+), 61 deletions(-) delete mode 100644 wgpu/src/assertions.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 99eede00d83..daaafd8c8d7 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -5,4 +5,3 @@ run-wasm = "run --release --package run-wasm --" rustflags = [ "--cfg=web_sys_unstable_apis" ] -target = "wasm32-unknown-unknown" diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fc3e46b2d1..5dff8528f96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,6 +114,7 @@ Additionally `Surface::get_default_config` now returns an Option and returns Non - Combine `Surface::get_supported_formats`, `Surface::get_supported_present_modes`, and `Surface::get_supported_alpha_modes` into `Surface::get_capabilities` and `SurfaceCapabilities`. By @cwfitzgerald in [#3157](https://github.com/gfx-rs/wgpu/pull/3157) - Make `Surface::get_default_config` return an Option to prevent panics. By @cwfitzgerald in [#3157](https://github.com/gfx-rs/wgpu/pull/3157) - Lower the `max_buffer_size` limit value for compatibility with Apple2 and WebGPU compliance. By @jinleili in [#3255](https://github.com/gfx-rs/wgpu/pull/3255) +- The `strict_assert` family of macros was moved to `wgpu-types`. By @i509VCB in [#3051](https://github.com/gfx-rs/wgpu/pull/3051) #### WebGPU diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 85156b2c242..591d2a9fb13 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -34,7 +34,7 @@ emscripten = ["hal/emscripten"] # Apply run-time checks, even in release builds. These are in addition # to the validation carried out at public APIs in all builds. -strict_asserts = [] +strict_asserts = ["wgt/strict_asserts"] angle = ["hal/gles"] # Enable API tracing trace = ["ron", "serde", "wgt/trace", "arrayvec/serde", "naga/serialize"] diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index c8b3fb7296b..ff9f3d4b657 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -35,9 +35,6 @@ clippy::pattern_type_mismatch, )] -#[macro_use] -mod assertions; - pub mod binding_model; pub mod command; mod conv; diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index 65f02c467d5..b7682968b20 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -19,6 +19,7 @@ use crate::{ LifeGuard, RefCount, }; use hal::BufferUses; +use wgt::{strict_assert, strict_assert_eq}; impl ResourceUses for BufferUses { const EXCLUSIVE: Self = Self::EXCLUSIVE; diff --git a/wgpu-core/src/track/metadata.rs b/wgpu-core/src/track/metadata.rs index 728ff5ca0e9..73da0d6c5d3 100644 --- a/wgpu-core/src/track/metadata.rs +++ b/wgpu-core/src/track/metadata.rs @@ -7,6 +7,7 @@ use crate::{ }; use bit_vec::BitVec; use std::{borrow::Cow, marker::PhantomData, mem}; +use wgt::strict_assert; /// A set of resources, holding a [`RefCount`] and epoch for each member. /// diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index 9d85e1ab7b4..4fdb64850c0 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -114,6 +114,7 @@ pub(crate) use stateless::{StatelessBindGroupSate, StatelessTracker}; pub(crate) use texture::{ TextureBindGroupState, TextureSelector, TextureTracker, TextureUsageScope, }; +use wgt::strict_assert_ne; /// A structure containing all the information about a particular resource /// transition. User code should be able to generate a pipeline barrier diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index bc0eb2875eb..57b0f721139 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -34,6 +34,7 @@ use hal::TextureUses; use arrayvec::ArrayVec; use naga::FastHashMap; +use wgt::{strict_assert, strict_assert_eq}; use std::{borrow::Cow, iter, marker::PhantomData, ops::Range, vec::Drain}; diff --git a/wgpu-types/Cargo.toml b/wgpu-types/Cargo.toml index 0748296a813..9e358cb2006 100644 --- a/wgpu-types/Cargo.toml +++ b/wgpu-types/Cargo.toml @@ -18,6 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] trace = ["serde"] replay = ["serde"] +strict_asserts = [] [dependencies] bitflags.workspace = true diff --git a/wgpu-types/src/assertions.rs b/wgpu-types/src/assertions.rs index e69de29bb2d..87b4de33efa 100644 --- a/wgpu-types/src/assertions.rs +++ b/wgpu-types/src/assertions.rs @@ -0,0 +1,66 @@ +//! Macros for validation internal to the wgpu. +//! +//! This module defines assertion macros that respect `wgpu-type`'s +//! `"strict_asserts"` feature. +//! +//! Because `wgpu-core`'s public APIs validate their arguments in all +//! types of builds, for performance, the `track` module skips some of +//! Rust's usual run-time checks on its internal operations in release +//! builds. However, some `wgpu-core` applications have a strong +//! preference for robustness over performance. To accommodate them, +//! `wgpu-core`'s `"strict_asserts"` feature enables that validation +//! in both debug and release builds. + +/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated. +#[cfg(feature = "strict_asserts")] +#[macro_export] +macro_rules! strict_assert { + ( $( $arg:tt )* ) => { + assert!( $( $arg )* ) + } +} + +/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated. +#[cfg(feature = "strict_asserts")] +#[macro_export] +macro_rules! strict_assert_eq { + ( $( $arg:tt )* ) => { + assert_eq!( $( $arg )* ) + } +} + +/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated. +#[cfg(feature = "strict_asserts")] +#[macro_export] +macro_rules! strict_assert_ne { + ( $( $arg:tt )* ) => { + assert_ne!( $( $arg )* ) + } +} + +/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated. +#[cfg(not(feature = "strict_asserts"))] +#[macro_export] +macro_rules! strict_assert { + ( $( $arg:tt )* ) => { + debug_assert!( $( $arg )* ) + }; +} + +/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated. +#[cfg(not(feature = "strict_asserts"))] +#[macro_export] +macro_rules! strict_assert_eq { + ( $( $arg:tt )* ) => { + debug_assert_eq!( $( $arg )* ) + }; +} + +/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated. +#[cfg(not(feature = "strict_asserts"))] +#[macro_export] +macro_rules! strict_assert_ne { + ( $( $arg:tt )* ) => { + debug_assert_ne!( $( $arg )* ) + }; +} diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 82ffaf31f31..4d648f45e9c 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -14,6 +14,8 @@ use serde::{Deserialize, Serialize}; use std::hash::{Hash, Hasher}; use std::{num::NonZeroU32, ops::Range}; +mod assertions; + // 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 diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 7da719dd3dc..0e1c2595188 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -79,7 +79,7 @@ test = true default = ["wgsl", "expose-ids", "strict_asserts"] # Apply run-time checks, even in release builds. These are in addition # to the validation carried out at public APIs in all builds. -strict_asserts = ["wgc/strict_asserts"] +strict_asserts = ["wgc/strict_asserts", "wgt/strict_asserts"] spirv = ["naga/spv-in"] glsl = ["naga/glsl-in"] wgsl = ["wgc?/wgsl"] diff --git a/wgpu/src/assertions.rs b/wgpu/src/assertions.rs deleted file mode 100644 index e34cb728c3e..00000000000 --- a/wgpu/src/assertions.rs +++ /dev/null @@ -1,50 +0,0 @@ -// TODO: Do not merge without docs -#![allow(missing_docs)] - -#[cfg(feature = "strict_asserts")] -#[macro_export] -macro_rules! strict_assert { - ( $( $arg:tt )* ) => { - assert!( $( $arg )* ) - } -} - -#[cfg(feature = "strict_asserts")] -#[macro_export] -macro_rules! strict_assert_eq { - ( $( $arg:tt )* ) => { - assert_eq!( $( $arg )* ) - } -} - -#[cfg(feature = "strict_asserts")] -#[macro_export] -macro_rules! strict_assert_ne { - ( $( $arg:tt )* ) => { - assert_ne!( $( $arg )* ) - } -} - -#[cfg(not(feature = "strict_asserts"))] -#[macro_export] -macro_rules! strict_assert { - ( $( $arg:tt )* ) => { - debug_assert!( $( $arg )* ) - }; -} - -#[cfg(not(feature = "strict_asserts"))] -#[macro_export] -macro_rules! strict_assert_eq { - ( $( $arg:tt )* ) => { - debug_assert_eq!( $( $arg )* ) - }; -} - -#[cfg(not(feature = "strict_asserts"))] -#[macro_export] -macro_rules! strict_assert_ne { - ( $( $arg:tt )* ) => { - debug_assert_ne!( $( $arg )* ) - }; -} diff --git a/wgpu/src/context.rs b/wgpu/src/context.rs index e40897d27d2..94e247ffc66 100644 --- a/wgpu/src/context.rs +++ b/wgpu/src/context.rs @@ -3,9 +3,9 @@ use std::{ }; use wgt::{ - AdapterInfo, BufferAddress, BufferSize, Color, DownlevelCapabilities, DynamicOffset, Extent3d, - Features, ImageDataLayout, ImageSubresourceRange, IndexFormat, Limits, ShaderStages, - SurfaceConfiguration, SurfaceStatus, TextureFormat, TextureFormatFeatures, + strict_assert, AdapterInfo, BufferAddress, BufferSize, Color, DownlevelCapabilities, + DynamicOffset, Extent3d, Features, ImageDataLayout, ImageSubresourceRange, IndexFormat, Limits, + ShaderStages, SurfaceConfiguration, SurfaceStatus, TextureFormat, TextureFormatFeatures, }; use crate::{ diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 2e4f21e89a3..2369989cbdd 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -6,8 +6,6 @@ #![doc(html_logo_url = "https://raw.githubusercontent.com/gfx-rs/wgpu/master/logo.png")] #![warn(missing_docs, unsafe_op_in_unsafe_fn)] -#[macro_use] -mod assertions; mod backend; mod context; pub mod util;