Fix up strict-assert usage (#3320)

* Removes unused assertions.rs

* Strict assert macro fixes

* Strict asserts shouldn't be default on wgpu
This commit is contained in:
Connor Fitzgerald
2022-12-21 01:04:45 -05:00
committed by GitHub
parent 4c5fef4a28
commit 9b9cc330ca
4 changed files with 33 additions and 83 deletions

View File

@@ -1,60 +0,0 @@
//! Macros for validation internal to the resource tracker.
//!
//! This module defines assertion macros that respect `wgpu-core`'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.
#[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 )* )
};
}

View File

@@ -73,11 +73,13 @@ mod allocation {
let name = desc.label.unwrap_or("Unlabeled buffer");
// SAFETY: allocator exists when the windows_rs feature is enabled
let mut allocator = device
.mem_allocator
.as_ref()
.strict_unwrap_unchecked()
.lock();
let mut allocator = unsafe {
device
.mem_allocator
.as_ref()
.strict_unwrap_unchecked()
.lock()
};
// let mut allocator = unsafe { device.mem_allocator.as_ref().unwrap_unchecked().lock() };
let allocation_desc = AllocationCreateDesc::from_winapi_d3d12_resource_desc(
@@ -114,11 +116,13 @@ mod allocation {
let name = desc.label.unwrap_or("Unlabeled texture");
// SAFETY: allocator exists when the windows_rs feature is enabled
let mut allocator = device
.mem_allocator
.as_ref()
.strict_unwrap_unchecked()
.lock();
let mut allocator = unsafe {
device
.mem_allocator
.as_ref()
.strict_unwrap_unchecked()
.lock()
};
let allocation_desc = AllocationCreateDesc::from_winapi_d3d12_resource_desc(
allocator.allocator.device().as_winapi(),
&raw_desc,

View File

@@ -11,7 +11,7 @@
//! `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.
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`].
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert {
@@ -20,7 +20,7 @@ macro_rules! strict_assert {
}
}
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`].
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert_eq {
@@ -29,7 +29,7 @@ macro_rules! strict_assert_eq {
}
}
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`].
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert_ne {
@@ -38,7 +38,7 @@ macro_rules! strict_assert_ne {
}
}
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated.
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`]
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert {
@@ -47,7 +47,7 @@ macro_rules! strict_assert {
};
}
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`]
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert_eq {
@@ -56,7 +56,7 @@ macro_rules! strict_assert_eq {
};
}
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`]
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert_ne {
@@ -65,22 +65,28 @@ macro_rules! strict_assert_ne {
};
}
/// Used to implement strict_assert for unwrap_unchecked
/// Unwrapping using strict_asserts
pub trait StrictAssertUnwrapExt<T> {
/// Implementation of strict_assert for unwrap_unchecked
fn strict_unwrap_unchecked(self) -> T;
/// Unchecked unwrap, with a [`strict_assert`] backed assertion of validitly.
///
/// # Safety
///
/// It _must_ be valid to call unwrap_unchecked on this value.
unsafe fn strict_unwrap_unchecked(self) -> T;
}
impl<T> StrictAssertUnwrapExt<T> for Option<T> {
fn strict_unwrap_unchecked(self) -> T {
unsafe fn strict_unwrap_unchecked(self) -> T {
strict_assert!(self.is_some(), "Called strict_unwrap_unchecked on None");
// SAFETY: Checked by above assert, or by assertion by unsafe.
unsafe { self.unwrap_unchecked() }
}
}
impl<T, E> StrictAssertUnwrapExt<T> for Result<T, E> {
fn strict_unwrap_unchecked(self) -> T {
unsafe fn strict_unwrap_unchecked(self) -> T {
strict_assert!(self.is_ok(), "Called strict_unwrap_unchecked on Err");
// SAFETY: Checked by above assert, or by assertion by unsafe.
unsafe { self.unwrap_unchecked() }
}
}

View File

@@ -76,10 +76,10 @@ name = "water"
test = true
[features]
default = ["wgsl", "expose-ids", "strict_asserts"]
default = ["wgsl", "expose-ids"]
# 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", "wgt/strict_asserts"]
strict_asserts = ["wgc?/strict_asserts", "wgt/strict_asserts"]
spirv = ["naga/spv-in"]
glsl = ["naga/glsl-in"]
wgsl = ["wgc?/wgsl"]