Add some comments and refactor a little bit
@@ -4,16 +4,20 @@ use core::intrinsics;
use core::mem::MaybeUninit;
use core::ptr;
+/// An internal convenience function to go from *const MaybeUninit<[T; N> to *const T.
#[inline(always)]
-pub(crate) fn ptr_const<T, const N: usize>(this: &MaybeUninit<[T; N]>) -> *const T {
- this as *const _ as *const T
+pub(crate) fn ptr_const<T, const N: usize>(this: *const MaybeUninit<[T; N]>) -> *const T {
+ this as *const T
}
+/// An internal convenience function to go from *mut MaybeUninit<[T; N> to *mut T.
#[inline(always)]
-pub(crate) fn ptr_mut<T, const N: usize>(this: &mut MaybeUninit<[T; N]>) -> *mut T {
- this as *mut _ as *mut T
+pub(crate) fn ptr_mut<T, const N: usize>(this: *mut MaybeUninit<[T; N]>) -> *mut T {
+ this as *mut T
}
+/// An internal function for calculating pointer offsets as usizes, while accounting
+/// directly for possible ZSTs. This is used specifically in the iterator implementations.
#[inline(always)]
pub(crate) const fn distance_between<T>(dest: *const T, origin: *const T) -> usize {
match intrinsics::size_of::<T>() {
@@ -29,13 +33,11 @@ pub(crate) fn reverse_copy<T, const N: usize>(
length: usize,
this: *const MaybeUninit<[T; N]>,
) -> MaybeUninit<[T; N]>
-where
- T: Copy,
-{
+where T: Copy {
+ let mut i = length;
+ let src = ptr_const(this);
let mut res: MaybeUninit<[T; N]> = MaybeUninit::uninit();
- let src = this as *const T;
let mut dest = ptr_mut(&mut res);
- let mut i = length;
while i > 0 {
unsafe {
src.add(i - 1).copy_to_nonoverlapping(dest, 1);
