about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-04-05 13:13:13 +0200
committerGitHub <noreply@github.com>2020-04-05 13:13:13 +0200
commitc185c4fe4740227100c869da44825ca6eb80be74 (patch)
tree247b9f32cbfe6c6a3a43d8df7810f8ed8df9dd57 /src/liballoc
parent2448a235549cbfe063010af1b9ee8dc35eaa626b (diff)
parent6cbe1726a70bac498bd46dc08c868f094117675c (diff)
downloadrust-c185c4fe4740227100c869da44825ca6eb80be74.tar.gz
rust-c185c4fe4740227100c869da44825ca6eb80be74.zip
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.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/raw_vec.rs9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs
index 12e32fd9d35..7ac67870eb7 100644
--- a/src/liballoc/raw_vec.rs
+++ b/src/liballoc/raw_vec.rs
@@ -570,16 +570,19 @@ impl<T> RawVec<T, Global> {
     ///
     /// # Safety
     ///
-    /// `shrink_to_fit(len)` must be called immediately prior to calling this function. This
-    /// implies, that `len` must be smaller than or equal to `self.capacity()`.
+    /// * `len` must be greater than or equal to the most recently requested capacity, and
+    /// * `len` must be less than or equal to `self.capacity()`.
+    ///
+    /// Note, that the requested capacity and `self.capacity()` could differ, as
+    /// an allocator could overallocate and return a greater memory block than requested.
     pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>]> {
+        // Sanity-check one half of the safety requirement (we cannot check the other half).
         debug_assert!(
             len <= self.capacity(),
             "`len` must be smaller than or equal to `self.capacity()`"
         );
 
         let me = ManuallyDrop::new(self);
-        // NOTE: not calling `capacity()` here; actually using the real `cap` field!
         let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
         Box::from_raw(slice)
     }