Skip to content

Commit 099241d

Browse files
bors[bot]cuviper
andauthored
1030: Be more cautious about drain drops r=cuviper a=cuviper This makes a greater effort to ensure that in all cases where either `DrainProducer` or `SliceDrain` will drop data, there are no references left pointing to that memory. It's not actually clear that this would be a problem anyway, as long as there's nothing accessing that zombie data, but it's not too much trouble to avoid it. Fixes rayon-rs#1029. Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents 7069695 + a17bcb9 commit 099241d

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/array.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ impl<T: Send, const N: usize> IndexedParallelIterator for IntoIter<T, N> {
7878
unsafe {
7979
// Drain every item, and then the local array can just fall out of scope.
8080
let mut array = ManuallyDrop::new(self.array);
81-
callback.callback(DrainProducer::new(&mut *array))
81+
let producer = DrainProducer::new(array.as_mut_slice());
82+
callback.callback(producer)
8283
}
8384
}
8485
}

src/vec.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ impl<'data, T: 'data + Send> Producer for DrainProducer<'data, T> {
225225

226226
impl<'data, T: 'data + Send> Drop for DrainProducer<'data, T> {
227227
fn drop(&mut self) {
228-
// use `Drop for [T]`
229-
unsafe { ptr::drop_in_place(self.slice) };
228+
// extract the slice so we can use `Drop for [T]`
229+
let slice_ptr: *mut [T] = mem::take::<&'data mut [T]>(&mut self.slice);
230+
unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
230231
}
231232
}
232233

@@ -276,7 +277,7 @@ impl<'data, T: 'data> iter::FusedIterator for SliceDrain<'data, T> {}
276277
impl<'data, T: 'data> Drop for SliceDrain<'data, T> {
277278
fn drop(&mut self) {
278279
// extract the iterator so we can use `Drop for [T]`
279-
let iter = mem::replace(&mut self.iter, [].iter_mut());
280-
unsafe { ptr::drop_in_place(iter.into_slice()) };
280+
let slice_ptr: *mut [T] = mem::replace(&mut self.iter, [].iter_mut()).into_slice();
281+
unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
281282
}
282283
}

0 commit comments

Comments
 (0)