Skip to content

Commit 6f63605

Browse files
committed
remove potential ub and make par iter mut nicer
1 parent 0f27b09 commit 6f63605

1 file changed

Lines changed: 15 additions & 30 deletions

File tree

src/matrix.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use crate::chunk::{Chunk, VoxelChunk};
21
use crate::{CHUNK_MAP_HEIGHT, CHUNK_MAP_WIDTH, CHUNK_WIDTH, ChunkIndexType};
32
use avian2d::parry::math::IVector;
43
use bevy::tasks::ComputeTaskPool;
4+
use std::array;
55
use std::hint::assert_unchecked;
66
use std::marker::PhantomData;
7-
use std::mem::{MaybeUninit, offset_of};
7+
use std::mem::MaybeUninit;
88
use std::ops::{Add, Index, IndexMut, Sub};
99
use std::ptr::NonNull;
1010
use std::range::RangeFrom;
11-
use std::{array, ptr};
1211
#[derive(Clone)]
1312
pub struct Matrix<T> {
1413
pub elems: [[T; CHUNK_MAP_WIDTH]; CHUNK_MAP_HEIGHT],
@@ -257,18 +256,12 @@ impl<T> MatrixBounded<T> {
257256
}
258257
pub fn par_iter_mut<R: Send + 'static>(
259258
&mut self,
260-
f: impl Fn(&mut [(MatrixIndex, &mut T)]) -> R + Send + Sync,
259+
f: impl Fn(&mut dyn Iterator<Item = (MatrixIndex, &mut T)>) -> R + Send + Sync,
261260
) -> Vec<R> {
262261
#[repr(transparent)]
263262
struct SendPtr<T>(NonNull<T>);
264263
unsafe impl<T> Send for SendPtr<T> {}
265264
unsafe impl<T> Sync for SendPtr<T> {}
266-
const FIRST: usize = offset_of!((MatrixIndex, &mut Chunk), 0);
267-
const SECOND: usize = offset_of!((MatrixIndex, &mut Chunk), 1);
268-
const FIRST_PTR: usize = offset_of!((MatrixIndex, *mut Chunk), 0);
269-
const SECOND_PTR: usize = offset_of!((MatrixIndex, *mut Chunk), 1);
270-
const _: () = assert!(FIRST == FIRST_PTR);
271-
const _: () = assert!(SECOND == SECOND_PTR);
272265
let task_pool = ComputeTaskPool::get();
273266
let chunk_size = (self.len / task_pool.thread_num()).max(1);
274267
let f_ref = &f;
@@ -280,49 +273,41 @@ impl<T> MatrixBounded<T> {
280273
task_pool.scope(|scope| {
281274
for chunk_ptr in list.chunks_mut(chunk_size) {
282275
scope.spawn(async move {
283-
let chunk =
284-
unsafe { &mut *(ptr::from_mut(chunk_ptr) as *mut [(MatrixIndex, &mut T)]) };
285-
f_ref(chunk)
276+
let mut iter = chunk_ptr
277+
.iter_mut()
278+
.map(|(i, p)| (*i, unsafe { p.0.as_mut() }));
279+
f_ref(&mut iter)
286280
});
287281
}
288282
})
289283
}
290284
pub fn par_iter_zip_mut<R: Send + 'static, K>(
291285
&mut self,
292286
other: &mut MatrixBounded<K>,
293-
f: impl Fn(&mut [(MatrixIndex, &mut T, &mut K)]) -> R + Send + Sync,
287+
f: impl Fn(&mut dyn Iterator<Item = (MatrixIndex, &mut T, &mut K)>) -> R + Send + Sync,
294288
) -> Vec<R> {
295289
#[repr(transparent)]
296-
struct SendPtr<T>(*mut T);
290+
struct SendPtr<T>(NonNull<T>);
297291
unsafe impl<T> Send for SendPtr<T> {}
298292
unsafe impl<T> Sync for SendPtr<T> {}
299-
const FIRST: usize = offset_of!((MatrixIndex, &mut Chunk, &mut VoxelChunk), 0);
300-
const SECOND: usize = offset_of!((MatrixIndex, &mut Chunk, &mut VoxelChunk), 1);
301-
const THIRD: usize = offset_of!((MatrixIndex, &mut Chunk, &mut VoxelChunk), 2);
302-
const FIRST_PTR: usize = offset_of!((MatrixIndex, *mut Chunk, *mut VoxelChunk), 0);
303-
const SECOND_PTR: usize = offset_of!((MatrixIndex, *mut Chunk, *mut VoxelChunk), 1);
304-
const THIRD_PTR: usize = offset_of!((MatrixIndex, *mut Chunk, *mut VoxelChunk), 2);
305-
const _: () = assert!(FIRST == FIRST_PTR);
306-
const _: () = assert!(SECOND == SECOND_PTR);
307-
const _: () = assert!(THIRD == THIRD_PTR);
308293
let task_pool = ComputeTaskPool::get();
309294
let chunk_size = (self.len / task_pool.thread_num()).max(1);
310295
let f_ref = &f;
311296
let mut list = Vec::with_capacity(self.len);
312297
list.extend(self.iter_mut().map(|(i, c)| {
313298
(
314299
i,
315-
SendPtr(ptr::from_mut(c)),
316-
SendPtr(ptr::from_mut(other[i].as_mut().unwrap())),
300+
SendPtr(NonNull::from_mut(c)),
301+
SendPtr(NonNull::from_mut(other[i].as_mut().unwrap())),
317302
)
318303
}));
319304
task_pool.scope(|scope| {
320305
for chunk_ptr in list.chunks_mut(chunk_size) {
321306
scope.spawn(async move {
322-
let chunk = unsafe {
323-
&mut *(ptr::from_mut(chunk_ptr) as *mut [(MatrixIndex, &mut T, &mut K)])
324-
};
325-
f_ref(chunk)
307+
let mut iter = chunk_ptr
308+
.iter_mut()
309+
.map(|(i, a, b)| (*i, unsafe { a.0.as_mut() }, unsafe { b.0.as_mut() }));
310+
f_ref(&mut iter)
326311
});
327312
}
328313
})

0 commit comments

Comments
 (0)