diff options
| author | Caio <c410.f3r@gmail.com> | 2021-09-30 08:40:05 -0300 |
|---|---|---|
| committer | Caio <c410.f3r@gmail.com> | 2021-09-30 08:40:05 -0300 |
| commit | fdccc7dad9f9d4227ce07189585cc5214868bc28 (patch) | |
| tree | c4eb0ef9735880da0954b2a539e31917695394bb /library/core/src/array | |
| parent | 4be574e6c91e004c13e133ea3a441e13aa2439d3 (diff) | |
| download | rust-fdccc7dad9f9d4227ce07189585cc5214868bc28.tar.gz rust-fdccc7dad9f9d4227ce07189585cc5214868bc28.zip | |
Use reference instead of raw pointer
Diffstat (limited to 'library/core/src/array')
| -rw-r--r-- | library/core/src/array/mod.rs | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index b823c33ee23..3fa63cf968f 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -559,16 +559,17 @@ where return unsafe { Some(Ok(mem::zeroed())) }; } - struct Guard<T, const N: usize> { - ptr: *mut T, + struct Guard<'a, T, const N: usize> { + array_mut: &'a mut [MaybeUninit<T>; N], initialized: usize, } - impl<T, const N: usize> Drop for Guard<T, N> { + impl<T, const N: usize> Drop for Guard<'_, T, N> { fn drop(&mut self) { debug_assert!(self.initialized <= N); - let initialized_part = crate::ptr::slice_from_raw_parts_mut(self.ptr, self.initialized); + let ptr = MaybeUninit::slice_as_mut_ptr(self.array_mut); + let initialized_part = crate::ptr::slice_from_raw_parts_mut(ptr, self.initialized); // SAFETY: this raw slice will contain only initialized objects. unsafe { @@ -578,8 +579,7 @@ where } let mut array = MaybeUninit::uninit_array::<N>(); - let mut guard: Guard<_, N> = - Guard { ptr: MaybeUninit::slice_as_mut_ptr(&mut array), initialized: 0 }; + let mut guard: Guard<'_, _, N> = Guard { array_mut: &mut array, initialized: 0 }; while let Some(item_rslt) = iter.next() { let item = match item_rslt { @@ -593,7 +593,7 @@ where // loop and the loop is aborted once it reaches N (which is // `array.len()`). unsafe { - array.get_unchecked_mut(guard.initialized).write(item); + guard.array_mut.get_unchecked_mut(guard.initialized).write(item); } guard.initialized += 1; |
