Skip to content

Commit b3db7f1

Browse files
committed
Implement From<BinaryHeap<T, C>> for Vec<T> when possible
This requires Rust 1.41.0 or greater.
1 parent 61eb0e6 commit b3db7f1

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
fn main() {
22
let ac = autocfg::new();
33

4+
// Required in order to implement `From<BinaryHeap<T, C>>` for `Vec<T>`.
5+
ac.emit_rustc_version(1, 41);
6+
47
// Required for stabilization of `unsafe_op_in_unsafe_fn` lint.
58
ac.emit_rustc_version(1, 52);
69

src/binary_heap.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,13 +1667,22 @@ impl<T: Ord> From<Vec<T>> for BinaryHeap<T> {
16671667
}
16681668
}
16691669

1670-
// #[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
1671-
// impl<T, C: Compare<T>> From<BinaryHeap<T, C>> for Vec<T> {
1672-
// fn from(heap: BinaryHeap<T, C>) -> Vec<T> {
1673-
// heap.data
1674-
// }
1675-
// }
1670+
/// # Compatibility
1671+
///
1672+
/// This trait is only implemented for Rust 1.41.0 or greater. For earlier versions, `Into<Vec<T>>`
1673+
/// is implemented for `BinaryHeap<T, C>` instead.
1674+
#[cfg(rustc_1_41)]
1675+
impl<T, C: Compare<T>> From<BinaryHeap<T, C>> for Vec<T> {
1676+
/// Converts a `BinaryHeap<T>` into a `Vec<T>`.
1677+
///
1678+
/// This conversion requires no data movement or allocation, and has
1679+
/// constant time complexity.
1680+
fn from(heap: BinaryHeap<T, C>) -> Vec<T> {
1681+
heap.data
1682+
}
1683+
}
16761684

1685+
#[cfg(not(rustc_1_41))]
16771686
impl<T, C: Compare<T>> Into<Vec<T>> for BinaryHeap<T, C> {
16781687
/// Converts a `BinaryHeap<T>` into a `Vec<T>`.
16791688
///

0 commit comments

Comments
 (0)