|
| 1 | +use crate::mem::{forget, replace, MaybeUninit}; |
| 2 | +use crate::ptr; |
| 3 | + |
| 4 | +/// The internal-use drop guard for implementing array methods. |
| 5 | +/// |
| 6 | +/// This is free to be changed whenever. Its purpose is not to provide a |
| 7 | +/// beautiful safe interface, but to make the unsafe details of `super`'s |
| 8 | +/// other methods slightly more obvious and have reduced code duplication. |
| 9 | +pub struct Guard<'a, T, const N: usize> { |
| 10 | + array_mut: &'a mut [MaybeUninit<T>; N], |
| 11 | + initialized: usize, |
| 12 | +} |
| 13 | + |
| 14 | +impl<'a, T, const N: usize> Guard<'a, T, N> { |
| 15 | + #[inline] |
| 16 | + pub fn new(buffer: &'a mut [MaybeUninit<T>; N]) -> Self { |
| 17 | + Self { array_mut: buffer, initialized: 0 } |
| 18 | + } |
| 19 | + |
| 20 | + #[inline] |
| 21 | + pub fn len(&self) -> usize { |
| 22 | + self.initialized |
| 23 | + } |
| 24 | + |
| 25 | + /// Initialize the next item |
| 26 | + /// |
| 27 | + /// # Safety |
| 28 | + /// |
| 29 | + /// Requires `self.len() < N`. |
| 30 | + #[inline] |
| 31 | + pub unsafe fn push_unchecked(&mut self, value: T) { |
| 32 | + debug_assert!(self.len() < N); |
| 33 | + // SAFETY: The precondition means we have space |
| 34 | + unsafe { |
| 35 | + self.array_mut.get_unchecked_mut(self.initialized).write(value); |
| 36 | + } |
| 37 | + self.initialized += 1; |
| 38 | + } |
| 39 | + |
| 40 | + /// Initialize the next `CHUNK` item(s) |
| 41 | + /// |
| 42 | + /// # Safety |
| 43 | + /// |
| 44 | + /// Requires `self.len() + CHUNK <= N`. |
| 45 | + #[inline] |
| 46 | + pub unsafe fn push_chunk_unchecked<const CHUNK: usize>(&mut self, chunk: [T; CHUNK]) { |
| 47 | + assert!(CHUNK <= N); |
| 48 | + debug_assert!(N - self.len() >= CHUNK); |
| 49 | + // SAFETY: The precondition means we have space |
| 50 | + unsafe { |
| 51 | + // Since we're going to write multiple items, make sure not to do so |
| 52 | + // via a `&mut MaybeUninit<T>`, as that would violate stacked borrows. |
| 53 | + let first = self.array_mut.as_mut_ptr(); |
| 54 | + let p = first.add(self.initialized).cast(); |
| 55 | + ptr::write(p, chunk); |
| 56 | + } |
| 57 | + self.initialized += CHUNK; |
| 58 | + } |
| 59 | + |
| 60 | + /// Read the whole buffer as an initialized array. |
| 61 | + /// |
| 62 | + /// This always de-initializes the original buffer -- even if `T: Copy`. |
| 63 | + /// |
| 64 | + /// # Safety |
| 65 | + /// |
| 66 | + /// Requires `self.len() == N`. |
| 67 | + #[inline] |
| 68 | + pub unsafe fn into_array_unchecked(self) -> [T; N] { |
| 69 | + debug_assert_eq!(self.len(), N); |
| 70 | + |
| 71 | + // This tells LLVM and MIRI that we don't care about the buffer after, |
| 72 | + // and the extra `undef` write is trivial for it to optimize away. |
| 73 | + let buffer = replace(self.array_mut, MaybeUninit::uninit_array()); |
| 74 | + |
| 75 | + // SAFETY: the condition above asserts that all elements are |
| 76 | + // initialized. |
| 77 | + let out = unsafe { MaybeUninit::array_assume_init(buffer) }; |
| 78 | + |
| 79 | + forget(self); |
| 80 | + |
| 81 | + out |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<T, const N: usize> Drop for Guard<'_, T, N> { |
| 86 | + fn drop(&mut self) { |
| 87 | + debug_assert!(self.initialized <= N); |
| 88 | + |
| 89 | + // SAFETY: this slice will contain only initialized objects. |
| 90 | + unsafe { |
| 91 | + crate::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut( |
| 92 | + &mut self.array_mut.get_unchecked_mut(..self.initialized), |
| 93 | + )); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments