Skip to content

Commit

Permalink
Use array::map instead of transmute in get_many (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
goffrie authored Jan 25, 2025
1 parent 6edd894 commit 2e5d917
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/vector/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,13 @@ where
match self {
FocusMut::Single(_, chunk) => {
// FIXME: Stable polyfill for std `get_many_mut`
let chunk: *mut A = ptr::from_mut(*chunk).cast();
let mut arr = [const { MaybeUninit::uninit() }; N];
for idx in 0..N {
arr[idx].write(unsafe { &mut *chunk.add(indices[idx]) });
}
unsafe { mem::transmute_copy(&arr) }
let chunk: *mut A = (*chunk).as_mut_ptr();
Some(indices.map(|index| {
// Safety:
// - `check_indices` ensures each index is `< self.len()`, which for `FocusMut::Single` is `chunk.len()`
// - `check_indices` ensures the indexes do not overlap
unsafe { &mut *chunk.add(index) }
}))
}
FocusMut::Full(pool, tree) => tree.get_many(pool, indices),
}
Expand Down Expand Up @@ -980,18 +981,19 @@ where
indices: [usize; N],
) -> Option<[&mut A; N]> {
check_indices(self.len(), &indices)?;
let mut arr = [const { MaybeUninit::uninit() }; N];
for idx in 0..N {
let phys_idx = self.physical_index(indices[idx]);
Some(indices.map(|phys_idx| {
if !contains(&self.target_range, &phys_idx) {
self.set_focus(pool, phys_idx);
}
let target_idx = phys_idx - self.target_range.start;
let chunk = self.get_focus_ptr();
let ptr = unsafe { Chunk::as_mut_slice_ptr(chunk) };
arr[idx].write(unsafe { &mut *ptr.cast::<A>().add(target_idx) });
}
unsafe { mem::transmute_copy(&arr) }
// Safety: we have called `set_focus` to get a valid chunk pointer
// and `target_idx` lies within it
unsafe {
let chunk = self.get_focus_ptr();
let ptr: *mut [A] = Chunk::as_mut_slice_ptr(chunk);
&mut *ptr.cast::<A>().add(target_idx)
}
}))
}

/// Gets the chunk for an index as a slice and its corresponding range within the TreeFocusMut.
Expand Down

0 comments on commit 2e5d917

Please sign in to comment.