wgpu_types/
assertions.rs

1//! Macros for validation internal to the wgpu.
2//!
3//! This module defines assertion macros that respect `wgpu-type`'s
4//! `"strict_asserts"` feature.
5//!
6//! Because `wgpu-core`'s public APIs validate their arguments in all
7//! types of builds, for performance, the `track` module skips some of
8//! Rust's usual run-time checks on its internal operations in release
9//! builds. However, some `wgpu-core` applications have a strong
10//! preference for robustness over performance. To accommodate them,
11//! `wgpu-core`'s `"strict_asserts"` feature enables that validation
12//! in both debug and release builds.
13
14/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`].
15#[cfg(feature = "strict_asserts")]
16#[macro_export]
17macro_rules! strict_assert {
18    ( $( $arg:tt )* ) => {
19        assert!( $( $arg )* )
20    }
21}
22
23/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`].
24#[cfg(feature = "strict_asserts")]
25#[macro_export]
26macro_rules! strict_assert_eq {
27    ( $( $arg:tt )* ) => {
28        assert_eq!( $( $arg )* )
29    }
30}
31
32/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`].
33#[cfg(feature = "strict_asserts")]
34#[macro_export]
35macro_rules! strict_assert_ne {
36    ( $( $arg:tt )* ) => {
37        assert_ne!( $( $arg )* )
38    }
39}
40
41/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`]
42#[cfg(not(feature = "strict_asserts"))]
43#[macro_export]
44macro_rules! strict_assert {
45    ( $( $arg:tt )* ) => {
46        debug_assert!( $( $arg )* )
47    };
48}
49
50/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`]
51#[cfg(not(feature = "strict_asserts"))]
52#[macro_export]
53macro_rules! strict_assert_eq {
54    ( $( $arg:tt )* ) => {
55        debug_assert_eq!( $( $arg )* )
56    };
57}
58
59/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`]
60#[cfg(not(feature = "strict_asserts"))]
61#[macro_export]
62macro_rules! strict_assert_ne {
63    ( $( $arg:tt )* ) => {
64        debug_assert_ne!( $( $arg )* )
65    };
66}
67
68/// Unwrapping using strict_asserts
69pub trait StrictAssertUnwrapExt<T> {
70    /// Unchecked unwrap, with a [`strict_assert`] backed assertion of validitly.
71    ///
72    /// # Safety
73    ///
74    /// It _must_ be valid to call unwrap_unchecked on this value.
75    unsafe fn strict_unwrap_unchecked(self) -> T;
76}
77
78impl<T> StrictAssertUnwrapExt<T> for Option<T> {
79    unsafe fn strict_unwrap_unchecked(self) -> T {
80        strict_assert!(self.is_some(), "Called strict_unwrap_unchecked on None");
81        // SAFETY: Checked by above assert, or by assertion by unsafe.
82        unsafe { self.unwrap_unchecked() }
83    }
84}
85
86impl<T, E> StrictAssertUnwrapExt<T> for Result<T, E> {
87    unsafe fn strict_unwrap_unchecked(self) -> T {
88        strict_assert!(self.is_ok(), "Called strict_unwrap_unchecked on Err");
89        // SAFETY: Checked by above assert, or by assertion by unsafe.
90        unsafe { self.unwrap_unchecked() }
91    }
92}