Skip to content

Commit c185c4f

Browse files
authored
Rollup merge of #70776 - RalfJung:raw-vec, r=Dylan-DPC,TimDiekmann
clarify comment in RawVec::into_box On first reading I almost thought "len <= cap" would be all that there is to check here. Expand the comment to clarify that that is not the case.
2 parents 2448a23 + 6cbe172 commit c185c4f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/liballoc/raw_vec.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -570,16 +570,19 @@ impl<T> RawVec<T, Global> {
570570
///
571571
/// # Safety
572572
///
573-
/// `shrink_to_fit(len)` must be called immediately prior to calling this function. This
574-
/// implies, that `len` must be smaller than or equal to `self.capacity()`.
573+
/// * `len` must be greater than or equal to the most recently requested capacity, and
574+
/// * `len` must be less than or equal to `self.capacity()`.
575+
///
576+
/// Note, that the requested capacity and `self.capacity()` could differ, as
577+
/// an allocator could overallocate and return a greater memory block than requested.
575578
pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>]> {
579+
// Sanity-check one half of the safety requirement (we cannot check the other half).
576580
debug_assert!(
577581
len <= self.capacity(),
578582
"`len` must be smaller than or equal to `self.capacity()`"
579583
);
580584

581585
let me = ManuallyDrop::new(self);
582-
// NOTE: not calling `capacity()` here; actually using the real `cap` field!
583586
let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
584587
Box::from_raw(slice)
585588
}

0 commit comments

Comments
 (0)