Skip to content

Commit 0a98071

Browse files
authored
Rollup merge of rust-lang#91529 - TennyZhuang:try_reserve_binary_heap, r=yaahc
add BinaryHeap::try_reserve and BinaryHeap::try_reserve_exact `try_reserve` of many collections were stablized in rust-lang#87993 in 1.57.0. Add `try_reserve` for the rest collections such as `BinaryHeap` should be not controversial.
2 parents 672f836 + 2235af1 commit 0a98071

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

library/alloc/src/collections/binary_heap.rs

+79
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ use core::mem::{self, swap, ManuallyDrop};
149149
use core::ops::{Deref, DerefMut};
150150
use core::ptr;
151151

152+
use crate::collections::TryReserveError;
152153
use crate::slice;
153154
use crate::vec::{self, AsIntoIter, Vec};
154155

@@ -953,6 +954,84 @@ impl<T> BinaryHeap<T> {
953954
self.data.reserve(additional);
954955
}
955956

957+
/// Tries to reserve the minimum capacity for exactly `additional`
958+
/// elements to be inserted in the given `BinaryHeap<T>`. After calling
959+
/// `try_reserve_exact`, capacity will be greater than or equal to
960+
/// `self.len() + additional` if it returns `Ok(())`.
961+
/// Does nothing if the capacity is already sufficient.
962+
///
963+
/// Note that the allocator may give the collection more space than it
964+
/// requests. Therefore, capacity can not be relied upon to be precisely
965+
/// minimal. Prefer [`try_reserve`] if future insertions are expected.
966+
///
967+
/// [`try_reserve`]: BinaryHeap::try_reserve
968+
///
969+
/// # Errors
970+
///
971+
/// If the capacity overflows, or the allocator reports a failure, then an error
972+
/// is returned.
973+
///
974+
/// # Examples
975+
///
976+
/// ```
977+
/// #![feature(try_reserve_2)]
978+
/// use std::collections::BinaryHeap;
979+
/// use std::collections::TryReserveError;
980+
///
981+
/// fn find_max_slow(data: &[u32]) -> Result<Option<u32>, TryReserveError> {
982+
/// let mut heap = BinaryHeap::new();
983+
///
984+
/// // Pre-reserve the memory, exiting if we can't
985+
/// heap.try_reserve_exact(data.len())?;
986+
///
987+
/// // Now we know this can't OOM in the middle of our complex work
988+
/// heap.extend(data.iter());
989+
///
990+
/// Ok(heap.pop())
991+
/// }
992+
/// # find_max_slow(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
993+
/// ```
994+
#[unstable(feature = "try_reserve_2", issue = "91789")]
995+
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
996+
self.data.try_reserve_exact(additional)
997+
}
998+
999+
/// Tries to reserve capacity for at least `additional` more elements to be inserted
1000+
/// in the given `BinaryHeap<T>`. The collection may reserve more space to avoid
1001+
/// frequent reallocations. After calling `try_reserve`, capacity will be
1002+
/// greater than or equal to `self.len() + additional`. Does nothing if
1003+
/// capacity is already sufficient.
1004+
///
1005+
/// # Errors
1006+
///
1007+
/// If the capacity overflows, or the allocator reports a failure, then an error
1008+
/// is returned.
1009+
///
1010+
/// # Examples
1011+
///
1012+
/// ```
1013+
/// #![feature(try_reserve_2)]
1014+
/// use std::collections::BinaryHeap;
1015+
/// use std::collections::TryReserveError;
1016+
///
1017+
/// fn find_max_slow(data: &[u32]) -> Result<Option<u32>, TryReserveError> {
1018+
/// let mut heap = BinaryHeap::new();
1019+
///
1020+
/// // Pre-reserve the memory, exiting if we can't
1021+
/// heap.try_reserve(data.len())?;
1022+
///
1023+
/// // Now we know this can't OOM in the middle of our complex work
1024+
/// heap.extend(data.iter());
1025+
///
1026+
/// Ok(heap.pop())
1027+
/// }
1028+
/// # find_max_slow(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?");
1029+
/// ```
1030+
#[unstable(feature = "try_reserve_2", issue = "91789")]
1031+
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1032+
self.data.try_reserve(additional)
1033+
}
1034+
9561035
/// Discards as much additional capacity as possible.
9571036
///
9581037
/// # Examples

0 commit comments

Comments
 (0)