1
Fork 0

Implement shrink_to method on collections

This commit is contained in:
Diggory Blake 2018-03-26 23:24:31 +01:00
parent f5631d9ac7
commit 04f6692aaf
13 changed files with 237 additions and 2 deletions

View file

@ -66,7 +66,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
use core::cmp::Ordering;
use core::cmp::{self, Ordering};
use core::fmt;
use core::hash::{self, Hash};
use core::intrinsics::{arith_offset, assume};
@ -586,6 +586,31 @@ impl<T> Vec<T> {
self.buf.shrink_to_fit(self.len);
}
/// Shrinks the capacity of the vector with a lower bound.
///
/// The capacity will remain at least as large as both the length
/// and the supplied value.
///
/// Panics if the current capacity is smaller than the supplied
/// minimum capacity.
///
/// # Examples
///
/// ```
/// #![feature(shrink_to)]
/// let mut vec = Vec::with_capacity(10);
/// vec.extend([1, 2, 3].iter().cloned());
/// assert_eq!(vec.capacity(), 10);
/// vec.shrink_to(4);
/// assert!(vec.capacity() >= 4);
/// vec.shrink_to(0);
/// assert!(vec.capacity() >= 3);
/// ```
#[unstable(feature = "shrink_to", reason = "new API", issue="0")]
pub fn shrink_to(&mut self, min_capacity: usize) {
self.buf.shrink_to_fit(cmp::max(self.len, min_capacity));
}
/// Converts the vector into [`Box<[T]>`][owned slice].
///
/// Note that this will drop any excess capacity.