Skip to content

Commit f52082f

Browse files
committed
Update documentation
1 parent f81b07e commit f52082f

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

library/alloc/src/vec/in_place_collect.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
//! This is handled by the [`InPlaceDrop`] guard for sink items (`U`) and by
5656
//! [`vec::IntoIter::forget_allocation_drop_remaining()`] for remaining source items (`T`).
5757
//!
58+
//! If dropping any remaining source item (`T`) panics then [`InPlaceDstBufDrop`] will handle dropping
59+
//! the already collected sink items (`U`) and freeing the allocation.
60+
//!
5861
//! [`vec::IntoIter::forget_allocation_drop_remaining()`]: super::IntoIter::forget_allocation_drop_remaining()
5962
//!
6063
//! # O(1) collect
@@ -194,8 +197,8 @@ where
194197
// Drop any remaining values at the tail of the source but prevent drop of the allocation
195198
// itself once IntoIter goes out of scope.
196199
// If the drop panics then we also try to drop the destination buffer and its elements.
197-
// This is safe because `forget_allocation_drop_remaining` forgets the allocation *before*
198-
// trying to drop the remaining elements.
200+
// This is safe because `forget_allocation_drop_remaining` immediately forgets the allocation
201+
// and won't panic before that.
199202
//
200203
// Note: This access to the source wouldn't be allowed by the TrustedRandomIteratorNoCoerce
201204
// contract (used by SpecInPlaceCollect below). But see the "O(1) collect" section in the

library/alloc/src/vec/into_iter.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,16 @@ impl<T, A: Allocator> IntoIter<T, A> {
9696
}
9797

9898
/// Drops remaining elements and relinquishes the backing allocation.
99+
/// This method guarantees it won't panic before relinquishing
100+
/// the backing allocation.
99101
///
100102
/// This is roughly equivalent to the following, but more efficient
101103
///
102104
/// ```
103105
/// # let mut into_iter = Vec::<u8>::with_capacity(10).into_iter();
106+
/// let mut into_iter = std::mem::replace(&mut into_iter, Vec::new().into_iter());
104107
/// (&mut into_iter).for_each(core::mem::drop);
105-
/// unsafe { core::ptr::write(&mut into_iter, Vec::new().into_iter()); }
108+
/// std::mem::forget(into_iter);
106109
/// ```
107110
///
108111
/// This method is used by in-place iteration, refer to the vec::in_place_collect
@@ -119,6 +122,8 @@ impl<T, A: Allocator> IntoIter<T, A> {
119122
self.ptr = self.buf.as_ptr();
120123
self.end = self.buf.as_ptr();
121124

125+
// Dropping the remaining elements can panic, so this needs to be
126+
// done only after updating the other fields.
122127
unsafe {
123128
ptr::drop_in_place(remaining);
124129
}

0 commit comments

Comments
 (0)