Skip to content

Commit 34c14de

Browse files
authored
Merge pull request #38 from clint-white/2021-edition
Migrate to Rust 2021 edition
2 parents bbf5534 + 7689e21 commit 34c14de

File tree

6 files changed

+37
-57
lines changed

6 files changed

+37
-57
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@ jobs:
1616
- macos-latest
1717
rust:
1818
- stable
19-
- 1.52.0 # MSRV
19+
- 1.56.0 # MSRV
2020
cargo_args:
2121
- ""
2222
- --features serde
2323
include:
24-
# Also test on nightly and Rust versions between MSRV and stable
25-
# where there is conditional compilation
2624
- os: ubuntu-latest
2725
rust: nightly
2826
cargo_args: ""
29-
- os: ubuntu-latest
30-
rust: 1.56.0
31-
cargo_args: ""
3227

3328
runs-on: ${{ matrix.os }}
3429

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
* `#[must_use]` attribute to many methods, porting and extending several
1212
rust-lang/rust PRs
13-
* Method `shrink_to()` for Rust 1.56.0 and greater, ported from rust-lang/rust
14-
* Implementation of `From<[T; N]>` for `BinaryHeap<T>` for Rust 1.56.0 or
15-
greater, ported from rust-lang/rust#84111
13+
* Method `shrink_to()`, ported from rust-lang/rust
14+
* Implementation of `From<[T; N]>` for `BinaryHeap<T>`, ported from
15+
rust-lang/rust#84111
1616
* Links to referenced items in the documenation
1717
* Example of a min-heap, ported from rust-lang/rust#60451
1818
* Documentation of time complexities of several methods, ported from
1919
rust-lang/rust#60952
2020

2121
### Changed
2222

23-
* Bump MSRV (minimum supported rust version) to rust 1.52.0.
23+
* Migrate to Rust 2021 Edition
24+
* Increase MSRV (minimum supported rust version) to rust 1.56.0.
2425
* Implement `From<BinaryHeap<T, C>>` for `Vec<T>` instead of `Into<Vec<T>>` for
2526
`BinaryHeap<T, C>`
2627
* Port rust-lang/rust#77435 improvement to rebuild heuristic of

Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ repository = "https://github.com/sekineh/binary-heap-plus-rs"
88
readme = "README.md"
99
keywords = ["binary", "heap", "priority", "queue"]
1010
categories = ["data-structures", "algorithms", ]
11-
edition = "2018"
12-
13-
[build-dependencies]
14-
autocfg = "1"
11+
edition = "2021"
12+
rust-version = "1.56.0"
1513

1614
[dependencies]
1715
compare = "0.1.0"
1816
serde = { version = "1.0.116", optional = true, features = ["derive"] }
1917

2018
[dev-dependencies]
2119
serde_json = "1.0.57"
22-
rand = "0.7.3"
20+
rand = "0.8"
2321

2422
[badges]
2523
# TODO: waiting for PR to land...: https://github.com/rust-lang/crates.io/pull/1838#

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ This crate is based on the standard library's implementation of
2626
[`BinaryHeap`](https://doc.rust-lang.org/stable/std/collections/struct.BinaryHeap.html)
2727
from Rust 1.62.0.
2828

29-
This crate requires Rust 1.52.0 or later. A few of its APIs are only available
30-
for more recent versions of Rust where they have been stabilized in the
31-
standard library; see the documentation for specifics.
29+
The minimum supported Rust version is 1.56.0.
3230

3331
# Changes
3432

build.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/binary_heap.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
//!
2222
//! ```
2323
//! use std::cmp::Ordering;
24-
//! // Only required for Rust versions prior to 1.43.0.
25-
//! use std::usize;
2624
//! use binary_heap_plus::BinaryHeap;
2725
//!
2826
//! #[derive(Copy, Clone, Eq, PartialEq)]
@@ -159,7 +157,7 @@ use std::slice;
159157
// use std::vec::Drain;
160158
use compare::Compare;
161159
use core::fmt;
162-
use core::mem::{size_of, swap, ManuallyDrop};
160+
use core::mem::{swap, ManuallyDrop};
163161
use core::ptr;
164162
#[cfg(feature = "serde")]
165163
use serde::{Deserialize, Serialize};
@@ -227,6 +225,15 @@ use std::vec;
227225
/// assert!(heap.is_empty())
228226
/// ```
229227
///
228+
/// A `BinaryHeap` with a known list of items can be initialized from an array:
229+
///
230+
/// ```
231+
/// use binary_heap_plus::BinaryHeap;
232+
///
233+
/// // This will create a max-heap.
234+
/// let heap = BinaryHeap::from([1, 5, 2]);
235+
/// ```
236+
///
230237
/// ## Min-heap
231238
///
232239
/// `BinaryHeap` can also act as a min-heap without requiring [`Reverse`] or a custom [`Ord`]
@@ -281,7 +288,7 @@ pub struct MaxComparator;
281288

282289
impl<T: Ord> Compare<T> for MaxComparator {
283290
fn compare(&self, a: &T, b: &T) -> Ordering {
284-
a.cmp(&b)
291+
a.cmp(b)
285292
}
286293
}
287294

@@ -293,7 +300,7 @@ pub struct MinComparator;
293300

294301
impl<T: Ord> Compare<T> for MinComparator {
295302
fn compare(&self, a: &T, b: &T) -> Ordering {
296-
b.cmp(&a)
303+
b.cmp(a)
297304
}
298305
}
299306

@@ -687,7 +694,7 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
687694
///
688695
/// // replace the comparor
689696
/// heap.replace_cmp(Comparator { ascending: false });
690-
/// assert_eq!(heap.into_iter_sorted().collect::<Vec<_>>(), vec![5, 3, 1]);
697+
/// assert_eq!(heap.into_iter_sorted().collect::<Vec<_>>(), [5, 3, 1]);
691698
/// ```
692699
#[inline]
693700
pub fn replace_cmp(&mut self, cmp: C) {
@@ -757,7 +764,7 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
757764
///
758765
/// ```
759766
/// use binary_heap_plus::BinaryHeap;
760-
/// let mut heap = BinaryHeap::from(vec![1, 3]);
767+
/// let mut heap = BinaryHeap::from([1, 3]);
761768
///
762769
/// assert_eq!(heap.pop(), Some(3));
763770
/// assert_eq!(heap.pop(), Some(1));
@@ -830,7 +837,7 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
830837
/// ```
831838
/// use binary_heap_plus::BinaryHeap;
832839
///
833-
/// let mut heap = BinaryHeap::from(vec![1, 2, 4, 5, 7]);
840+
/// let mut heap = BinaryHeap::from([1, 2, 4, 5, 7]);
834841
/// heap.push(6);
835842
/// heap.push(3);
836843
///
@@ -1010,11 +1017,9 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
10101017

10111018
let tail_len = self.len() - start;
10121019

1013-
// `usize::BITS` requires Rust 1.53.0 or greater.
1014-
#[allow(clippy::manual_bits)]
10151020
#[inline(always)]
10161021
fn log2_fast(x: usize) -> usize {
1017-
8 * size_of::<usize>() - (x.leading_zeros() as usize) - 1
1022+
(usize::BITS - x.leading_zeros() - 1) as usize
10181023
}
10191024

10201025
// `rebuild` takes O(self.len()) operations
@@ -1061,8 +1066,8 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
10611066
/// ```
10621067
/// use binary_heap_plus::BinaryHeap;
10631068
///
1064-
/// let mut a = BinaryHeap::from(vec![-10, 1, 2, 3, 3]);
1065-
/// let mut b = BinaryHeap::from(vec![-20, 5, 43]);
1069+
/// let mut a = BinaryHeap::from([-10, 1, 2, 3, 3]);
1070+
/// let mut b = BinaryHeap::from([-20, 5, 43]);
10661071
///
10671072
/// a.append(&mut b);
10681073
///
@@ -1093,15 +1098,15 @@ impl<T, C> BinaryHeap<T, C> {
10931098
///
10941099
/// ```
10951100
/// use binary_heap_plus::BinaryHeap;
1096-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
1101+
/// let heap = BinaryHeap::from([1, 2, 3, 4]);
10971102
///
10981103
/// // Print 1, 2, 3, 4 in arbitrary order
10991104
/// for x in heap.iter() {
11001105
/// println!("{}", x);
11011106
/// }
11021107
/// ```
11031108
// #[stable(feature = "rust1", since = "1.0.0")]
1104-
pub fn iter(&self) -> Iter<T> {
1109+
pub fn iter(&self) -> Iter<'_, T> {
11051110
Iter {
11061111
iter: self.data.iter(),
11071112
}
@@ -1116,9 +1121,9 @@ impl<T, C> BinaryHeap<T, C> {
11161121
///
11171122
/// ```
11181123
/// use binary_heap_plus::BinaryHeap;
1119-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
1124+
/// let heap = BinaryHeap::from([1, 2, 3, 4, 5]);
11201125
///
1121-
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]);
1126+
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
11221127
/// ```
11231128
// #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
11241129
pub fn into_iter_sorted(self) -> IntoIterSorted<T, C> {
@@ -1258,12 +1263,7 @@ impl<T, C> BinaryHeap<T, C> {
12581263
/// heap.shrink_to(10);
12591264
/// assert!(heap.capacity() >= 10);
12601265
/// ```
1261-
///
1262-
/// # Compatibility
1263-
///
1264-
/// This feature requires Rust 1.56.0 or greater.
12651266
#[inline]
1266-
#[cfg(rustc_1_56)]
12671267
pub fn shrink_to(&mut self, min_capacity: usize) {
12681268
self.data.shrink_to(min_capacity)
12691269
}
@@ -1277,7 +1277,7 @@ impl<T, C> BinaryHeap<T, C> {
12771277
///
12781278
/// ```
12791279
/// use binary_heap_plus::BinaryHeap;
1280-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]);
1280+
/// let heap = BinaryHeap::from([1, 2, 3, 4, 5, 6, 7]);
12811281
/// let vec = heap.into_vec();
12821282
///
12831283
/// // Will print in some order
@@ -1299,7 +1299,7 @@ impl<T, C> BinaryHeap<T, C> {
12991299
///
13001300
/// ```
13011301
/// use binary_heap_plus::BinaryHeap;
1302-
/// let heap = BinaryHeap::from(vec![1, 3]);
1302+
/// let heap = BinaryHeap::from([1, 3]);
13031303
///
13041304
/// assert_eq!(heap.len(), 2);
13051305
/// ```
@@ -1346,7 +1346,7 @@ impl<T, C> BinaryHeap<T, C> {
13461346
///
13471347
/// ```
13481348
/// use binary_heap_plus::BinaryHeap;
1349-
/// let mut heap = BinaryHeap::from(vec![1, 3]);
1349+
/// let mut heap = BinaryHeap::from([1, 3]);
13501350
///
13511351
/// assert!(!heap.is_empty());
13521352
///
@@ -1372,7 +1372,7 @@ impl<T, C> BinaryHeap<T, C> {
13721372
///
13731373
/// ```
13741374
/// use binary_heap_plus::BinaryHeap;
1375-
/// let mut heap = BinaryHeap::from(vec![1, 3]);
1375+
/// let mut heap = BinaryHeap::from([1, 3]);
13761376
///
13771377
/// assert!(!heap.is_empty());
13781378
///
@@ -1655,10 +1655,6 @@ impl<T: Ord> From<Vec<T>> for BinaryHeap<T> {
16551655
}
16561656
}
16571657

1658-
/// # Compatibility
1659-
///
1660-
/// This trait is only implemented for Rust 1.56.0 or greater.
1661-
#[cfg(rustc_1_56)]
16621658
impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
16631659
/// ```
16641660
/// use binary_heap_plus::BinaryHeap;
@@ -1706,7 +1702,7 @@ impl<T, C> IntoIterator for BinaryHeap<T, C> {
17061702
///
17071703
/// ```
17081704
/// use binary_heap_plus::BinaryHeap;
1709-
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4]);
1705+
/// let heap = BinaryHeap::from([1, 2, 3, 4]);
17101706
///
17111707
/// // Print 1, 2, 3, 4 in arbitrary order
17121708
/// for x in heap.into_iter() {

0 commit comments

Comments
 (0)