Skip to content

Commit

Permalink
Rollup merge of #82331 - frol:feat/std-binary-heap-as-slice, r=Amanieu
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dylan-DPC committed Mar 29, 2021
2 parents 48691ea + 595f3f2 commit 2843baa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,27 @@ impl<T> BinaryHeap<T> {
self.data.shrink_to(min_capacity)
}

/// Returns a slice of all values in the underlying vector, in arbitrary
/// order.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(binary_heap_as_slice)]
/// use std::collections::BinaryHeap;
/// use std::io::{self, Write};
///
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
///
/// io::sink().write(heap.as_slice()).unwrap();
/// ```
#[unstable(feature = "binary_heap_as_slice", issue = "83659")]
pub fn as_slice(&self) -> &[T] {
self.data.as_slice()
}

/// Consumes the `BinaryHeap` and returns the underlying vector
/// in arbitrary order.
///
Expand Down
1 change: 1 addition & 0 deletions library/alloc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![feature(binary_heap_drain_sorted)]
#![feature(slice_ptr_get)]
#![feature(binary_heap_retain)]
#![feature(binary_heap_as_slice)]
#![feature(inplace_iteration)]
#![feature(iter_map_while)]
#![feature(vecdeque_binary_search)]
Expand Down

0 comments on commit 2843baa

Please sign in to comment.