1
Fork 0

Auto merge of #86879 - YohDeadfall:stabilize-vec-shrink-to, r=dtolnay

Stabilize Vec<T>::shrink_to

This PR stabilizes `shrink_to` feature and closes the corresponding issue. The second point was addressed already, and no `panic!` should occur.

Closes #56431.
This commit is contained in:
bors 2021-08-08 19:37:02 +00:00
commit ad981d58e1
9 changed files with 8 additions and 16 deletions

View file

@ -973,7 +973,6 @@ impl<T> BinaryHeap<T> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(shrink_to)]
/// use std::collections::BinaryHeap; /// use std::collections::BinaryHeap;
/// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100); /// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100);
/// ///
@ -982,7 +981,7 @@ impl<T> BinaryHeap<T> {
/// assert!(heap.capacity() >= 10); /// assert!(heap.capacity() >= 10);
/// ``` /// ```
#[inline] #[inline]
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
self.data.shrink_to(min_capacity) self.data.shrink_to(min_capacity)
} }

View file

@ -816,7 +816,6 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(shrink_to)]
/// use std::collections::VecDeque; /// use std::collections::VecDeque;
/// ///
/// let mut buf = VecDeque::with_capacity(15); /// let mut buf = VecDeque::with_capacity(15);
@ -827,7 +826,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// buf.shrink_to(0); /// buf.shrink_to(0);
/// assert!(buf.capacity() >= 4); /// assert!(buf.capacity() >= 4);
/// ``` /// ```
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
let min_capacity = cmp::min(min_capacity, self.capacity()); let min_capacity = cmp::min(min_capacity, self.capacity());
// We don't have to worry about an overflow as neither `self.len()` nor `self.capacity()` // We don't have to worry about an overflow as neither `self.len()` nor `self.capacity()`

View file

@ -1100,7 +1100,6 @@ impl String {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(shrink_to)]
/// let mut s = String::from("foo"); /// let mut s = String::from("foo");
/// ///
/// s.reserve(100); /// s.reserve(100);
@ -1113,7 +1112,7 @@ impl String {
/// ``` /// ```
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[inline] #[inline]
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
self.vec.shrink_to(min_capacity) self.vec.shrink_to(min_capacity)
} }

View file

@ -947,7 +947,6 @@ impl<T, A: Allocator> Vec<T, A> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(shrink_to)]
/// let mut vec = Vec::with_capacity(10); /// let mut vec = Vec::with_capacity(10);
/// vec.extend([1, 2, 3]); /// vec.extend([1, 2, 3]);
/// assert_eq!(vec.capacity(), 10); /// assert_eq!(vec.capacity(), 10);
@ -957,7 +956,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// assert!(vec.capacity() >= 3); /// assert!(vec.capacity() >= 3);
/// ``` /// ```
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
if self.capacity() > min_capacity { if self.capacity() > min_capacity {
self.buf.shrink_to_fit(cmp::max(self.len, min_capacity)); self.buf.shrink_to_fit(cmp::max(self.len, min_capacity));

View file

@ -667,7 +667,6 @@ where
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(shrink_to)]
/// use std::collections::HashMap; /// use std::collections::HashMap;
/// ///
/// let mut map: HashMap<i32, i32> = HashMap::with_capacity(100); /// let mut map: HashMap<i32, i32> = HashMap::with_capacity(100);
@ -680,7 +679,7 @@ where
/// assert!(map.capacity() >= 2); /// assert!(map.capacity() >= 2);
/// ``` /// ```
#[inline] #[inline]
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
self.base.shrink_to(min_capacity); self.base.shrink_to(min_capacity);
} }

View file

@ -464,7 +464,6 @@ where
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(shrink_to)]
/// use std::collections::HashSet; /// use std::collections::HashSet;
/// ///
/// let mut set = HashSet::with_capacity(100); /// let mut set = HashSet::with_capacity(100);
@ -477,7 +476,7 @@ where
/// assert!(set.capacity() >= 2); /// assert!(set.capacity() >= 2);
/// ``` /// ```
#[inline] #[inline]
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
self.base.shrink_to(min_capacity) self.base.shrink_to(min_capacity)
} }

View file

@ -321,7 +321,6 @@ impl OsString {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// #![feature(shrink_to)]
/// use std::ffi::OsString; /// use std::ffi::OsString;
/// ///
/// let mut s = OsString::from("foo"); /// let mut s = OsString::from("foo");
@ -335,7 +334,7 @@ impl OsString {
/// assert!(s.capacity() >= 3); /// assert!(s.capacity() >= 3);
/// ``` /// ```
#[inline] #[inline]
#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
self.inner.shrink_to(min_capacity) self.inner.shrink_to(min_capacity)
} }

View file

@ -308,7 +308,6 @@
#![feature(ptr_internals)] #![feature(ptr_internals)]
#![feature(rustc_attrs)] #![feature(rustc_attrs)]
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(shrink_to)]
#![feature(slice_concat_ext)] #![feature(slice_concat_ext)]
#![feature(slice_internals)] #![feature(slice_internals)]
#![feature(slice_ptr_get)] #![feature(slice_ptr_get)]

View file

@ -1398,7 +1398,7 @@ impl PathBuf {
/// Invokes [`shrink_to`] on the underlying instance of [`OsString`]. /// Invokes [`shrink_to`] on the underlying instance of [`OsString`].
/// ///
/// [`shrink_to`]: OsString::shrink_to /// [`shrink_to`]: OsString::shrink_to
#[unstable(feature = "shrink_to", issue = "56431")] #[stable(feature = "shrink_to", since = "1.56.0")]
#[inline] #[inline]
pub fn shrink_to(&mut self, min_capacity: usize) { pub fn shrink_to(&mut self, min_capacity: usize) {
self.inner.shrink_to(min_capacity) self.inner.shrink_to(min_capacity)