Skip to content

Commit dc924bc

Browse files
andyleisersonjimblandy
authored andcommitted
Remove unsafe from hal::BufferBinding::new_unchecked
1 parent 9b966bf commit dc924bc

File tree

6 files changed

+41
-51
lines changed

6 files changed

+41
-51
lines changed

wgpu-core/src/command/bundle.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -964,11 +964,9 @@ impl RenderBundle {
964964
size,
965965
} => {
966966
let buffer = buffer.try_raw(snatch_guard)?;
967-
let bb = unsafe {
968-
// SAFETY: The binding size was checked against the buffer size
969-
// in `set_index_buffer` and again in `IndexState::flush`.
970-
hal::BufferBinding::new_unchecked(buffer, *offset, *size)
971-
};
967+
// SAFETY: The binding size was checked against the buffer size
968+
// in `set_index_buffer` and again in `IndexState::flush`.
969+
let bb = hal::BufferBinding::new_unchecked(buffer, *offset, *size);
972970
unsafe { raw.set_index_buffer(bb, *index_format) };
973971
}
974972
Cmd::SetVertexBuffer {
@@ -978,11 +976,9 @@ impl RenderBundle {
978976
size,
979977
} => {
980978
let buffer = buffer.try_raw(snatch_guard)?;
981-
let bb = unsafe {
982-
// SAFETY: The binding size was checked against the buffer size
983-
// in `set_vertex_buffer` and again in `VertexState::flush`.
984-
hal::BufferBinding::new_unchecked(buffer, *offset, *size)
985-
};
979+
// SAFETY: The binding size was checked against the buffer size
980+
// in `set_vertex_buffer` and again in `VertexState::flush`.
981+
let bb = hal::BufferBinding::new_unchecked(buffer, *offset, *size);
986982
unsafe { raw.set_vertex_buffer(*slot, bb) };
987983
}
988984
Cmd::SetPushConstant {

wgpu-core/src/indirect_validation/dispatch.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,12 @@ impl Dispatch {
232232
resource_index: 0,
233233
count: 1,
234234
}],
235-
buffers: &[unsafe {
236-
// SAFETY: We just created the buffer with this size.
237-
hal::BufferBinding::new_unchecked(dst_buffer.as_ref(), 0, Some(DST_BUFFER_SIZE))
238-
}],
235+
// SAFETY: We just created the buffer with this size.
236+
buffers: &[hal::BufferBinding::new_unchecked(
237+
dst_buffer.as_ref(),
238+
0,
239+
Some(DST_BUFFER_SIZE),
240+
)],
239241
samplers: &[],
240242
textures: &[],
241243
acceleration_structures: &[],
@@ -277,10 +279,8 @@ impl Dispatch {
277279
resource_index: 0,
278280
count: 1,
279281
}],
280-
buffers: &[unsafe {
281-
// SAFETY: We calculated the binding size to fit within the buffer.
282-
hal::BufferBinding::new_unchecked(buffer, 0, binding_size)
283-
}],
282+
// SAFETY: We calculated the binding size to fit within the buffer.
283+
buffers: &[hal::BufferBinding::new_unchecked(buffer, 0, binding_size)],
284284
samplers: &[],
285285
textures: &[],
286286
acceleration_structures: &[],

wgpu-core/src/indirect_validation/draw.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,8 @@ impl Draw {
135135
resource_index: 0,
136136
count: 1,
137137
}],
138-
buffers: &[unsafe {
139-
// SAFETY: We calculated the binding size to fit within the buffer.
140-
hal::BufferBinding::new_unchecked(buffer, 0, binding_size)
141-
}],
138+
// SAFETY: We calculated the binding size to fit within the buffer.
139+
buffers: &[hal::BufferBinding::new_unchecked(buffer, 0, binding_size)],
142140
samplers: &[],
143141
textures: &[],
144142
acceleration_structures: &[],
@@ -683,10 +681,12 @@ fn create_buffer_and_bind_group(
683681
resource_index: 0,
684682
count: 1,
685683
}],
686-
buffers: &[unsafe {
687-
// SAFETY: We just created the buffer with this size.
688-
hal::BufferBinding::new_unchecked(buffer.as_ref(), 0, BUFFER_SIZE)
689-
}],
684+
// SAFETY: We just created the buffer with this size.
685+
buffers: &[hal::BufferBinding::new_unchecked(
686+
buffer.as_ref(),
687+
0,
688+
BUFFER_SIZE,
689+
)],
690690
samplers: &[],
691691
textures: &[],
692692
acceleration_structures: &[],

wgpu-core/src/resource.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,12 @@ impl Buffer {
562562
) -> Result<(hal::BufferBinding<'a, dyn hal::DynBuffer>, u64), BindingError> {
563563
let buf_raw = self.try_raw(snatch_guard)?;
564564
let resolved_size = self.resolve_binding_size(offset, binding_size)?;
565-
unsafe {
566-
// SAFETY: The offset and size passed to hal::BufferBinding::new_unchecked must
567-
// define a binding contained within the buffer.
568-
Ok((
569-
hal::BufferBinding::new_unchecked(buf_raw, offset, binding_size),
570-
resolved_size,
571-
))
572-
}
565+
// SAFETY: The offset and size passed to hal::BufferBinding::new_unchecked must
566+
// define a binding contained within the buffer.
567+
Ok((
568+
hal::BufferBinding::new_unchecked(buf_raw, offset, binding_size),
569+
resolved_size,
570+
))
573571
}
574572

575573
/// Returns the mapping callback in case of error so that the callback can be fired outside

wgpu-hal/examples/halmark/main.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,12 @@ impl<A: hal::Api> Example<A> {
447447
let texture_view = unsafe { device.create_texture_view(&texture, &view_desc).unwrap() };
448448

449449
let global_group = {
450-
let global_buffer_binding = unsafe {
451-
// SAFETY: This is the same size that was specified for buffer creation.
452-
hal::BufferBinding::new_unchecked(
453-
&global_buffer,
454-
0,
455-
NonZeroU64::new(global_buffer_desc.size),
456-
)
457-
};
450+
// SAFETY: This is the same size that was specified for buffer creation.
451+
let global_buffer_binding = hal::BufferBinding::new_unchecked(
452+
&global_buffer,
453+
0,
454+
NonZeroU64::new(global_buffer_desc.size),
455+
);
458456
let texture_binding = hal::TextureBinding {
459457
view: &texture_view,
460458
usage: wgpu_types::TextureUses::RESOURCE,
@@ -488,14 +486,12 @@ impl<A: hal::Api> Example<A> {
488486
};
489487

490488
let local_group = {
491-
let local_buffer_binding = unsafe {
492-
// SAFETY: The size must fit within the buffer.
493-
hal::BufferBinding::new_unchecked(
494-
&local_buffer,
495-
0,
496-
wgpu_types::BufferSize::new(size_of::<Locals>() as _),
497-
)
498-
};
489+
// SAFETY: The size must fit within the buffer.
490+
let local_buffer_binding = hal::BufferBinding::new_unchecked(
491+
&local_buffer,
492+
0,
493+
wgpu_types::BufferSize::new(size_of::<Locals>() as _),
494+
);
499495
let local_group_desc = hal::BindGroupDescriptor {
500496
label: Some("local"),
501497
layout: &local_group_layout,

wgpu-hal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2098,7 +2098,7 @@ impl<'a, B: DynBuffer + ?Sized> BufferBinding<'a, B> {
20982098
/// pass a zero size. When the zero-size binding issue is resolved, the
20992099
/// argument should just match the type of the member.
21002100
/// TODO(<https://github.com/gfx-rs/wgpu/issues/3170>): remove the parameter
2101-
pub unsafe fn new_unchecked<S: Into<Option<NonZeroU64>>>(
2101+
pub fn new_unchecked<S: Into<Option<NonZeroU64>>>(
21022102
buffer: &'a B,
21032103
offset: wgt::BufferAddress,
21042104
size: S,

0 commit comments

Comments
 (0)