Skip to content

Commit 95da898

Browse files
authored
Add load/store_unchecked to ByteAddressableBuffer without bounds check (#746)
1 parent 71d6001 commit 95da898

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

crates/spirv-std/src/byte_addressable_buffer.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ impl<'a> ByteAddressableBuffer<'a> {
5050
buffer_load_intrinsic(self.data, byte_index)
5151
}
5252

53+
/// Loads an arbitrary type from the buffer. `byte_index` must be a multiple of 4, otherwise,
54+
/// it will get silently rounded down to the nearest multiple of 4. Bounds checking is not
55+
/// performed.
56+
///
57+
/// # Safety
58+
/// This function allows writing a type to an untyped buffer, then reading a different type
59+
/// from the same buffer, allowing all sorts of safety guarantees to be bypassed (effectively a
60+
/// transmute). Additionally, bounds checking is not performed.
61+
pub unsafe fn load_unchecked<T>(self, byte_index: u32) -> T {
62+
buffer_load_intrinsic(self.data, byte_index)
63+
}
64+
5365
/// Stores an arbitrary type int the buffer. `byte_index` must be a multiple of 4, otherwise,
5466
/// it will get silently rounded down to the nearest multiple of 4.
5567
///
@@ -63,4 +75,16 @@ impl<'a> ByteAddressableBuffer<'a> {
6375
}
6476
buffer_store_intrinsic(self.data, byte_index, value);
6577
}
78+
79+
/// Stores an arbitrary type int the buffer. `byte_index` must be a multiple of 4, otherwise,
80+
/// it will get silently rounded down to the nearest multiple of 4. Bounds checking is not
81+
/// performed.
82+
///
83+
/// # Safety
84+
/// This function allows writing a type to an untyped buffer, then reading a different type
85+
/// from the same buffer, allowing all sorts of safety guarantees to be bypassed (effectively a
86+
/// transmute). Additionally, bounds checking is not performed.
87+
pub unsafe fn store_unchecked<T>(self, byte_index: u32, value: T) {
88+
buffer_store_intrinsic(self.data, byte_index, value);
89+
}
6690
}

0 commit comments

Comments
 (0)