diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 32ed9ee..c0eeea9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,19 +16,14 @@ jobs: - macos-latest rust: - stable - - 1.52.0 # MSRV + - 1.56.0 # MSRV cargo_args: - "" - --features serde include: - # Also test on nightly and Rust versions between MSRV and stable - # where there is conditional compilation - os: ubuntu-latest rust: nightly cargo_args: "" - - os: ubuntu-latest - rust: 1.56.0 - cargo_args: "" runs-on: ${{ matrix.os }} diff --git a/CHANGELOG.md b/CHANGELOG.md index baac573..16b1cd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `#[must_use]` attribute to many methods, porting and extending several rust-lang/rust PRs -* Method `shrink_to()` for Rust 1.56.0 and greater, ported from rust-lang/rust -* Implementation of `From<[T; N]>` for `BinaryHeap` for Rust 1.56.0 or - greater, ported from rust-lang/rust#84111 +* Method `shrink_to()`, ported from rust-lang/rust +* Implementation of `From<[T; N]>` for `BinaryHeap`, ported from + rust-lang/rust#84111 * Links to referenced items in the documenation * Example of a min-heap, ported from rust-lang/rust#60451 * Documentation of time complexities of several methods, ported from @@ -20,7 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -* Bump MSRV (minimum supported rust version) to rust 1.52.0. +* Migrate to Rust 2021 Edition +* Increase MSRV (minimum supported rust version) to rust 1.56.0. * Implement `From>` for `Vec` instead of `Into>` for `BinaryHeap` * Port rust-lang/rust#77435 improvement to rebuild heuristic of diff --git a/Cargo.toml b/Cargo.toml index 12a8604..969a47f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,8 @@ repository = "https://github.com/sekineh/binary-heap-plus-rs" readme = "README.md" keywords = ["binary", "heap", "priority", "queue"] categories = ["data-structures", "algorithms", ] -edition = "2018" - -[build-dependencies] -autocfg = "1" +edition = "2021" +rust-version = "1.56.0" [dependencies] compare = "0.1.0" @@ -19,7 +17,7 @@ serde = { version = "1.0.116", optional = true, features = ["derive"] } [dev-dependencies] serde_json = "1.0.57" -rand = "0.7.3" +rand = "0.8" [badges] # TODO: waiting for PR to land...: https://github.com/rust-lang/crates.io/pull/1838# diff --git a/README.md b/README.md index 2877e50..854ef12 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,7 @@ This crate is based on the standard library's implementation of [`BinaryHeap`](https://doc.rust-lang.org/stable/std/collections/struct.BinaryHeap.html) from Rust 1.62.0. -This crate requires Rust 1.52.0 or later. A few of its APIs are only available -for more recent versions of Rust where they have been stabilized in the -standard library; see the documentation for specifics. +The minimum supported Rust version is 1.56.0. # Changes diff --git a/build.rs b/build.rs deleted file mode 100644 index e50a3cf..0000000 --- a/build.rs +++ /dev/null @@ -1,8 +0,0 @@ -fn main() { - let ac = autocfg::new(); - - // Required for stabilization of `Vec::shrink_to()` and `IntoIterator` for arrays. - ac.emit_rustc_version(1, 56); - - autocfg::rerun_path("build.rs"); -} diff --git a/src/binary_heap.rs b/src/binary_heap.rs index 73baae6..de040c4 100644 --- a/src/binary_heap.rs +++ b/src/binary_heap.rs @@ -21,8 +21,6 @@ //! //! ``` //! use std::cmp::Ordering; -//! // Only required for Rust versions prior to 1.43.0. -//! use std::usize; //! use binary_heap_plus::BinaryHeap; //! //! #[derive(Copy, Clone, Eq, PartialEq)] @@ -159,7 +157,7 @@ use std::slice; // use std::vec::Drain; use compare::Compare; use core::fmt; -use core::mem::{size_of, swap, ManuallyDrop}; +use core::mem::{swap, ManuallyDrop}; use core::ptr; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -227,6 +225,15 @@ use std::vec; /// assert!(heap.is_empty()) /// ``` /// +/// A `BinaryHeap` with a known list of items can be initialized from an array: +/// +/// ``` +/// use binary_heap_plus::BinaryHeap; +/// +/// // This will create a max-heap. +/// let heap = BinaryHeap::from([1, 5, 2]); +/// ``` +/// /// ## Min-heap /// /// `BinaryHeap` can also act as a min-heap without requiring [`Reverse`] or a custom [`Ord`] @@ -281,7 +288,7 @@ pub struct MaxComparator; impl Compare for MaxComparator { fn compare(&self, a: &T, b: &T) -> Ordering { - a.cmp(&b) + a.cmp(b) } } @@ -293,7 +300,7 @@ pub struct MinComparator; impl Compare for MinComparator { fn compare(&self, a: &T, b: &T) -> Ordering { - b.cmp(&a) + b.cmp(a) } } @@ -687,7 +694,7 @@ impl> BinaryHeap { /// /// // replace the comparor /// heap.replace_cmp(Comparator { ascending: false }); - /// assert_eq!(heap.into_iter_sorted().collect::>(), vec![5, 3, 1]); + /// assert_eq!(heap.into_iter_sorted().collect::>(), [5, 3, 1]); /// ``` #[inline] pub fn replace_cmp(&mut self, cmp: C) { @@ -757,7 +764,7 @@ impl> BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let mut heap = BinaryHeap::from(vec![1, 3]); + /// let mut heap = BinaryHeap::from([1, 3]); /// /// assert_eq!(heap.pop(), Some(3)); /// assert_eq!(heap.pop(), Some(1)); @@ -830,7 +837,7 @@ impl> BinaryHeap { /// ``` /// use binary_heap_plus::BinaryHeap; /// - /// let mut heap = BinaryHeap::from(vec![1, 2, 4, 5, 7]); + /// let mut heap = BinaryHeap::from([1, 2, 4, 5, 7]); /// heap.push(6); /// heap.push(3); /// @@ -1010,11 +1017,9 @@ impl> BinaryHeap { let tail_len = self.len() - start; - // `usize::BITS` requires Rust 1.53.0 or greater. - #[allow(clippy::manual_bits)] #[inline(always)] fn log2_fast(x: usize) -> usize { - 8 * size_of::() - (x.leading_zeros() as usize) - 1 + (usize::BITS - x.leading_zeros() - 1) as usize } // `rebuild` takes O(self.len()) operations @@ -1061,8 +1066,8 @@ impl> BinaryHeap { /// ``` /// use binary_heap_plus::BinaryHeap; /// - /// let mut a = BinaryHeap::from(vec![-10, 1, 2, 3, 3]); - /// let mut b = BinaryHeap::from(vec![-20, 5, 43]); + /// let mut a = BinaryHeap::from([-10, 1, 2, 3, 3]); + /// let mut b = BinaryHeap::from([-20, 5, 43]); /// /// a.append(&mut b); /// @@ -1093,7 +1098,7 @@ impl BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let heap = BinaryHeap::from(vec![1, 2, 3, 4]); + /// let heap = BinaryHeap::from([1, 2, 3, 4]); /// /// // Print 1, 2, 3, 4 in arbitrary order /// for x in heap.iter() { @@ -1101,7 +1106,7 @@ impl BinaryHeap { /// } /// ``` // #[stable(feature = "rust1", since = "1.0.0")] - pub fn iter(&self) -> Iter { + pub fn iter(&self) -> Iter<'_, T> { Iter { iter: self.data.iter(), } @@ -1116,9 +1121,9 @@ impl BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]); + /// let heap = BinaryHeap::from([1, 2, 3, 4, 5]); /// - /// assert_eq!(heap.into_iter_sorted().take(2).collect::>(), vec![5, 4]); + /// assert_eq!(heap.into_iter_sorted().take(2).collect::>(), [5, 4]); /// ``` // #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")] pub fn into_iter_sorted(self) -> IntoIterSorted { @@ -1258,12 +1263,7 @@ impl BinaryHeap { /// heap.shrink_to(10); /// assert!(heap.capacity() >= 10); /// ``` - /// - /// # Compatibility - /// - /// This feature requires Rust 1.56.0 or greater. #[inline] - #[cfg(rustc_1_56)] pub fn shrink_to(&mut self, min_capacity: usize) { self.data.shrink_to(min_capacity) } @@ -1277,7 +1277,7 @@ impl BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5, 6, 7]); + /// let heap = BinaryHeap::from([1, 2, 3, 4, 5, 6, 7]); /// let vec = heap.into_vec(); /// /// // Will print in some order @@ -1299,7 +1299,7 @@ impl BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let heap = BinaryHeap::from(vec![1, 3]); + /// let heap = BinaryHeap::from([1, 3]); /// /// assert_eq!(heap.len(), 2); /// ``` @@ -1346,7 +1346,7 @@ impl BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let mut heap = BinaryHeap::from(vec![1, 3]); + /// let mut heap = BinaryHeap::from([1, 3]); /// /// assert!(!heap.is_empty()); /// @@ -1372,7 +1372,7 @@ impl BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let mut heap = BinaryHeap::from(vec![1, 3]); + /// let mut heap = BinaryHeap::from([1, 3]); /// /// assert!(!heap.is_empty()); /// @@ -1655,10 +1655,6 @@ impl From> for BinaryHeap { } } -/// # Compatibility -/// -/// This trait is only implemented for Rust 1.56.0 or greater. -#[cfg(rustc_1_56)] impl From<[T; N]> for BinaryHeap { /// ``` /// use binary_heap_plus::BinaryHeap; @@ -1706,7 +1702,7 @@ impl IntoIterator for BinaryHeap { /// /// ``` /// use binary_heap_plus::BinaryHeap; - /// let heap = BinaryHeap::from(vec![1, 2, 3, 4]); + /// let heap = BinaryHeap::from([1, 2, 3, 4]); /// /// // Print 1, 2, 3, 4 in arbitrary order /// for x in heap.into_iter() {