about summary refs log tree commit diff
path: root/library/alloc/src/collections
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-03-30 00:32:18 +0200
committerGitHub <noreply@github.com>2021-03-30 00:32:18 +0200
commit2843baaeb6e20c494b3b938b4d4024ead7bbdd1b (patch)
tree903506aa3d82a267877caae41513a1bf19ec9238 /library/alloc/src/collections
parent48691ea6e639640f110b43e33d4aba1f07e7415c (diff)
parent595f3f25fcc9e11598eb0de2b8bb01022386147c (diff)
downloadrust-2843baaeb6e20c494b3b938b4d4024ead7bbdd1b.tar.gz
rust-2843baaeb6e20c494b3b938b4d4024ead7bbdd1b.zip
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
Diffstat (limited to 'library/alloc/src/collections')
-rw-r--r--library/alloc/src/collections/binary_heap.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs
index b5e66d37ab4..bf9f7432fb5 100644
--- a/library/alloc/src/collections/binary_heap.rs
+++ b/library/alloc/src/collections/binary_heap.rs
@@ -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.
     ///