Skip to content

Commit 985036a

Browse files
A few more tweaks
1 parent c2819d9 commit 985036a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/heap/heap_helpers.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ impl<'a, T> StaticHeapHole<'a, T> {
118118
pub(crate) unsafe fn move_to(&mut self, index: usize) {
119119
debug_assert!(index != self.position);
120120
debug_assert!(index < self.data.len());
121-
let index_ptr = self.data.as_ptr().add(index);
122-
let hole_ptr = self.data.as_mut_ptr().add(self.position);
121+
// This avoids aliasing, per the most recent revision of BinaryHeap's code.
122+
let ptr = self.data.as_mut_ptr();
123+
let index_ptr: *const _ = ptr.add(index);
124+
let hole_ptr = ptr.add(self.position);
123125
index_ptr.copy_to_nonoverlapping(hole_ptr, 1);
124126
self.position = index;
125127
}

src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,9 @@ impl<T, const N: usize> StaticVec<T, N> {
462462
// This is the same aliasing-avoidance / optimization-trick approach used by regular Vec.
463463
let res = self.data.as_ptr() as *const T;
464464
// A pointer to "element zero" of an array is never null.
465-
unsafe { assume(!res.is_null()); }
465+
unsafe {
466+
assume(!res.is_null());
467+
}
466468
res
467469
}
468470

@@ -483,7 +485,9 @@ impl<T, const N: usize> StaticVec<T, N> {
483485
pub const fn as_mut_ptr(&mut self) -> *mut T {
484486
// See the comment above in `as_ptr()`.
485487
let res = self.data.as_mut_ptr() as *mut T;
486-
unsafe { assume(!res.is_null()); }
488+
unsafe {
489+
assume(!res.is_null());
490+
}
487491
res
488492
}
489493

@@ -2615,7 +2619,9 @@ impl<T, const N: usize> StaticVec<T, N> {
26152619
#[inline(always)]
26162620
pub(crate) const fn first_ptr(this: &MaybeUninit<[T; N]>) -> *const T {
26172621
let res = this.as_ptr() as *const T;
2618-
unsafe { assume(!res.is_null()); }
2622+
unsafe {
2623+
assume(!res.is_null());
2624+
}
26192625
res
26202626
}
26212627

@@ -2625,7 +2631,9 @@ impl<T, const N: usize> StaticVec<T, N> {
26252631
#[inline(always)]
26262632
pub(crate) const fn first_ptr_mut(this: &mut MaybeUninit<[T; N]>) -> *mut T {
26272633
let res = this.as_mut_ptr() as *mut T;
2628-
unsafe { assume(!res.is_null()); }
2634+
unsafe {
2635+
assume(!res.is_null());
2636+
}
26292637
res
26302638
}
26312639
}

src/utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ pub(crate) const fn zst_ptr_add<T>(ptr: *const T, count: usize) -> *const T {
9696
/// An internal convenience function for incrementing mutable ZST pointers by usize offsets.
9797
#[inline(always)]
9898
pub(crate) const fn zst_ptr_add_mut<T>(ptr: *mut T, count: usize) -> *mut T {
99-
debug_assert!(size_of::<T>() == 0, "`zst_ptr_add_mut` called on a non-ZST!");
99+
debug_assert!(
100+
size_of::<T>() == 0,
101+
"`zst_ptr_add_mut` called on a non-ZST!"
102+
);
100103
(ptr as *mut u8).wrapping_add(count) as *mut T
101104
}
102105

0 commit comments

Comments
 (0)