Skip to content

Commit cce2b48

Browse files
authored
examples: Replace one more unsafe transmute() with trivial ptr::cast() (#351)
Noticed that one more pointer was being transmuted to another type rather than using a trivial cast, before `unsafe`ly dereferencing it. This also removes the `ptr::read()` syntax introduced in #344 again, in favour of using a simple and more standard `*` deref.
1 parent 14baa1b commit cce2b48

File tree

3 files changed

+4
-6
lines changed

3 files changed

+4
-6
lines changed

examples/compute/compute-argument-buffer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn main() {
8787
command_buffer.commit();
8888
command_buffer.wait_until_completed();
8989

90-
let sum = unsafe { sum.contents().cast::<u32>().read() };
90+
let sum = unsafe { *sum.contents().cast::<u32>() };
9191
assert_eq!(465, sum);
9292
});
9393
}

examples/compute/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn main() {
6969
let mut gpu_end = 0;
7070
device.sample_timestamps(&mut cpu_end, &mut gpu_end);
7171

72-
let sum = unsafe { sum.contents().cast::<u32>().read() };
72+
let sum = unsafe { *sum.contents().cast::<u32>() };
7373
println!("Compute shader sum: {}", sum);
7474

7575
assert_eq!(num_elements, sum);

examples/raytracing/renderer.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
collections::BTreeMap,
3-
mem::transmute,
43
ops::Index,
54
sync::{Arc, Condvar, Mutex},
65
};
@@ -451,10 +450,9 @@ impl Renderer {
451450
command_encoder.end_encoding();
452451
command_buffer.commit();
453452
command_buffer.wait_until_completed();
454-
let compacted_size: *const u32 = unsafe { transmute(compacted_size_buffer.contents()) };
455-
let compacted_size = unsafe { *compacted_size } as NSUInteger;
453+
let compacted_size = unsafe { *compacted_size_buffer.contents().cast::<u32>() };
456454
let compacted_acceleration_structure =
457-
device.new_acceleration_structure_with_size(compacted_size);
455+
device.new_acceleration_structure_with_size(compacted_size as NSUInteger);
458456
let command_buffer = queue.new_command_buffer();
459457
let command_encoder = command_buffer.new_acceleration_structure_command_encoder();
460458
command_encoder.copy_and_compact_acceleration_structure(

0 commit comments

Comments
 (0)