Skip to content

Commit fdccc7d

Browse files
committed
Use reference instead of raw pointer
1 parent 4be574e commit fdccc7d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

library/core/src/array/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -559,16 +559,17 @@ where
559559
return unsafe { Some(Ok(mem::zeroed())) };
560560
}
561561

562-
struct Guard<T, const N: usize> {
563-
ptr: *mut T,
562+
struct Guard<'a, T, const N: usize> {
563+
array_mut: &'a mut [MaybeUninit<T>; N],
564564
initialized: usize,
565565
}
566566

567-
impl<T, const N: usize> Drop for Guard<T, N> {
567+
impl<T, const N: usize> Drop for Guard<'_, T, N> {
568568
fn drop(&mut self) {
569569
debug_assert!(self.initialized <= N);
570570

571-
let initialized_part = crate::ptr::slice_from_raw_parts_mut(self.ptr, self.initialized);
571+
let ptr = MaybeUninit::slice_as_mut_ptr(self.array_mut);
572+
let initialized_part = crate::ptr::slice_from_raw_parts_mut(ptr, self.initialized);
572573

573574
// SAFETY: this raw slice will contain only initialized objects.
574575
unsafe {
@@ -578,8 +579,7 @@ where
578579
}
579580

580581
let mut array = MaybeUninit::uninit_array::<N>();
581-
let mut guard: Guard<_, N> =
582-
Guard { ptr: MaybeUninit::slice_as_mut_ptr(&mut array), initialized: 0 };
582+
let mut guard: Guard<'_, _, N> = Guard { array_mut: &mut array, initialized: 0 };
583583

584584
while let Some(item_rslt) = iter.next() {
585585
let item = match item_rslt {
@@ -593,7 +593,7 @@ where
593593
// loop and the loop is aborted once it reaches N (which is
594594
// `array.len()`).
595595
unsafe {
596-
array.get_unchecked_mut(guard.initialized).write(item);
596+
guard.array_mut.get_unchecked_mut(guard.initialized).write(item);
597597
}
598598
guard.initialized += 1;
599599

0 commit comments

Comments
 (0)