Skip to content

Commit 0c2265d

Browse files
chore: separate new unsafe ops into blocks
Unsafe operations can be exhausting to validate by themselves. Therefore, it's often interesting to separate these out so we can justify each individual operation, and let a human reader accumulate and drop supporting safety context in the smallest increments necessary. To that end, this commit can pave the way for future work where we may do something like enable [`clippy::undocumented_unsafe_blocks`], which calls out `unsafe` blocks that do not have a `SAFETY` comment immediately above them. This commit only separates the operations in `unsafe` blocks I added in this same PR; I'm deliberately leaving existing `unsafe` blocks alone, ATM. [`clippy::undocumented_unsafe_blocks`]: https://rust-lang.github.io/rust-clippy/stable/index.html#undocumented_unsafe_blocks
1 parent 1f984c3 commit 0c2265d

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

wgpu-core/src/track/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,9 @@ impl<A: hub::HalApi> ResourceMetadataProvider<'_, A> {
420420
}
421421
ResourceMetadataProvider::Indirect { metadata } => {
422422
metadata.tracker_assert_in_bounds(index);
423-
(unsafe { *metadata.epochs.get_unchecked(index) }, unsafe {
424-
metadata
425-
.ref_counts
426-
.get_unchecked(index)
427-
.clone()
428-
.unwrap_unchecked()
423+
(unsafe { *metadata.epochs.get_unchecked(index) }, {
424+
let ref_count = unsafe { metadata.ref_counts.get_unchecked(index) };
425+
unsafe { ref_count.clone().unwrap_unchecked() }
429426
})
430427
}
431428
ResourceMetadataProvider::Resource { epoch } => {

wgpu-core/src/track/texture.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,10 @@ impl<A: hub::HalApi> TextureUsageScope<A> {
291291
self.tracker_assert_in_bounds(index);
292292
scope.tracker_assert_in_bounds(index);
293293

294+
let texture_data = unsafe { texture_data_from_texture(storage, index32) };
294295
unsafe {
295296
insert_or_merge(
296-
texture_data_from_texture(storage, index32),
297+
texture_data,
297298
&mut self.set,
298299
&mut self.metadata,
299300
index32,
@@ -359,9 +360,10 @@ impl<A: hub::HalApi> TextureUsageScope<A> {
359360

360361
self.tracker_assert_in_bounds(index);
361362

363+
let texture_data = unsafe { texture_data_from_texture(storage, index32) };
362364
unsafe {
363365
insert_or_merge(
364-
texture_data_from_texture(storage, index32),
366+
texture_data,
365367
&mut self.set,
366368
&mut self.metadata,
367369
index32,
@@ -467,13 +469,8 @@ impl<A: hub::HalApi> TextureTracker<A> {
467469

468470
self.tracker_assert_in_bounds(index);
469471

470-
unsafe {
471-
self.metadata
472-
.ref_counts
473-
.get_unchecked(index)
474-
.as_ref()
475-
.unwrap_unchecked()
476-
}
472+
let ref_count = unsafe { self.metadata.ref_counts.get_unchecked(index) };
473+
unsafe { ref_count.as_ref().unwrap_unchecked() }
477474
}
478475

479476
/// Inserts a single texture and a state into the resource tracker.
@@ -683,9 +680,10 @@ impl<A: hub::HalApi> TextureTracker<A> {
683680
if unsafe { !scope.metadata.owned.get(index).unwrap_unchecked() } {
684681
continue;
685682
}
683+
let texture_data = unsafe { texture_data_from_texture(storage, index32) };
686684
unsafe {
687685
insert_or_barrier_update(
688-
texture_data_from_texture(storage, index32),
686+
texture_data,
689687
Some(&mut self.start_set),
690688
&mut self.end_set,
691689
&mut self.metadata,

wgpu-hal/src/vulkan/device.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ impl super::DeviceShared {
4848
.collect();
4949
&buffer_vec
5050
};
51+
52+
let name = unsafe { CStr::from_bytes_with_nul_unchecked(name_bytes) };
53+
5154
let _result = unsafe {
5255
extension.debug_utils_set_object_name(
5356
self.raw.handle(),
5457
&vk::DebugUtilsObjectNameInfoEXT::builder()
5558
.object_type(object_type)
5659
.object_handle(object.as_raw())
57-
.object_name(CStr::from_bytes_with_nul_unchecked(name_bytes)),
60+
.object_name(name),
5861
)
5962
};
6063
}

0 commit comments

Comments
 (0)