Skip to content

Commit 6024408

Browse files
committed
Strict assert macro fixes
1 parent 213371f commit 6024408

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

wgpu-hal/src/dx12/suballocation.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ mod allocation {
7373
let name = desc.label.unwrap_or("Unlabeled buffer");
7474

7575
// SAFETY: allocator exists when the windows_rs feature is enabled
76-
let mut allocator = device
77-
.mem_allocator
78-
.as_ref()
79-
.strict_unwrap_unchecked()
80-
.lock();
76+
let mut allocator = unsafe {
77+
device
78+
.mem_allocator
79+
.as_ref()
80+
.strict_unwrap_unchecked()
81+
.lock()
82+
};
8183

8284
// let mut allocator = unsafe { device.mem_allocator.as_ref().unwrap_unchecked().lock() };
8385
let allocation_desc = AllocationCreateDesc::from_winapi_d3d12_resource_desc(
@@ -114,11 +116,13 @@ mod allocation {
114116
let name = desc.label.unwrap_or("Unlabeled texture");
115117

116118
// SAFETY: allocator exists when the windows_rs feature is enabled
117-
let mut allocator = device
118-
.mem_allocator
119-
.as_ref()
120-
.strict_unwrap_unchecked()
121-
.lock();
119+
let mut allocator = unsafe {
120+
device
121+
.mem_allocator
122+
.as_ref()
123+
.strict_unwrap_unchecked()
124+
.lock()
125+
};
122126
let allocation_desc = AllocationCreateDesc::from_winapi_d3d12_resource_desc(
123127
allocator.allocator.device().as_winapi(),
124128
&raw_desc,

wgpu-types/src/assertions.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! `wgpu-core`'s `"strict_asserts"` feature enables that validation
1212
//! in both debug and release builds.
1313
14-
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated.
14+
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`].
1515
#[cfg(feature = "strict_asserts")]
1616
#[macro_export]
1717
macro_rules! strict_assert {
@@ -20,7 +20,7 @@ macro_rules! strict_assert {
2020
}
2121
}
2222

23-
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
23+
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`].
2424
#[cfg(feature = "strict_asserts")]
2525
#[macro_export]
2626
macro_rules! strict_assert_eq {
@@ -29,7 +29,7 @@ macro_rules! strict_assert_eq {
2929
}
3030
}
3131

32-
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
32+
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`].
3333
#[cfg(feature = "strict_asserts")]
3434
#[macro_export]
3535
macro_rules! strict_assert_ne {
@@ -38,7 +38,7 @@ macro_rules! strict_assert_ne {
3838
}
3939
}
4040

41-
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated.
41+
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`]
4242
#[cfg(not(feature = "strict_asserts"))]
4343
#[macro_export]
4444
macro_rules! strict_assert {
@@ -47,7 +47,7 @@ macro_rules! strict_assert {
4747
};
4848
}
4949

50-
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
50+
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`]
5151
#[cfg(not(feature = "strict_asserts"))]
5252
#[macro_export]
5353
macro_rules! strict_assert_eq {
@@ -56,7 +56,7 @@ macro_rules! strict_assert_eq {
5656
};
5757
}
5858

59-
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
59+
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`]
6060
#[cfg(not(feature = "strict_asserts"))]
6161
#[macro_export]
6262
macro_rules! strict_assert_ne {
@@ -65,22 +65,28 @@ macro_rules! strict_assert_ne {
6565
};
6666
}
6767

68-
/// Used to implement strict_assert for unwrap_unchecked
68+
/// Unwrapping using strict_asserts
6969
pub trait StrictAssertUnwrapExt<T> {
70-
/// Implementation of strict_assert for unwrap_unchecked
71-
fn strict_unwrap_unchecked(self) -> 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;
7276
}
7377

7478
impl<T> StrictAssertUnwrapExt<T> for Option<T> {
75-
fn strict_unwrap_unchecked(self) -> T {
79+
unsafe fn strict_unwrap_unchecked(self) -> T {
7680
strict_assert!(self.is_some(), "Called strict_unwrap_unchecked on None");
81+
// SAFETY: Checked by above assert, or by assertion by unsafe.
7782
unsafe { self.unwrap_unchecked() }
7883
}
7984
}
8085

8186
impl<T, E> StrictAssertUnwrapExt<T> for Result<T, E> {
82-
fn strict_unwrap_unchecked(self) -> T {
87+
unsafe fn strict_unwrap_unchecked(self) -> T {
8388
strict_assert!(self.is_ok(), "Called strict_unwrap_unchecked on Err");
89+
// SAFETY: Checked by above assert, or by assertion by unsafe.
8490
unsafe { self.unwrap_unchecked() }
8591
}
8692
}

wgpu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test = true
7979
default = ["wgsl", "expose-ids", "strict_asserts"]
8080
# Apply run-time checks, even in release builds. These are in addition
8181
# to the validation carried out at public APIs in all builds.
82-
strict_asserts = ["wgc/strict_asserts", "wgt/strict_asserts"]
82+
strict_asserts = ["wgc?/strict_asserts", "wgt/strict_asserts"]
8383
spirv = ["naga/spv-in"]
8484
glsl = ["naga/glsl-in"]
8585
wgsl = ["wgc?/wgsl"]

0 commit comments

Comments
 (0)