From 596d1f2627844df90e44a598d5748c16b4be4007 Mon Sep 17 00:00:00 2001 From: chansuke Date: Sat, 21 Nov 2020 18:21:17 +0900 Subject: [PATCH 1/3] Add excess capacity description on Vec::leak --- library/alloc/src/vec/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index ede1601fa33cc..9781e81e27409 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1783,6 +1783,16 @@ impl Vec { /// static_ref[0] += 1; /// assert_eq!(static_ref, &[2, 2, 3]); /// ``` + /// + /// As well as `Vec::into_boxed_slice`, any excess capacity will be removed: + /// + /// ``` + /// let mut vec = Vec::with_capacity(10); + /// vec.extend([1, 2, 3].iter().cloned()); + /// assert_eq!(vec.capacity(), 10); + /// let slice = vec.leak(); + /// assert_eq!(slice.to_vec().capacity(), 3); + /// ``` #[stable(feature = "vec_leak", since = "1.47.0")] #[inline] pub fn leak<'a>(self) -> &'a mut [T] From 747e8fffd0c6ec5489d61b8c14dfd450886a04ad Mon Sep 17 00:00:00 2001 From: chansuke Date: Wed, 2 Dec 2020 20:38:49 +0900 Subject: [PATCH 2/3] Fix example --- library/alloc/src/vec/mod.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 9781e81e27409..e6a021d55c14a 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1784,14 +1784,20 @@ impl Vec { /// assert_eq!(static_ref, &[2, 2, 3]); /// ``` /// - /// As well as `Vec::into_boxed_slice`, any excess capacity will be removed: + /// Drop excess capacity: /// /// ``` - /// let mut vec = Vec::with_capacity(10); - /// vec.extend([1, 2, 3].iter().cloned()); - /// assert_eq!(vec.capacity(), 10); - /// let slice = vec.leak(); - /// assert_eq!(slice.to_vec().capacity(), 3); + /// let mut v = Vec::with_capacity(10); + /// v.extend([1, 2, 3].iter().cloned()); + /// let slice = v.clone().into_boxed_slice(); + /// let static_ref = Box::leak(slice); + /// + /// unsafe { + /// let p = v.as_mut_ptr(); + /// let len = v.len(); + /// let rebuilt = Vec::from_raw_parts(p, len, static_ref.len()); + /// assert_eq!(rebuilt.capacity(), 3); + /// } /// ``` #[stable(feature = "vec_leak", since = "1.47.0")] #[inline] From b2d873f5ec6287d65f727e8c13b174ec2ae4bd45 Mon Sep 17 00:00:00 2001 From: chansuke Date: Sun, 7 Feb 2021 10:50:54 +0900 Subject: [PATCH 3/3] Fix build error --- library/alloc/src/vec/mod.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index e6a021d55c14a..98abdfe09c428 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1790,13 +1790,11 @@ impl Vec { /// let mut v = Vec::with_capacity(10); /// v.extend([1, 2, 3].iter().cloned()); /// let slice = v.clone().into_boxed_slice(); - /// let static_ref = Box::leak(slice); /// /// unsafe { - /// let p = v.as_mut_ptr(); - /// let len = v.len(); - /// let rebuilt = Vec::from_raw_parts(p, len, static_ref.len()); - /// assert_eq!(rebuilt.capacity(), 3); + /// let p = slice.as_ptr(); + /// let rebuilt = std::slice::from_raw_parts(p, slice.len()); + /// assert_eq!(rebuilt.len(), 3); /// } /// ``` #[stable(feature = "vec_leak", since = "1.47.0")]