1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::collections::BinaryHeap;

pub trait BinaryHeapExtensions<T> {
    fn into_sorted_iter(self) -> BinaryHeapIntoSortedIter<T>;
}

pub struct BinaryHeapIntoSortedIter<T> {
    binary_heap: BinaryHeap<T>,
}

impl<T: Ord> Iterator for BinaryHeapIntoSortedIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.binary_heap.pop()
    }
}

impl<T> BinaryHeapExtensions<T> for BinaryHeap<T> {
    fn into_sorted_iter(self) -> BinaryHeapIntoSortedIter<T> {
        BinaryHeapIntoSortedIter { binary_heap: self }
    }
}