From 6233f3f4a391b9ef562e77dc4fb857ffaa4ca410 Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Sat, 20 Feb 2021 15:22:48 +0200 Subject: [PATCH 1/3] alloc: Added `as_slice` method to `BinaryHeap` collection --- library/alloc/src/collections/binary_heap.rs | 23 ++++++++++++++++++++ library/alloc/tests/lib.rs | 1 + 2 files changed, 24 insertions(+) diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index 8a36b2af76522..7a43a3a868b77 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -889,6 +889,29 @@ impl BinaryHeap { 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; + /// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]); + /// let slice = heap.as_slice(); + /// + /// // Will print in some order + /// for x in slice { + /// println!("{}", x); + /// } + /// ``` + #[unstable(feature = "binary_heap_as_slice", issue = "82331")] + pub fn as_slice(&self) -> &[T] { + self.data.as_slice() + } + /// Consumes the `BinaryHeap` and returns the underlying vector /// in arbitrary order. /// diff --git a/library/alloc/tests/lib.rs b/library/alloc/tests/lib.rs index dd98f806451d8..4679121c929f8 100644 --- a/library/alloc/tests/lib.rs +++ b/library/alloc/tests/lib.rs @@ -15,6 +15,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(int_bits_const)] From dd2b8a0444a957b9d1d6481abd38b025c855d79d Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Sat, 13 Mar 2021 17:21:56 +0200 Subject: [PATCH 2/3] provide a more realistic example for BinaryHeap::as_slice --- library/alloc/src/collections/binary_heap.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index 7a43a3a868b77..ed32ff496c65a 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -899,13 +899,11 @@ impl BinaryHeap { /// ``` /// #![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]); - /// let slice = heap.as_slice(); /// - /// // Will print in some order - /// for x in slice { - /// println!("{}", x); - /// } + /// io::sink().write(heap.as_slice()).unwrap(); /// ``` #[unstable(feature = "binary_heap_as_slice", issue = "82331")] pub fn as_slice(&self) -> &[T] { From 595f3f25fcc9e11598eb0de2b8bb01022386147c Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Mon, 29 Mar 2021 22:44:48 +0300 Subject: [PATCH 3/3] Updated the tracking issue # --- library/alloc/src/collections/binary_heap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index ed32ff496c65a..902ef9b123ec2 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -905,7 +905,7 @@ impl BinaryHeap { /// /// io::sink().write(heap.as_slice()).unwrap(); /// ``` - #[unstable(feature = "binary_heap_as_slice", issue = "82331")] + #[unstable(feature = "binary_heap_as_slice", issue = "83659")] pub fn as_slice(&self) -> &[T] { self.data.as_slice() }