Skip to content

Commit c9cc18a

Browse files
committed
relax trait requirements on IndexMap
Signed-off-by: Jean-Pierre De Jesus DIAZ <me@jeandudey.tech>
1 parent 2bf9286 commit c9cc18a

File tree

2 files changed

+49
-60
lines changed

2 files changed

+49
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
- renamed `pool::singleton::arc::Pool` to `ArcPool` and moved it into the `pool::arc` module
2323
- [breaking-change] changed the target support of memory pool API to only support 32-bit x86 and a
2424
subset of ARM targets. See the module level documentation of the `pool` module for details
25+
- relax trait requirements on `IndexMap`.
2526

2627
- [breaking-change] this crate now depends on `atomic-polyfill` v1.0.1, meaning that targets that
2728
require a polyfill need a `critical-section` **v1.x.x** implementation.

src/indexmap.rs

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ where
313313

314314
impl<K, V, const N: usize> Clone for CoreMap<K, V, N>
315315
where
316-
K: Eq + Hash + Clone,
316+
K: Clone,
317317
V: Clone,
318318
{
319319
fn clone(&self) -> Self {
@@ -499,12 +499,7 @@ impl<K, V, S, const N: usize> IndexMap<K, V, BuildHasherDefault<S>, N> {
499499
}
500500
}
501501

502-
impl<K, V, S, const N: usize> IndexMap<K, V, S, N>
503-
where
504-
K: Eq + Hash,
505-
S: BuildHasher,
506-
{
507-
/* Public API */
502+
impl<K, V, S, const N: usize> IndexMap<K, V, S, N> {
508503
/// Returns the number of elements the map can hold
509504
pub fn capacity(&self) -> usize {
510505
N
@@ -652,39 +647,6 @@ where
652647
.map(|bucket| (&bucket.key, &mut bucket.value))
653648
}
654649

655-
/// Returns an entry for the corresponding key
656-
/// ```
657-
/// use heapless::FnvIndexMap;
658-
/// use heapless::Entry;
659-
/// let mut map = FnvIndexMap::<_, _, 16>::new();
660-
/// if let Entry::Vacant(v) = map.entry("a") {
661-
/// v.insert(1).unwrap();
662-
/// }
663-
/// if let Entry::Occupied(mut o) = map.entry("a") {
664-
/// println!("found {}", *o.get()); // Prints 1
665-
/// o.insert(2);
666-
/// }
667-
/// // Prints 2
668-
/// println!("val: {}", *map.get("a").unwrap());
669-
/// ```
670-
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, N> {
671-
let hash_val = hash_with(&key, &self.build_hasher);
672-
if let Some((probe, pos)) = self.core.find(hash_val, &key) {
673-
Entry::Occupied(OccupiedEntry {
674-
key,
675-
probe,
676-
pos,
677-
core: &mut self.core,
678-
})
679-
} else {
680-
Entry::Vacant(VacantEntry {
681-
key,
682-
hash_val,
683-
core: &mut self.core,
684-
})
685-
}
686-
}
687-
688650
/// Return the number of key-value pairs in the map.
689651
///
690652
/// Computes in **O(1)** time.
@@ -735,6 +697,46 @@ where
735697
*pos = None;
736698
}
737699
}
700+
}
701+
702+
impl<K, V, S, const N: usize> IndexMap<K, V, S, N>
703+
where
704+
K: Eq + Hash,
705+
S: BuildHasher,
706+
{
707+
/* Public API */
708+
/// Returns an entry for the corresponding key
709+
/// ```
710+
/// use heapless::FnvIndexMap;
711+
/// use heapless::Entry;
712+
/// let mut map = FnvIndexMap::<_, _, 16>::new();
713+
/// if let Entry::Vacant(v) = map.entry("a") {
714+
/// v.insert(1).unwrap();
715+
/// }
716+
/// if let Entry::Occupied(mut o) = map.entry("a") {
717+
/// println!("found {}", *o.get()); // Prints 1
718+
/// o.insert(2);
719+
/// }
720+
/// // Prints 2
721+
/// println!("val: {}", *map.get("a").unwrap());
722+
/// ```
723+
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, N> {
724+
let hash_val = hash_with(&key, &self.build_hasher);
725+
if let Some((probe, pos)) = self.core.find(hash_val, &key) {
726+
Entry::Occupied(OccupiedEntry {
727+
key,
728+
probe,
729+
pos,
730+
core: &mut self.core,
731+
})
732+
} else {
733+
Entry::Vacant(VacantEntry {
734+
key,
735+
hash_val,
736+
core: &mut self.core,
737+
})
738+
}
739+
}
738740

739741
/// Returns a reference to the value corresponding to the key.
740742
///
@@ -931,7 +933,7 @@ where
931933

932934
impl<K, V, S, const N: usize> Clone for IndexMap<K, V, S, N>
933935
where
934-
K: Eq + Hash + Clone,
936+
K: Clone,
935937
V: Clone,
936938
S: Clone,
937939
{
@@ -945,9 +947,8 @@ where
945947

946948
impl<K, V, S, const N: usize> fmt::Debug for IndexMap<K, V, S, N>
947949
where
948-
K: Eq + Hash + fmt::Debug,
950+
K: fmt::Debug,
949951
V: fmt::Debug,
950-
S: BuildHasher,
951952
{
952953
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
953954
f.debug_map().entries(self.iter()).finish()
@@ -956,8 +957,7 @@ where
956957

957958
impl<K, V, S, const N: usize> Default for IndexMap<K, V, S, N>
958959
where
959-
K: Eq + Hash,
960-
S: BuildHasher + Default,
960+
S: Default,
961961
{
962962
fn default() -> Self {
963963
// Const assert
@@ -1052,11 +1052,7 @@ impl<K, V, const N: usize> Iterator for IntoIter<K, V, N> {
10521052
}
10531053
}
10541054

1055-
impl<K, V, S, const N: usize> IntoIterator for IndexMap<K, V, S, N>
1056-
where
1057-
K: Eq + Hash,
1058-
S: BuildHasher,
1059-
{
1055+
impl<K, V, S, const N: usize> IntoIterator for IndexMap<K, V, S, N> {
10601056
type Item = (K, V);
10611057
type IntoIter = IntoIter<K, V, N>;
10621058

@@ -1067,11 +1063,7 @@ where
10671063
}
10681064
}
10691065

1070-
impl<'a, K, V, S, const N: usize> IntoIterator for &'a IndexMap<K, V, S, N>
1071-
where
1072-
K: Eq + Hash,
1073-
S: BuildHasher,
1074-
{
1066+
impl<'a, K, V, S, const N: usize> IntoIterator for &'a IndexMap<K, V, S, N> {
10751067
type Item = (&'a K, &'a V);
10761068
type IntoIter = Iter<'a, K, V>;
10771069

@@ -1080,11 +1072,7 @@ where
10801072
}
10811073
}
10821074

1083-
impl<'a, K, V, S, const N: usize> IntoIterator for &'a mut IndexMap<K, V, S, N>
1084-
where
1085-
K: Eq + Hash,
1086-
S: BuildHasher,
1087-
{
1075+
impl<'a, K, V, S, const N: usize> IntoIterator for &'a mut IndexMap<K, V, S, N> {
10881076
type Item = (&'a K, &'a mut V);
10891077
type IntoIter = IterMut<'a, K, V>;
10901078

0 commit comments

Comments
 (0)