Skip to content

Commit 111405b

Browse files
Revisit the implementation of a few of the "building block" functions
1 parent 9813e18 commit 111405b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
const_fn_trait_bound,
3737
const_intrinsic_copy,
3838
const_maybe_uninit_as_mut_ptr,
39+
const_maybe_uninit_as_ptr,
3940
const_maybe_uninit_assume_init,
4041
const_mut_refs,
4142
const_precise_live_drops,
@@ -459,7 +460,11 @@ impl<T, const N: usize> StaticVec<T, N> {
459460
/// ```
460461
#[inline(always)]
461462
pub const fn as_ptr(&self) -> *const T {
462-
Self::first_ptr(&self.data)
463+
// This is the same aliasing-avoidance / optimization-trick approach used by regular Vec.
464+
let res = self.data.as_ptr() as *const T;
465+
// A pointer to "element zero" of an array is never null.
466+
unsafe { assume(!ptr.is_null()); }
467+
res
463468
}
464469

465470
/// Returns a mutable pointer to the first element of the StaticVec's internal array.
@@ -477,7 +482,10 @@ impl<T, const N: usize> StaticVec<T, N> {
477482
/// ```
478483
#[inline(always)]
479484
pub const fn as_mut_ptr(&mut self) -> *mut T {
480-
Self::first_ptr_mut(&mut self.data)
485+
// See the comment above in `as_ptr()`.
486+
let res = self.data.as_mut_ptr() as *mut T;
487+
unsafe { assume(!ptr.is_null()); }
488+
res
481489
}
482490

483491
/// Returns a constant reference to a slice of the StaticVec's inhabited area.
@@ -2607,15 +2615,19 @@ impl<T, const N: usize> StaticVec<T, N> {
26072615
/// as opposed to slices.
26082616
#[inline(always)]
26092617
pub(crate) const fn first_ptr(this: &MaybeUninit<[T; N]>) -> *const T {
2610-
this as *const MaybeUninit<[T; N]> as *const T
2618+
let res = this.as_ptr() as *const T;
2619+
unsafe { assume(!ptr.is_null()); }
2620+
res
26112621
}
26122622

26132623
/// An internal convenience function to go from `&mut MaybeUninit<[T; N]>` to `*mut T`.
26142624
/// Similar to [`MaybeUninit::first_ptr_mut`](core::mem::MaybeUninit::first_ptr_mut), but for
26152625
/// arrays as opposed to slices.
26162626
#[inline(always)]
26172627
pub(crate) const fn first_ptr_mut(this: &mut MaybeUninit<[T; N]>) -> *mut T {
2618-
this as *mut MaybeUninit<[T; N]> as *mut T
2628+
let res = this.as_mut_ptr() as *mut T;
2629+
unsafe { assume(!ptr.is_null()); }
2630+
res
26192631
}
26202632
}
26212633

0 commit comments

Comments
 (0)