combine: improve pointer handling

This commit is contained in:
Simon Gardling 2025-03-28 17:33:26 -04:00
parent b8f1e28eed
commit 492c527498
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -114,11 +114,14 @@ where
// We mutate grid buffers and read grid data. We use unsafe because we need shared/unique borrows on different fields of the same Grid struct. // We mutate grid buffers and read grid data. We use unsafe because we need shared/unique borrows on different fields of the same Grid struct.
bufs.iter().enumerate().for_each(|(i, buf)| { bufs.iter().enumerate().for_each(|(i, buf)| {
let buf_ptr = *buf as *const Vec<f32> as *mut Vec<f32>; let buf_ptr = *buf as *const Vec<f32> as *mut Vec<f32>;
unsafe { buf_ptr.as_mut() }.unwrap().fill(0.0); // SAFETY! we can take these are raw pointers because we are
// getting it from a `&mut [Grid]`
let buf_ptr_mut = unsafe { buf_ptr.as_mut().unwrap_unchecked() };
buf_ptr_mut.fill(0.0);
datas.iter().enumerate().for_each(|(j, other)| { datas.iter().enumerate().for_each(|(j, other)| {
let multiplier = attraction_table[i].as_ref()[j]; let multiplier = attraction_table[i].as_ref()[j];
unsafe { buf_ptr.as_mut() } buf_ptr_mut
.unwrap()
.iter_mut() .iter_mut()
.zip(*other) .zip(*other)
.for_each(|(to, from)| *to += from * multiplier) .for_each(|(to, from)| *to += from * multiplier)