Skip to content

Commit 61eb0e6

Browse files
committed
Add shrink_to() method, stabilized in Rust 1.56.0
See rust-lang/rust#86879.
1 parent 9bd3739 commit 61eb0e6

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ fn main() {
44
// Required for stabilization of `unsafe_op_in_unsafe_fn` lint.
55
ac.emit_rustc_version(1, 52);
66

7+
// Required for stabilization of `Vec::shrink_to()`.
8+
ac.emit_rustc_version(1, 56);
9+
710
autocfg::rerun_path("build.rs");
811
}

src/binary_heap.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,33 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
911911
self.data.shrink_to_fit();
912912
}
913913

914+
/// Discards capacity with a lower bound.
915+
///
916+
/// The capacity will remain at least as large as both the length
917+
/// and the supplied value.
918+
///
919+
/// If the current capacity is less than the lower limit, this is a no-op.
920+
///
921+
/// # Examples
922+
///
923+
/// ```
924+
/// use std::collections::BinaryHeap;
925+
/// let mut heap: BinaryHeap<i32> = BinaryHeap::with_capacity(100);
926+
///
927+
/// assert!(heap.capacity() >= 100);
928+
/// heap.shrink_to(10);
929+
/// assert!(heap.capacity() >= 10);
930+
/// ```
931+
///
932+
/// # Compatibility
933+
///
934+
/// This feature requires Rust 1.56.0 or greater.
935+
#[inline]
936+
#[cfg(rustc_1_56)]
937+
pub fn shrink_to(&mut self, min_capacity: usize) {
938+
self.data.shrink_to(min_capacity)
939+
}
940+
914941
/// Removes the greatest item from the binary heap and returns it, or `None` if it
915942
/// is empty.
916943
///

0 commit comments

Comments
 (0)