diff --git a/Cargo.toml b/Cargo.toml index 9199733e..d3aef2ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "indexmap" +edition = "2018" version = "1.5.0" authors = [ "bluss", diff --git a/benches/bench.rs b/benches/bench.rs index 74beb36e..102cd49c 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,6 +1,5 @@ #![feature(test)] -extern crate fnv; -extern crate rand; + extern crate test; #[macro_use] extern crate lazy_static; @@ -13,8 +12,6 @@ type FnvBuilder = BuildHasherDefault; use test::black_box; use test::Bencher; -extern crate indexmap; - use indexmap::IndexMap; use std::collections::HashMap; diff --git a/benches/faststring.rs b/benches/faststring.rs index 4cd56507..86b7e9cf 100644 --- a/benches/faststring.rs +++ b/benches/faststring.rs @@ -1,12 +1,9 @@ #![feature(test)] -extern crate lazy_static; -extern crate rand; + extern crate test; use test::Bencher; -extern crate indexmap; - use indexmap::IndexMap; use std::collections::HashMap; diff --git a/build.rs b/build.rs index 171a38a1..645c0f1c 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,3 @@ -extern crate autocfg; - fn main() { let ac = autocfg::new(); ac.emit_sysroot_crate("std"); diff --git a/src/equivalent.rs b/src/equivalent.rs index 649d8095..ad6635ff 100644 --- a/src/equivalent.rs +++ b/src/equivalent.rs @@ -1,4 +1,4 @@ -use std::borrow::Borrow; +use core::borrow::Borrow; /// Key equivalence trait. /// diff --git a/src/lib.rs b/src/lib.rs index 3def5d6b..2df2ceed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ // We *mostly* avoid unsafe code, but `map::core::raw` allows it to use `RawTable` buckets. #![deny(unsafe_code)] +#![warn(rust_2018_idioms)] #![doc(html_root_url = "https://docs.rs/indexmap/1/")] -#![cfg_attr(not(has_std), no_std)] +#![no_std] //! [`IndexMap`] is a hash table where the iteration order of the key-value //! pairs is independent of the hash values of the keys. @@ -33,8 +34,6 @@ //! to use alternate hashers: //! //! ``` -//! # extern crate fnv; -//! # extern crate fxhash; //! use fnv::FnvBuildHasher; //! use fxhash::FxBuildHasher; //! use indexmap::{IndexMap, IndexSet}; @@ -82,22 +81,15 @@ #[cfg(not(has_std))] extern crate alloc; -extern crate hashbrown; +#[cfg(has_std)] +#[macro_use] +extern crate std; #[cfg(not(has_std))] -pub(crate) mod std { - pub use core::*; - pub mod alloc { - pub use alloc::*; - } - pub mod collections { - pub use alloc::collections::*; - } - pub use alloc::vec; -} +use alloc::vec::{self, Vec}; -#[cfg(not(has_std))] -use std::vec::Vec; +#[cfg(has_std)] +use std::vec::{self, Vec}; #[macro_use] mod macros; @@ -115,9 +107,9 @@ pub mod set; #[cfg(feature = "rayon")] mod rayon; -pub use equivalent::Equivalent; -pub use map::IndexMap; -pub use set::IndexSet; +pub use crate::equivalent::Equivalent; +pub use crate::map::IndexMap; +pub use crate::set::IndexSet; // shared private items diff --git a/src/macros.rs b/src/macros.rs index 8e785235..c4d84217 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,12 +1,11 @@ #[cfg(has_std)] -#[macro_export(local_inner_macros)] +#[macro_export] /// Create an `IndexMap` from a list of key-value pairs /// /// ## Example /// /// ``` -/// #[macro_use] extern crate indexmap; -/// # fn main() { +/// use indexmap::indexmap; /// /// let map = indexmap!{ /// "a" => 1, @@ -18,16 +17,15 @@ /// /// // "a" is the first key /// assert_eq!(map.keys().next(), Some(&"a")); -/// # } /// ``` macro_rules! indexmap { (@single $($x:tt)*) => (()); - (@count $($rest:expr),*) => (<[()]>::len(&[$(indexmap!(@single $rest)),*])); + (@count $($rest:expr),*) => (<[()]>::len(&[$($crate::indexmap!(@single $rest)),*])); - ($($key:expr => $value:expr,)+) => { indexmap!($($key => $value),+) }; + ($($key:expr => $value:expr,)+) => { $crate::indexmap!($($key => $value),+) }; ($($key:expr => $value:expr),*) => { { - let _cap = indexmap!(@count $($key),*); + let _cap = $crate::indexmap!(@count $($key),*); let mut _map = $crate::IndexMap::with_capacity(_cap); $( _map.insert($key, $value); @@ -38,14 +36,13 @@ macro_rules! indexmap { } #[cfg(has_std)] -#[macro_export(local_inner_macros)] +#[macro_export] /// Create an `IndexSet` from a list of values /// /// ## Example /// /// ``` -/// #[macro_use] extern crate indexmap; -/// # fn main() { +/// use indexmap::indexset; /// /// let set = indexset!{ /// "a", @@ -57,16 +54,15 @@ macro_rules! indexmap { /// /// // "a" is the first value /// assert_eq!(set.iter().next(), Some(&"a")); -/// # } /// ``` macro_rules! indexset { (@single $($x:tt)*) => (()); - (@count $($rest:expr),*) => (<[()]>::len(&[$(indexset!(@single $rest)),*])); + (@count $($rest:expr),*) => (<[()]>::len(&[$($crate::indexset!(@single $rest)),*])); - ($($value:expr,)+) => { indexset!($($value),+) }; + ($($value:expr,)+) => { $crate::indexset!($($value),+) }; ($($value:expr),*) => { { - let _cap = indexset!(@count $($value),*); + let _cap = $crate::indexset!(@count $($value),*); let mut _set = $crate::IndexSet::with_capacity(_cap); $( _set.insert($value); diff --git a/src/map.rs b/src/map.rs index c8518447..4157c564 100644 --- a/src/map.rs +++ b/src/map.rs @@ -3,30 +3,26 @@ mod core; -#[cfg(not(has_std))] -use std::vec::Vec; - -pub use mutable_keys::MutableKeys; +pub use crate::mutable_keys::MutableKeys; #[cfg(feature = "rayon")] -pub use rayon::map as rayon; +pub use crate::rayon::map as rayon; -use std::hash::BuildHasher; -use std::hash::Hash; -use std::hash::Hasher; -use std::iter::FromIterator; -use std::ops::RangeFull; +use crate::vec::{self, Vec}; +use ::core::cmp::Ordering; +use ::core::fmt; +use ::core::hash::{BuildHasher, Hash, Hasher}; +use ::core::iter::FromIterator; +use ::core::ops::{Index, IndexMut, RangeFull}; +use ::core::slice::{Iter as SliceIter, IterMut as SliceIterMut}; #[cfg(has_std)] use std::collections::hash_map::RandomState; -use std::cmp::Ordering; -use std::fmt; - use self::core::IndexMapCore; -use equivalent::Equivalent; -use util::third; -use {Bucket, Entries, HashValue}; +use crate::equivalent::Equivalent; +use crate::util::third; +use crate::{Bucket, Entries, HashValue}; pub use self::core::{Entry, OccupiedEntry, VacantEntry}; @@ -133,7 +129,7 @@ where V: fmt::Debug, S: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if cfg!(not(feature = "test_debug")) { f.debug_map().entries(self.iter()).finish() } else { @@ -295,34 +291,34 @@ where /// in-place manipulation. /// /// Computes in **O(1)** time (amortized average). - pub fn entry(&mut self, key: K) -> Entry { + pub fn entry(&mut self, key: K) -> Entry<'_, K, V> { let hash = self.hash(&key); self.core.entry(hash, key) } /// Return an iterator over the key-value pairs of the map, in their order - pub fn iter(&self) -> Iter { + pub fn iter(&self) -> Iter<'_, K, V> { Iter { iter: self.as_entries().iter(), } } /// Return an iterator over the key-value pairs of the map, in their order - pub fn iter_mut(&mut self) -> IterMut { + pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { IterMut { iter: self.as_entries_mut().iter_mut(), } } /// Return an iterator over the keys of the map, in their order - pub fn keys(&self) -> Keys { + pub fn keys(&self) -> Keys<'_, K, V> { Keys { iter: self.as_entries().iter(), } } /// Return an iterator over the values of the map, in their order - pub fn values(&self) -> Values { + pub fn values(&self) -> Values<'_, K, V> { Values { iter: self.as_entries().iter(), } @@ -330,7 +326,7 @@ where /// Return an iterator over mutable references to the the values of the map, /// in their order - pub fn values_mut(&mut self) -> ValuesMut { + pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> { ValuesMut { iter: self.as_entries_mut().iter_mut(), } @@ -667,7 +663,7 @@ where /// Clears the `IndexMap`, returning all key-value pairs as a drain iterator. /// Keeps the allocated memory for reuse. - pub fn drain(&mut self, range: RangeFull) -> Drain { + pub fn drain(&mut self, range: RangeFull) -> Drain<'_, K, V> { Drain { iter: self.core.drain(range), } @@ -720,10 +716,6 @@ impl IndexMap { } } -use std::slice::Iter as SliceIter; -use std::slice::IterMut as SliceIterMut; -use std::vec::IntoIter as VecIntoIter; - /// An iterator over the keys of a `IndexMap`. /// /// This `struct` is created by the [`keys`] method on [`IndexMap`]. See its @@ -731,7 +723,7 @@ use std::vec::IntoIter as VecIntoIter; /// /// [`keys`]: struct.IndexMap.html#method.keys /// [`IndexMap`]: struct.IndexMap.html -pub struct Keys<'a, K: 'a, V: 'a> { +pub struct Keys<'a, K, V> { pub(crate) iter: SliceIter<'a, Bucket>, } @@ -763,7 +755,7 @@ impl<'a, K, V> Clone for Keys<'a, K, V> { } impl<'a, K: fmt::Debug, V> fmt::Debug for Keys<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -775,7 +767,7 @@ impl<'a, K: fmt::Debug, V> fmt::Debug for Keys<'a, K, V> { /// /// [`values`]: struct.IndexMap.html#method.values /// [`IndexMap`]: struct.IndexMap.html -pub struct Values<'a, K: 'a, V: 'a> { +pub struct Values<'a, K, V> { iter: SliceIter<'a, Bucket>, } @@ -807,7 +799,7 @@ impl<'a, K, V> Clone for Values<'a, K, V> { } impl<'a, K, V: fmt::Debug> fmt::Debug for Values<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -819,7 +811,7 @@ impl<'a, K, V: fmt::Debug> fmt::Debug for Values<'a, K, V> { /// /// [`values_mut`]: struct.IndexMap.html#method.values_mut /// [`IndexMap`]: struct.IndexMap.html -pub struct ValuesMut<'a, K: 'a, V: 'a> { +pub struct ValuesMut<'a, K, V> { iter: SliceIterMut<'a, Bucket>, } @@ -848,7 +840,7 @@ impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> { /// /// [`iter`]: struct.IndexMap.html#method.iter /// [`IndexMap`]: struct.IndexMap.html -pub struct Iter<'a, K: 'a, V: 'a> { +pub struct Iter<'a, K, V> { iter: SliceIter<'a, Bucket>, } @@ -880,7 +872,7 @@ impl<'a, K, V> Clone for Iter<'a, K, V> { } impl<'a, K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -892,7 +884,7 @@ impl<'a, K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'a, K, V> { /// /// [`iter_mut`]: struct.IndexMap.html#method.iter_mut /// [`IndexMap`]: struct.IndexMap.html -pub struct IterMut<'a, K: 'a, V: 'a> { +pub struct IterMut<'a, K, V> { iter: SliceIterMut<'a, Bucket>, } @@ -922,7 +914,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> { /// [`into_iter`]: struct.IndexMap.html#method.into_iter /// [`IndexMap`]: struct.IndexMap.html pub struct IntoIter { - pub(crate) iter: VecIntoIter>, + pub(crate) iter: vec::IntoIter>, } impl Iterator for IntoIter { @@ -944,7 +936,7 @@ impl ExactSizeIterator for IntoIter { } impl fmt::Debug for IntoIter { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.iter.as_slice().iter().map(Bucket::refs); f.debug_list().entries(iter).finish() } @@ -957,12 +949,8 @@ impl fmt::Debug for IntoIter { /// /// [`drain`]: struct.IndexMap.html#method.drain /// [`IndexMap`]: struct.IndexMap.html -pub struct Drain<'a, K, V> -where - K: 'a, - V: 'a, -{ - pub(crate) iter: ::std::vec::Drain<'a, Bucket>, +pub struct Drain<'a, K, V> { + pub(crate) iter: vec::Drain<'a, Bucket>, } impl<'a, K, V> Iterator for Drain<'a, K, V> { @@ -1013,8 +1001,6 @@ where } } -use std::ops::{Index, IndexMut}; - impl<'a, K, V, Q: ?Sized, S> Index<&'a Q> for IndexMap where Q: Hash + Equivalent, @@ -1149,7 +1135,8 @@ where #[cfg(test)] mod tests { use super::*; - use util::enumerate; + use crate::util::enumerate; + use std::string::String; #[test] fn it_works() { @@ -1405,8 +1392,7 @@ mod tests { map_b.swap_remove(&1); assert_ne!(map_a, map_b); - let map_c: IndexMap<_, String> = - map_b.into_iter().map(|(k, v)| (k, v.to_owned())).collect(); + let map_c: IndexMap<_, String> = map_b.into_iter().map(|(k, v)| (k, v.into())).collect(); assert_ne!(map_a, map_c); assert_ne!(map_c, map_a); } diff --git a/src/map/core.rs b/src/map/core.rs index 89c20ff7..044bae73 100644 --- a/src/map/core.rs +++ b/src/map/core.rs @@ -9,20 +9,17 @@ mod raw; -#[cfg(not(has_std))] -use std::vec::Vec; - use hashbrown::raw::RawTable; -use std::cmp; -use std::fmt; -use std::mem::replace; -use std::ops::RangeFull; -use std::vec::Drain; +use crate::vec::{Drain, Vec}; +use core::cmp; +use core::fmt; +use core::mem::replace; +use core::ops::RangeFull; -use equivalent::Equivalent; -use util::enumerate; -use {Bucket, Entries, HashValue}; +use crate::equivalent::Equivalent; +use crate::util::enumerate; +use crate::{Bucket, Entries, HashValue}; /// Core of the map that does not depend on S pub(crate) struct IndexMapCore { @@ -65,7 +62,7 @@ where K: fmt::Debug, V: fmt::Debug, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("IndexMapCore") .field("indices", &raw::DebugIndices(&self.indices)) .field("entries", &self.entries) @@ -132,7 +129,7 @@ impl IndexMapCore { self.entries.clear(); } - pub(crate) fn drain(&mut self, range: RangeFull) -> Drain> { + pub(crate) fn drain(&mut self, range: RangeFull) -> Drain<'_, Bucket> { self.indices.clear(); self.entries.drain(range) } @@ -228,7 +225,7 @@ impl IndexMapCore { /// Entry for an existing key-value pair or a vacant location to /// insert one. -pub enum Entry<'a, K: 'a, V: 'a> { +pub enum Entry<'a, K, V> { /// Existing slot with equivalent key. Occupied(OccupiedEntry<'a, K, V>), /// Vacant slot (no equivalent key in the map). @@ -300,7 +297,7 @@ impl<'a, K, V> Entry<'a, K, V> { } impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for Entry<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { Entry::Vacant(ref v) => f.debug_tuple(stringify!(Entry)).field(v).finish(), Entry::Occupied(ref o) => f.debug_tuple(stringify!(Entry)).field(o).finish(), @@ -355,7 +352,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { } impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for OccupiedEntry<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct(stringify!(OccupiedEntry)) .field("key", self.key()) .field("value", self.get()) @@ -367,7 +364,7 @@ impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for OccupiedEntry<'a /// It is part of the [`Entry`] enum. /// /// [`Entry`]: enum.Entry.html -pub struct VacantEntry<'a, K: 'a, V: 'a> { +pub struct VacantEntry<'a, K, V> { map: &'a mut IndexMapCore, hash: HashValue, key: K, @@ -394,7 +391,7 @@ impl<'a, K, V> VacantEntry<'a, K, V> { } impl<'a, K: 'a + fmt::Debug, V: 'a> fmt::Debug for VacantEntry<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple(stringify!(VacantEntry)) .field(self.key()) .finish() @@ -405,5 +402,5 @@ impl<'a, K: 'a + fmt::Debug, V: 'a> fmt::Debug for VacantEntry<'a, K, V> { fn assert_send_sync() { fn assert_send_sync() {} assert_send_sync::>(); - assert_send_sync::>(); + assert_send_sync::>(); } diff --git a/src/map/core/raw.rs b/src/map/core/raw.rs index c06f8c33..fa075f01 100644 --- a/src/map/core/raw.rs +++ b/src/map/core/raw.rs @@ -3,15 +3,15 @@ //! mostly in dealing with its bucket "pointers". use super::{Entry, Equivalent, HashValue, IndexMapCore, VacantEntry}; +use core::fmt; +use core::mem::replace; use hashbrown::raw::RawTable; -use std::fmt; -use std::mem::replace; type RawBucket = hashbrown::raw::Bucket; pub(super) struct DebugIndices<'a>(pub &'a RawTable); impl fmt::Debug for DebugIndices<'_> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let indices = unsafe { self.0.iter().map(|raw_bucket| raw_bucket.read()) }; f.debug_list().entries(indices).finish() } @@ -49,7 +49,7 @@ impl IndexMapCore { unsafe { self.indices.erase(raw_bucket) }; } - pub(crate) fn entry(&mut self, hash: HashValue, key: K) -> Entry + pub(crate) fn entry(&mut self, hash: HashValue, key: K) -> Entry<'_, K, V> where K: Eq, { @@ -193,7 +193,7 @@ impl IndexMapCore { /// [`Entry`]: enum.Entry.html // SAFETY: The lifetime of the map reference also constrains the raw bucket, // which is essentially a raw pointer into the map indices. -pub struct OccupiedEntry<'a, K: 'a, V: 'a> { +pub struct OccupiedEntry<'a, K, V> { map: &'a mut IndexMapCore, raw_bucket: RawBucket, key: K, diff --git a/src/mutable_keys.rs b/src/mutable_keys.rs index 19df504e..0688441e 100644 --- a/src/mutable_keys.rs +++ b/src/mutable_keys.rs @@ -1,5 +1,4 @@ -use std::hash::BuildHasher; -use std::hash::Hash; +use core::hash::{BuildHasher, Hash}; use super::{Equivalent, IndexMap}; diff --git a/src/rayon/map.rs b/src/rayon/map.rs index c0a5899e..417d2ee7 100644 --- a/src/rayon/map.rs +++ b/src/rayon/map.rs @@ -6,17 +6,17 @@ //! Requires crate feature `"rayon"` use super::collect; -use super::rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; -use super::rayon::prelude::*; +use rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; +use rayon::prelude::*; -use std::cmp::Ordering; -use std::fmt; -use std::hash::BuildHasher; -use std::hash::Hash; +use crate::vec::Vec; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{BuildHasher, Hash}; -use Bucket; -use Entries; -use IndexMap; +use crate::Bucket; +use crate::Entries; +use crate::IndexMap; /// Requires crate feature `"rayon"`. impl IntoParallelIterator for IndexMap @@ -47,7 +47,7 @@ pub struct IntoParIter { } impl fmt::Debug for IntoParIter { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::refs); f.debug_list().entries(iter).finish() } @@ -87,7 +87,7 @@ where /// /// [`par_iter`]: ../struct.IndexMap.html#method.par_iter /// [`IndexMap`]: ../struct.IndexMap.html -pub struct ParIter<'a, K: 'a, V: 'a> { +pub struct ParIter<'a, K, V> { entries: &'a [Bucket], } @@ -98,7 +98,7 @@ impl<'a, K, V> Clone for ParIter<'a, K, V> { } impl<'a, K: fmt::Debug, V: fmt::Debug> fmt::Debug for ParIter<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::refs); f.debug_list().entries(iter).finish() } @@ -138,7 +138,7 @@ where /// /// [`par_iter_mut`]: ../struct.IndexMap.html#method.par_iter_mut /// [`IndexMap`]: ../struct.IndexMap.html -pub struct ParIterMut<'a, K: 'a, V: 'a> { +pub struct ParIterMut<'a, K, V> { entries: &'a mut [Bucket], } @@ -167,7 +167,7 @@ where /// /// While parallel iterators can process items in any order, their relative order /// in the map is still preserved for operations like `reduce` and `collect`. - pub fn par_keys(&self) -> ParKeys { + pub fn par_keys(&self) -> ParKeys<'_, K, V> { ParKeys { entries: self.as_entries(), } @@ -177,7 +177,7 @@ where /// /// While parallel iterators can process items in any order, their relative order /// in the map is still preserved for operations like `reduce` and `collect`. - pub fn par_values(&self) -> ParValues { + pub fn par_values(&self) -> ParValues<'_, K, V> { ParValues { entries: self.as_entries(), } @@ -205,7 +205,7 @@ where /// /// [`par_keys`]: ../struct.IndexMap.html#method.par_keys /// [`IndexMap`]: ../struct.IndexMap.html -pub struct ParKeys<'a, K: 'a, V: 'a> { +pub struct ParKeys<'a, K, V> { entries: &'a [Bucket], } @@ -216,7 +216,7 @@ impl<'a, K, V> Clone for ParKeys<'a, K, V> { } impl<'a, K: fmt::Debug, V> fmt::Debug for ParKeys<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::key_ref); f.debug_list().entries(iter).finish() } @@ -239,7 +239,7 @@ impl<'a, K: Sync, V: Sync> IndexedParallelIterator for ParKeys<'a, K, V> { /// /// [`par_values`]: ../struct.IndexMap.html#method.par_values /// [`IndexMap`]: ../struct.IndexMap.html -pub struct ParValues<'a, K: 'a, V: 'a> { +pub struct ParValues<'a, K, V> { entries: &'a [Bucket], } @@ -250,7 +250,7 @@ impl<'a, K, V> Clone for ParValues<'a, K, V> { } impl<'a, K, V: fmt::Debug> fmt::Debug for ParValues<'a, K, V> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::value_ref); f.debug_list().entries(iter).finish() } @@ -277,7 +277,7 @@ where /// /// While parallel iterators can process items in any order, their relative order /// in the map is still preserved for operations like `reduce` and `collect`. - pub fn par_values_mut(&mut self) -> ParValuesMut { + pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> { ParValuesMut { entries: self.as_entries_mut(), } @@ -326,7 +326,7 @@ where /// /// [`par_values_mut`]: ../struct.IndexMap.html#method.par_values_mut /// [`IndexMap`]: ../struct.IndexMap.html -pub struct ParValuesMut<'a, K: 'a, V: 'a> { +pub struct ParValuesMut<'a, K, V> { entries: &'a mut [Bucket], } @@ -398,6 +398,7 @@ where #[cfg(test)] mod tests { use super::*; + use std::string::String; #[test] fn insert_order() { @@ -433,10 +434,8 @@ mod tests { map_b.insert(3, "3"); assert!(!map_a.par_eq(&map_b)); - let map_c: IndexMap<_, String> = map_b - .into_par_iter() - .map(|(k, v)| (k, v.to_owned())) - .collect(); + let map_c: IndexMap<_, String> = + map_b.into_par_iter().map(|(k, v)| (k, v.into())).collect(); assert!(!map_a.par_eq(&map_c)); assert!(!map_c.par_eq(&map_a)); } diff --git a/src/rayon/mod.rs b/src/rayon/mod.rs index 76386ff5..57c810be 100644 --- a/src/rayon/mod.rs +++ b/src/rayon/mod.rs @@ -1,9 +1,13 @@ -extern crate rayon; +use rayon::prelude::*; -use self::rayon::prelude::*; +#[cfg(not(has_std))] +use alloc::collections::LinkedList; +#[cfg(has_std)] use std::collections::LinkedList; +use crate::vec::Vec; + // generate `ParallelIterator` methods by just forwarding to the underlying // self.entries and mapping its elements. macro_rules! parallel_iterator_methods { diff --git a/src/rayon/set.rs b/src/rayon/set.rs index 9e0bbe5c..af316bb5 100644 --- a/src/rayon/set.rs +++ b/src/rayon/set.rs @@ -6,18 +6,18 @@ //! Requires crate feature `"rayon"`. use super::collect; -use super::rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; -use super::rayon::prelude::*; +use rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; +use rayon::prelude::*; -use std::cmp::Ordering; -use std::fmt; -use std::hash::BuildHasher; -use std::hash::Hash; +use crate::vec::Vec; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{BuildHasher, Hash}; -use Entries; -use IndexSet; +use crate::Entries; +use crate::IndexSet; -type Bucket = ::Bucket; +type Bucket = crate::Bucket; /// Requires crate feature `"rayon"`. impl IntoParallelIterator for IndexSet @@ -47,7 +47,7 @@ pub struct IntoParIter { } impl fmt::Debug for IntoParIter { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::key_ref); f.debug_list().entries(iter).finish() } @@ -86,7 +86,7 @@ where /// /// [`IndexSet`]: ../struct.IndexSet.html /// [`par_iter`]: ../struct.IndexSet.html#method.par_iter -pub struct ParIter<'a, T: 'a> { +pub struct ParIter<'a, T> { entries: &'a [Bucket], } @@ -97,7 +97,7 @@ impl<'a, T> Clone for ParIter<'a, T> { } impl<'a, T: fmt::Debug> fmt::Debug for ParIter<'a, T> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::key_ref); f.debug_list().entries(iter).finish() } @@ -241,7 +241,7 @@ where /// /// [`IndexSet`]: ../struct.IndexSet.html /// [`par_difference`]: ../struct.IndexSet.html#method.par_difference -pub struct ParDifference<'a, T: 'a, S1: 'a, S2: 'a> { +pub struct ParDifference<'a, T, S1, S2> { set1: &'a IndexSet, set2: &'a IndexSet, } @@ -258,7 +258,7 @@ where S1: BuildHasher, S2: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list() .entries(self.set1.difference(&self.set2)) .finish() @@ -292,7 +292,7 @@ where /// /// [`IndexSet`]: ../struct.IndexSet.html /// [`par_intersection`]: ../struct.IndexSet.html#method.par_intersection -pub struct ParIntersection<'a, T: 'a, S1: 'a, S2: 'a> { +pub struct ParIntersection<'a, T, S1, S2> { set1: &'a IndexSet, set2: &'a IndexSet, } @@ -309,7 +309,7 @@ where S1: BuildHasher, S2: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list() .entries(self.set1.intersection(&self.set2)) .finish() @@ -343,7 +343,7 @@ where /// /// [`IndexSet`]: ../struct.IndexSet.html /// [`par_symmetric_difference`]: ../struct.IndexSet.html#method.par_symmetric_difference -pub struct ParSymmetricDifference<'a, T: 'a, S1: 'a, S2: 'a> { +pub struct ParSymmetricDifference<'a, T, S1, S2> { set1: &'a IndexSet, set2: &'a IndexSet, } @@ -360,7 +360,7 @@ where S1: BuildHasher, S2: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list() .entries(self.set1.symmetric_difference(&self.set2)) .finish() @@ -394,7 +394,7 @@ where /// /// [`IndexSet`]: ../struct.IndexSet.html /// [`par_union`]: ../struct.IndexSet.html#method.par_union -pub struct ParUnion<'a, T: 'a, S1: 'a, S2: 'a> { +pub struct ParUnion<'a, T, S1, S2> { set1: &'a IndexSet, set2: &'a IndexSet, } @@ -411,7 +411,7 @@ where S1: BuildHasher, S2: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.set1.union(&self.set2)).finish() } } diff --git a/src/serde.rs b/src/serde.rs index 9199a8d5..853c6b9a 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -1,16 +1,14 @@ -extern crate serde; - -use self::serde::de::value::{MapDeserializer, SeqDeserializer}; -use self::serde::de::{ +use serde::de::value::{MapDeserializer, SeqDeserializer}; +use serde::de::{ Deserialize, Deserializer, Error, IntoDeserializer, MapAccess, SeqAccess, Visitor, }; -use self::serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer}; +use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer}; -use std::fmt::{self, Formatter}; -use std::hash::{BuildHasher, Hash}; -use std::marker::PhantomData; +use core::fmt::{self, Formatter}; +use core::hash::{BuildHasher, Hash}; +use core::marker::PhantomData; -use IndexMap; +use crate::IndexMap; /// Requires crate feature `"serde-1"` impl Serialize for IndexMap @@ -41,7 +39,7 @@ where { type Value = IndexMap; - fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result { write!(formatter, "a map") } @@ -89,7 +87,7 @@ where } } -use IndexSet; +use crate::IndexSet; /// Requires crate feature `"serde-1"` impl Serialize for IndexSet @@ -118,7 +116,7 @@ where { type Value = IndexSet; - fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result { write!(formatter, "a set") } diff --git a/src/set.rs b/src/set.rs index 28ee8640..fdd9e67d 100644 --- a/src/set.rs +++ b/src/set.rs @@ -1,22 +1,18 @@ //! A hash set implemented using `IndexMap` #[cfg(feature = "rayon")] -pub use rayon::set as rayon; - -#[cfg(not(has_std))] -use std::vec::Vec; +pub use crate::rayon::set as rayon; #[cfg(has_std)] use std::collections::hash_map::RandomState; -use std::cmp::Ordering; -use std::fmt; -use std::hash::{BuildHasher, Hash}; -use std::iter::{Chain, FromIterator}; -use std::ops::RangeFull; -use std::ops::{BitAnd, BitOr, BitXor, Sub}; -use std::slice; -use std::vec; +use crate::vec::{self, Vec}; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{BuildHasher, Hash}; +use core::iter::{Chain, FromIterator}; +use core::ops::{BitAnd, BitOr, BitXor, RangeFull, Sub}; +use core::slice; use super::{Entries, Equivalent, IndexMap}; @@ -119,7 +115,7 @@ where T: fmt::Debug + Hash + Eq, S: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if cfg!(not(feature = "test_debug")) { f.debug_set().entries(self.iter()).finish() } else { @@ -262,7 +258,7 @@ where } /// Return an iterator over the values of the set, in their order - pub fn iter(&self) -> Iter { + pub fn iter(&self) -> Iter<'_, T> { Iter { iter: self.map.keys().iter, } @@ -562,7 +558,7 @@ where /// Clears the `IndexSet`, returning all values as a drain iterator. /// Keeps the allocated memory for reuse. - pub fn drain(&mut self, range: RangeFull) -> Drain { + pub fn drain(&mut self, range: RangeFull) -> Drain<'_, T> { Drain { iter: self.map.drain(range).iter, } @@ -636,7 +632,7 @@ impl ExactSizeIterator for IntoIter { } impl fmt::Debug for IntoIter { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.iter.as_slice().iter().map(Bucket::key_ref); f.debug_list().entries(iter).finish() } @@ -649,7 +645,7 @@ impl fmt::Debug for IntoIter { /// /// [`IndexSet`]: struct.IndexSet.html /// [`iter`]: struct.IndexSet.html#method.iter -pub struct Iter<'a, T: 'a> { +pub struct Iter<'a, T> { iter: slice::Iter<'a, Bucket>, } @@ -680,7 +676,7 @@ impl<'a, T> Clone for Iter<'a, T> { } impl<'a, T: fmt::Debug> fmt::Debug for Iter<'a, T> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -692,7 +688,7 @@ impl<'a, T: fmt::Debug> fmt::Debug for Iter<'a, T> { /// /// [`IndexSet`]: struct.IndexSet.html /// [`drain`]: struct.IndexSet.html#method.drain -pub struct Drain<'a, T: 'a> { +pub struct Drain<'a, T> { iter: vec::Drain<'a, Bucket>, } @@ -840,7 +836,7 @@ where /// /// [`IndexSet`]: struct.IndexSet.html /// [`difference`]: struct.IndexSet.html#method.difference -pub struct Difference<'a, T: 'a, S: 'a> { +pub struct Difference<'a, T, S> { iter: Iter<'a, T>, other: &'a IndexSet, } @@ -895,7 +891,7 @@ where T: fmt::Debug + Eq + Hash, S: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -907,7 +903,7 @@ where /// /// [`IndexSet`]: struct.IndexSet.html /// [`intersection`]: struct.IndexSet.html#method.intersection -pub struct Intersection<'a, T: 'a, S: 'a> { +pub struct Intersection<'a, T, S> { iter: Iter<'a, T>, other: &'a IndexSet, } @@ -962,7 +958,7 @@ where T: fmt::Debug + Eq + Hash, S: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -974,7 +970,7 @@ where /// /// [`IndexSet`]: struct.IndexSet.html /// [`symmetric_difference`]: struct.IndexSet.html#method.symmetric_difference -pub struct SymmetricDifference<'a, T: 'a, S1: 'a, S2: 'a> { +pub struct SymmetricDifference<'a, T, S1, S2> { iter: Chain, Difference<'a, T, S1>>, } @@ -1027,7 +1023,7 @@ where S1: BuildHasher, S2: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -1039,7 +1035,7 @@ where /// /// [`IndexSet`]: struct.IndexSet.html /// [`union`]: struct.IndexSet.html#method.union -pub struct Union<'a, T: 'a, S: 'a> { +pub struct Union<'a, T, S> { iter: Chain, Difference<'a, T, S>>, } @@ -1089,7 +1085,7 @@ where T: fmt::Debug + Eq + Hash, S: BuildHasher, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } @@ -1163,7 +1159,8 @@ where #[cfg(test)] mod tests { use super::*; - use util::enumerate; + use crate::util::enumerate; + use std::string::String; #[test] fn it_works() { diff --git a/src/util.rs b/src/util.rs index 4174316f..e7bb0e1a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,4 @@ -use std::iter::Enumerate; +use core::iter::Enumerate; pub(crate) fn third(t: (A, B, C)) -> C { t.2 diff --git a/tests/equivalent_trait.rs b/tests/equivalent_trait.rs index 1020bb90..ff5943a3 100644 --- a/tests/equivalent_trait.rs +++ b/tests/equivalent_trait.rs @@ -1,6 +1,4 @@ -#[macro_use] -extern crate indexmap; - +use indexmap::indexmap; use indexmap::Equivalent; use std::hash::Hash; diff --git a/tests/quick.rs b/tests/quick.rs index 1399d0a8..4f8f9e58 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -1,14 +1,7 @@ -extern crate indexmap; -extern crate itertools; -#[macro_use] -extern crate quickcheck; -extern crate rand; - -extern crate fnv; - use indexmap::IndexMap; use itertools::Itertools; +use quickcheck::quickcheck; use quickcheck::Arbitrary; use quickcheck::Gen; @@ -144,7 +137,7 @@ quickcheck! { } -use Op::*; +use crate::Op::*; #[derive(Copy, Clone, Debug)] enum Op { Add(K, V), diff --git a/tests/tests.rs b/tests/tests.rs index 7b919465..7d522f1c 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,6 +1,4 @@ -#[macro_use] -extern crate indexmap; -extern crate itertools; +use indexmap::{indexmap, indexset}; #[test] fn test_sort() {