7575
7676#[ cfg( not( no_global_oom_handling) ) ]
7777use core:: clone:: TrivialClone ;
78- #[ cfg( not( no_global_oom_handling) ) ]
79- use core:: cmp;
8078use core:: cmp:: Ordering ;
8179use core:: hash:: { Hash , Hasher } ;
8280#[ cfg( not( no_global_oom_handling) ) ]
@@ -88,7 +86,7 @@ use core::mem::{self, Assume, ManuallyDrop, MaybeUninit, SizedTypeProperties, Tr
8886use core:: ops:: { self , Index , IndexMut , Range , RangeBounds } ;
8987use core:: ptr:: { self , NonNull } ;
9088use core:: slice:: { self , SliceIndex } ;
91- use core:: { fmt, hint, intrinsics, ub_checks} ;
89+ use core:: { cmp , fmt, hint, intrinsics, ub_checks} ;
9290
9391#[ stable( feature = "extract_if" , since = "1.87.0" ) ]
9492pub use self :: extract_if:: ExtractIf ;
@@ -1613,6 +1611,73 @@ impl<T, A: Allocator> Vec<T, A> {
16131611 }
16141612 }
16151613
1614+ /// Tries to shrink the capacity of the vector as much as possible
1615+ ///
1616+ /// The behavior of this method depends on the allocator, which may either shrink the vector
1617+ /// in-place or reallocate. The resulting vector might still have some excess capacity, just as
1618+ /// is the case for [`with_capacity`]. See [`Allocator::shrink`] for more details.
1619+ ///
1620+ /// [`with_capacity`]: Vec::with_capacity
1621+ ///
1622+ /// # Errors
1623+ ///
1624+ /// This function returns an error if the allocator fails to shrink the allocation,
1625+ /// the vector thereafter is still safe to use, the capacity remains unchanged
1626+ /// however. See [`Allocator::shrink`].
1627+ ///
1628+ /// # Examples
1629+ ///
1630+ /// ```
1631+ /// #![feature(vec_fallible_shrink)]
1632+ ///
1633+ /// let mut vec = Vec::with_capacity(10);
1634+ /// vec.extend([1, 2, 3]);
1635+ /// assert!(vec.capacity() >= 10);
1636+ /// vec.try_shrink_to_fit().expect("why is the test harness failing to shrink to 12 bytes");
1637+ /// assert!(vec.capacity() >= 3);
1638+ /// ```
1639+ #[ unstable( feature = "vec_fallible_shrink" , issue = "152350" ) ]
1640+ #[ inline]
1641+ pub fn try_shrink_to_fit ( & mut self ) -> Result < ( ) , TryReserveError > {
1642+ if self . capacity ( ) > self . len { self . buf . try_shrink_to_fit ( self . len ) } else { Ok ( ( ) ) }
1643+ }
1644+
1645+ /// Shrinks the capacity of the vector with a lower bound.
1646+ ///
1647+ /// The capacity will remain at least as large as both the length
1648+ /// and the supplied value.
1649+ ///
1650+ /// If the current capacity is less than the lower limit, this is a no-op.
1651+ ///
1652+ /// # Errors
1653+ ///
1654+ /// This function returns an error if the allocator fails to shrink the allocation,
1655+ /// the vector thereafter is still safe to use, the capacity remains unchanged
1656+ /// however. See [`Allocator::shrink`].
1657+ ///
1658+ /// # Examples
1659+ ///
1660+ /// ```
1661+ /// #![feature(vec_fallible_shrink)]
1662+ ///
1663+ /// let mut vec = Vec::with_capacity(10);
1664+ /// vec.extend([1, 2, 3]);
1665+ /// assert!(vec.capacity() >= 10);
1666+ /// vec.try_shrink_to(4).expect("why is the test harness failing to shrink to 12 bytes");
1667+ /// assert!(vec.capacity() >= 4);
1668+ /// vec.try_shrink_to(0).expect("this is a no-op and thus the allocator isn't involved.");
1669+ /// assert!(vec.capacity() >= 3);
1670+ /// ```
1671+ #[ unstable( feature = "vec_fallible_shrink" , issue = "152350" ) ]
1672+ #[ inline]
1673+ pub fn try_shrink_to ( & mut self , min_capacity : usize ) -> Result < ( ) , TryReserveError > {
1674+ if self . capacity ( ) > min_capacity {
1675+ self . buf . try_shrink_to_fit ( cmp:: max ( self . len , min_capacity) )
1676+ } else {
1677+ Ok ( ( ) )
1678+ }
1679+ }
1680+
16161681 /// Converts the vector into [`Box<[T]>`][owned slice].
16171682 ///
16181683 /// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].
0 commit comments