Skip to content

Commit 7f53e74

Browse files
committed
add BinaryHeap::into_vec
1 parent f3af76d commit 7f53e74

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/binary_heap.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub enum Max {}
7474
7575
pub struct BinaryHeap<T, K, const N: usize> {
7676
pub(crate) _kind: PhantomData<K>,
77-
pub(crate) data: Vec<T, N>,
77+
pub(crate) data: ManuallyDrop<Vec<T, N>>,
7878
}
7979

8080
impl<T, K, const N: usize> BinaryHeap<T, K, N> {
@@ -94,7 +94,7 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> {
9494
pub const fn new() -> Self {
9595
Self {
9696
_kind: PhantomData,
97-
data: Vec::new(),
97+
data: ManuallyDrop::new(Vec::new()),
9898
}
9999
}
100100
}
@@ -310,6 +310,14 @@ where
310310
self.sift_up(0, old_len);
311311
}
312312

313+
/// Returns the underlying ```Vec<T,N>```. Order is arbitrary and time is O(1).
314+
pub fn into_vec(self) -> Vec<T, N> {
315+
// prevents dropping self.data at the end of this fn
316+
let mut dropless_heap = ManuallyDrop::new(self);
317+
// https://users.rust-lang.org/t/moving-out-of-a-type-implementing-drop/38225/5
318+
unsafe { ManuallyDrop::take(&mut dropless_heap.data) }
319+
}
320+
313321
/* Private API */
314322
fn sift_down_to_bottom(&mut self, mut pos: usize) {
315323
let end = self.len();

0 commit comments

Comments
 (0)