Simplify implementation of `as_slice` and `as_mut_slice`
@@ -212,15 +212,16 @@ impl<T, const N: usize> StaticVec<T, { N }> {
///Returns a constant reference to a slice of the StaticVec's inhabited area.
#[inline(always)]
pub fn as_slice(&self) -> &[T] {
- unsafe { &*(self.data.get_unchecked(0..self.length) as *const [MaybeUninit<T>] as *const [T]) }
+ // Safety: Self is an array, and is guaranteed that the first `length`
+ // elements are initialized. Therefore this is a valid slice.
+ unsafe { slice::from_raw_parts(self.as_ptr(), self.length) }
}
///Returns a mutable reference to a slice of the StaticVec's inhabited area.
#[inline(always)]
pub fn as_mut_slice(&mut self) -> &mut [T] {
- unsafe {
- &mut *(self.data.get_unchecked_mut(0..self.length) as *mut [MaybeUninit<T>] as *mut [T])
- }
+ // Safety: See as_slice
+ unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.length) }
}
///Returns a constant reference to the element of the StaticVec at `index`, if `index` is within the range `0..length`.
