Skip to content

Commit 2843baa

Browse files
authored
Rollup merge of #82331 - frol:feat/std-binary-heap-as-slice, r=Amanieu
alloc: Added `as_slice` method to `BinaryHeap` collection I initially asked about whether it is useful addition on https://internals.rust-lang.org/t/should-i-add-as-slice-method-to-binaryheap/13816, and it seems there were no objections, so went ahead with this PR. > There is [`BinaryHeap::into_vec`](https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#method.into_vec), but it consumes the value. I wonder if there is API design limitation that should be taken into account. Implementation-wise, the inner buffer is just a Vec, so it is trivial to expose as_slice from it. Please, guide me through if I need to add tests or something else. UPD: Tracking issue #83659
2 parents 48691ea + 595f3f2 commit 2843baa

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

library/alloc/src/collections/binary_heap.rs

+21
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,27 @@ impl<T> BinaryHeap<T> {
958958
self.data.shrink_to(min_capacity)
959959
}
960960

961+
/// Returns a slice of all values in the underlying vector, in arbitrary
962+
/// order.
963+
///
964+
/// # Examples
965+
///
966+
/// Basic usage:
967+
///
968+
/// ```
969+
/// #![feature(binary_heap_as_slice)]
970+
/// use std::collections::BinaryHeap;
971+
/// use std::io::{self, Write};
972+
///
973+
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
974+
///
975+
/// io::sink().write(heap.as_slice()).unwrap();
976+
/// ```
977+
#[unstable(feature = "binary_heap_as_slice", issue = "83659")]
978+
pub fn as_slice(&self) -> &[T] {
979+
self.data.as_slice()
980+
}
981+
961982
/// Consumes the `BinaryHeap` and returns the underlying vector
962983
/// in arbitrary order.
963984
///

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(binary_heap_drain_sorted)]
1515
#![feature(slice_ptr_get)]
1616
#![feature(binary_heap_retain)]
17+
#![feature(binary_heap_as_slice)]
1718
#![feature(inplace_iteration)]
1819
#![feature(iter_map_while)]
1920
#![feature(vecdeque_binary_search)]

0 commit comments

Comments
 (0)