From f4cacc1422f09cdc80bd62ecb675be53124b08ec Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 23 Jul 2020 16:42:23 -0700 Subject: [PATCH] Relax some bounds to match std --- src/map.rs | 152 ++++++++++++++++++++--------------------------- src/rayon/map.rs | 28 ++++++--- src/rayon/set.rs | 6 +- src/set.rs | 92 ++++++++++++---------------- 4 files changed, 123 insertions(+), 155 deletions(-) diff --git a/src/map.rs b/src/map.rs index 6025631e..6c2c466d 100644 --- a/src/map.rs +++ b/src/map.rs @@ -125,9 +125,8 @@ impl Entries for IndexMap { impl fmt::Debug for IndexMap where - K: fmt::Debug + Hash + Eq, + K: fmt::Debug, V: fmt::Debug, - S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if cfg!(not(feature = "test_debug")) { @@ -165,10 +164,7 @@ impl IndexMap { /// /// Computes in **O(n)** time. #[inline] - pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self - where - S: BuildHasher, - { + pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self { if n == 0 { IndexMap { core: IndexMapCore::new(), @@ -182,6 +178,21 @@ impl IndexMap { } } + /// Create a new map with `hash_builder` + pub fn with_hasher(hash_builder: S) -> Self { + Self::with_capacity_and_hasher(0, hash_builder) + } + + /// Computes in **O(1)** time. + pub fn capacity(&self) -> usize { + self.core.capacity() + } + + /// Return a reference to the map's `BuildHasher`. + pub fn hasher(&self) -> &S { + &self.hash_builder + } + /// Return the number of key-value pairs in the map. /// /// Computes in **O(1)** time. @@ -198,33 +209,42 @@ impl IndexMap { self.len() == 0 } - /// Create a new map with `hash_builder` - pub fn with_hasher(hash_builder: S) -> Self - where - S: BuildHasher, - { - Self::with_capacity_and_hasher(0, hash_builder) + /// Return an iterator over the key-value pairs of the map, in their order + pub fn iter(&self) -> Iter<'_, K, V> { + Iter { + iter: self.as_entries().iter(), + } } - /// Return a reference to the map's `BuildHasher`. - pub fn hasher(&self) -> &S - where - S: BuildHasher, - { - &self.hash_builder + /// Return an iterator over the key-value pairs of the map, in their order + pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { + IterMut { + iter: self.as_entries_mut().iter_mut(), + } } - /// Computes in **O(1)** time. - pub fn capacity(&self) -> usize { - self.core.capacity() + /// Return an iterator over the keys of the map, in their order + 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<'_, K, V> { + Values { + iter: self.as_entries().iter(), + } + } + + /// Return an iterator over mutable references to the the values of the map, + /// in their order + pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> { + ValuesMut { + iter: self.as_entries_mut().iter_mut(), + } } -} -impl IndexMap -where - K: Hash + Eq, - S: BuildHasher, -{ /// Remove all key-value pairs in the map, while preserving its capacity. /// /// Computes in **O(n)** time. @@ -232,6 +252,20 @@ where self.core.clear(); } + /// 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<'_, K, V> { + Drain { + iter: self.core.drain(range), + } + } +} + +impl IndexMap +where + K: Hash + Eq, + S: BuildHasher, +{ /// Reserve capacity for `additional` more key-value pairs. /// /// Computes in **O(n)** time. @@ -296,42 +330,6 @@ where self.core.entry(hash, key) } - /// Return an iterator over the key-value pairs of the map, in their order - 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<'_, 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<'_, 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<'_, K, V> { - Values { - iter: self.as_entries().iter(), - } - } - - /// Return an iterator over mutable references to the the values of the map, - /// in their order - pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> { - ValuesMut { - iter: self.as_entries_mut().iter_mut(), - } - } - /// Return `true` if an equivalent to `key` exists in the map. /// /// Computes in **O(1)** time (average). @@ -660,14 +658,6 @@ where pub fn reverse(&mut self) { self.core.reverse() } - - /// 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<'_, K, V> { - Drain { - iter: self.core.drain(range), - } - } } impl IndexMap { @@ -963,11 +953,7 @@ impl DoubleEndedIterator for Drain<'_, K, V> { double_ended_iterator_methods!(Bucket::key_value); } -impl<'a, K, V, S> IntoIterator for &'a IndexMap -where - K: Hash + Eq, - S: BuildHasher, -{ +impl<'a, K, V, S> IntoIterator for &'a IndexMap { type Item = (&'a K, &'a V); type IntoIter = Iter<'a, K, V>; fn into_iter(self) -> Self::IntoIter { @@ -975,11 +961,7 @@ where } } -impl<'a, K, V, S> IntoIterator for &'a mut IndexMap -where - K: Hash + Eq, - S: BuildHasher, -{ +impl<'a, K, V, S> IntoIterator for &'a mut IndexMap { type Item = (&'a K, &'a mut V); type IntoIter = IterMut<'a, K, V>; fn into_iter(self) -> Self::IntoIter { @@ -987,11 +969,7 @@ where } } -impl IntoIterator for IndexMap -where - K: Hash + Eq, - S: BuildHasher, -{ +impl IntoIterator for IndexMap { type Item = (K, V); type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -1099,7 +1077,7 @@ where impl Default for IndexMap where - S: BuildHasher + Default, + S: Default, { /// Return an empty `IndexMap` fn default() -> Self { diff --git a/src/rayon/map.rs b/src/rayon/map.rs index 204c86dc..a992b793 100644 --- a/src/rayon/map.rs +++ b/src/rayon/map.rs @@ -21,9 +21,8 @@ use crate::IndexMap; /// Requires crate feature `"rayon"`. impl IntoParallelIterator for IndexMap where - K: Hash + Eq + Send, + K: Send, V: Send, - S: BuildHasher, { type Item = (K, V); type Iter = IntoParIter; @@ -66,9 +65,8 @@ impl IndexedParallelIterator for IntoParIter { /// Requires crate feature `"rayon"`. impl<'a, K, V, S> IntoParallelIterator for &'a IndexMap where - K: Hash + Eq + Sync, + K: Sync, V: Sync, - S: BuildHasher, { type Item = (&'a K, &'a V); type Iter = ParIter<'a, K, V>; @@ -117,7 +115,7 @@ impl IndexedParallelIterator for ParIter<'_, K, V> { /// Requires crate feature `"rayon"`. impl<'a, K, V, S> IntoParallelIterator for &'a mut IndexMap where - K: Hash + Eq + Sync + Send, + K: Sync + Send, V: Send, S: BuildHasher, { @@ -159,9 +157,8 @@ impl IndexedParallelIterator for ParIterMut<'_, K, V> { /// See also the `IntoParallelIterator` implementations. impl IndexMap where - K: Hash + Eq + Sync, + K: Sync, V: Sync, - S: BuildHasher, { /// Return a parallel iterator over the keys of the map. /// @@ -182,7 +179,14 @@ where entries: self.as_entries(), } } +} +impl IndexMap +where + K: Hash + Eq + Sync, + V: Sync, + S: BuildHasher, +{ /// Returns `true` if `self` contains all of the same key-value pairs as `other`, /// regardless of each map's indexed order, determined in parallel. pub fn par_eq(&self, other: &IndexMap) -> bool @@ -269,9 +273,8 @@ impl IndexedParallelIterator for ParValues<'_, K, V> { /// Requires crate feature `"rayon"`. impl IndexMap where - K: Hash + Eq + Send, + K: Send, V: Send, - S: BuildHasher, { /// Return a parallel iterator over mutable references to the the values of the map /// @@ -282,7 +285,14 @@ where entries: self.as_entries_mut(), } } +} +impl IndexMap +where + K: Hash + Eq + Send, + V: Send, + S: BuildHasher, +{ /// Sort the map’s key-value pairs in parallel, by the default ordering of the keys. pub fn par_sort_keys(&mut self) where diff --git a/src/rayon/set.rs b/src/rayon/set.rs index 49b8792f..f6e08d48 100644 --- a/src/rayon/set.rs +++ b/src/rayon/set.rs @@ -22,8 +22,7 @@ type Bucket = crate::Bucket; /// Requires crate feature `"rayon"`. impl IntoParallelIterator for IndexSet where - T: Hash + Eq + Send, - S: BuildHasher, + T: Send, { type Item = T; type Iter = IntoParIter; @@ -66,8 +65,7 @@ impl IndexedParallelIterator for IntoParIter { /// Requires crate feature `"rayon"`. impl<'a, T, S> IntoParallelIterator for &'a IndexSet where - T: Hash + Eq + Sync, - S: BuildHasher, + T: Sync, { type Item = &'a T; type Iter = ParIter<'a, T>; diff --git a/src/set.rs b/src/set.rs index c1693c54..c51c8daf 100644 --- a/src/set.rs +++ b/src/set.rs @@ -112,8 +112,7 @@ impl Entries for IndexSet { impl fmt::Debug for IndexSet where - T: fmt::Debug + Hash + Eq, - S: BuildHasher, + T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if cfg!(not(feature = "test_debug")) { @@ -150,15 +149,29 @@ impl IndexSet { /// (Does not allocate if `n` is zero.) /// /// Computes in **O(n)** time. - pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self - where - S: BuildHasher, - { + pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self { IndexSet { map: IndexMap::with_capacity_and_hasher(n, hash_builder), } } + /// Create a new set with `hash_builder` + pub fn with_hasher(hash_builder: S) -> Self { + IndexSet { + map: IndexMap::with_hasher(hash_builder), + } + } + + /// Computes in **O(1)** time. + pub fn capacity(&self) -> usize { + self.map.capacity() + } + + /// Return a reference to the set's `BuildHasher`. + pub fn hasher(&self) -> &S { + self.map.hasher() + } + /// Return the number of elements in the set. /// /// Computes in **O(1)** time. @@ -173,27 +186,26 @@ impl IndexSet { self.map.is_empty() } - /// Create a new set with `hash_builder` - pub fn with_hasher(hash_builder: S) -> Self - where - S: BuildHasher, - { - IndexSet { - map: IndexMap::with_hasher(hash_builder), + /// Return an iterator over the values of the set, in their order + pub fn iter(&self) -> Iter<'_, T> { + Iter { + iter: self.map.keys().iter, } } - /// Return a reference to the set's `BuildHasher`. - pub fn hasher(&self) -> &S - where - S: BuildHasher, - { - self.map.hasher() + /// Remove all elements in the set, while preserving its capacity. + /// + /// Computes in **O(n)** time. + pub fn clear(&mut self) { + self.map.clear(); } - /// Computes in **O(1)** time. - pub fn capacity(&self) -> usize { - self.map.capacity() + /// Clears the `IndexSet`, returning all values as a drain iterator. + /// Keeps the allocated memory for reuse. + pub fn drain(&mut self, range: RangeFull) -> Drain<'_, T> { + Drain { + iter: self.map.drain(range).iter, + } } } @@ -202,13 +214,6 @@ where T: Hash + Eq, S: BuildHasher, { - /// Remove all elements in the set, while preserving its capacity. - /// - /// Computes in **O(n)** time. - pub fn clear(&mut self) { - self.map.clear(); - } - /// Reserve capacity for `additional` more values. /// /// Computes in **O(n)** time. @@ -257,13 +262,6 @@ where } } - /// Return an iterator over the values of the set, in their order - pub fn iter(&self) -> Iter<'_, T> { - Iter { - iter: self.map.keys().iter, - } - } - /// Return an iterator over the values that are in `self` but not `other`. /// /// Values are produced in the same order that they appear in `self`. @@ -555,14 +553,6 @@ where pub fn reverse(&mut self) { self.map.reverse() } - - /// Clears the `IndexSet`, returning all values as a drain iterator. - /// Keeps the allocated memory for reuse. - pub fn drain(&mut self, range: RangeFull) -> Drain<'_, T> { - Drain { - iter: self.map.drain(range).iter, - } - } } impl IndexSet { @@ -702,11 +692,7 @@ impl DoubleEndedIterator for Drain<'_, T> { double_ended_iterator_methods!(Bucket::key); } -impl<'a, T, S> IntoIterator for &'a IndexSet -where - T: Hash + Eq, - S: BuildHasher, -{ +impl<'a, T, S> IntoIterator for &'a IndexSet { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -715,11 +701,7 @@ where } } -impl IntoIterator for IndexSet -where - T: Hash + Eq, - S: BuildHasher, -{ +impl IntoIterator for IndexSet { type Item = T; type IntoIter = IntoIter; @@ -767,7 +749,7 @@ where impl Default for IndexSet where - S: BuildHasher + Default, + S: Default, { /// Return an empty `IndexSet` fn default() -> Self {