Skip to content

Commit 44a609f

Browse files
bors[bot]jeandudey
andauthored
Merge #331
331: Relax trait requirements on IndexMap/IndexSet. r=korken89 a=jeandudey Some requirements are not necessary and the specification can be removed/simplified. This makes these types a bit more ergonomic when using them in a trait or function definition as not all traits need to be specified for some cases. Co-authored-by: Jean-Pierre De Jesus DIAZ <me@jeandudey.tech>
2 parents 2bf9286 + d580244 commit 44a609f

File tree

3 files changed

+107
-118
lines changed

3 files changed

+107
-118
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` and `IndexSet`.
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

src/indexset.rs

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ impl<T, S, const N: usize> IndexSet<T, BuildHasherDefault<S>, N> {
9292
}
9393
}
9494

95-
impl<T, S, const N: usize> IndexSet<T, S, N>
96-
where
97-
T: Eq + Hash,
98-
S: BuildHasher,
99-
{
95+
impl<T, S, const N: usize> IndexSet<T, S, N> {
10096
/// Returns the number of elements the set can hold
10197
///
10298
/// # Examples
@@ -147,6 +143,60 @@ where
147143
self.map.last().map(|(k, _v)| k)
148144
}
149145

146+
/// Returns the number of elements in the set.
147+
///
148+
/// # Examples
149+
///
150+
/// ```
151+
/// use heapless::FnvIndexSet;
152+
///
153+
/// let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
154+
/// assert_eq!(v.len(), 0);
155+
/// v.insert(1).unwrap();
156+
/// assert_eq!(v.len(), 1);
157+
/// ```
158+
pub fn len(&self) -> usize {
159+
self.map.len()
160+
}
161+
162+
/// Returns `true` if the set contains no elements.
163+
///
164+
/// # Examples
165+
///
166+
/// ```
167+
/// use heapless::FnvIndexSet;
168+
///
169+
/// let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
170+
/// assert!(v.is_empty());
171+
/// v.insert(1).unwrap();
172+
/// assert!(!v.is_empty());
173+
/// ```
174+
pub fn is_empty(&self) -> bool {
175+
self.map.is_empty()
176+
}
177+
178+
/// Clears the set, removing all values.
179+
///
180+
/// # Examples
181+
///
182+
/// ```
183+
/// use heapless::FnvIndexSet;
184+
///
185+
/// let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
186+
/// v.insert(1).unwrap();
187+
/// v.clear();
188+
/// assert!(v.is_empty());
189+
/// ```
190+
pub fn clear(&mut self) {
191+
self.map.clear()
192+
}
193+
}
194+
195+
impl<T, S, const N: usize> IndexSet<T, S, N>
196+
where
197+
T: Eq + Hash,
198+
S: BuildHasher,
199+
{
150200
/// Visits the values representing the difference, i.e. the values that are in `self` but not in
151201
/// `other`.
152202
///
@@ -277,54 +327,6 @@ where
277327
self.iter().chain(other.difference(self))
278328
}
279329

280-
/// Returns the number of elements in the set.
281-
///
282-
/// # Examples
283-
///
284-
/// ```
285-
/// use heapless::FnvIndexSet;
286-
///
287-
/// let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
288-
/// assert_eq!(v.len(), 0);
289-
/// v.insert(1).unwrap();
290-
/// assert_eq!(v.len(), 1);
291-
/// ```
292-
pub fn len(&self) -> usize {
293-
self.map.len()
294-
}
295-
296-
/// Returns `true` if the set contains no elements.
297-
///
298-
/// # Examples
299-
///
300-
/// ```
301-
/// use heapless::FnvIndexSet;
302-
///
303-
/// let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
304-
/// assert!(v.is_empty());
305-
/// v.insert(1).unwrap();
306-
/// assert!(!v.is_empty());
307-
/// ```
308-
pub fn is_empty(&self) -> bool {
309-
self.map.is_empty()
310-
}
311-
312-
/// Clears the set, removing all values.
313-
///
314-
/// # Examples
315-
///
316-
/// ```
317-
/// use heapless::FnvIndexSet;
318-
///
319-
/// let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
320-
/// v.insert(1).unwrap();
321-
/// v.clear();
322-
/// assert!(v.is_empty());
323-
/// ```
324-
pub fn clear(&mut self) {
325-
self.map.clear()
326-
}
327-
328330
/// Returns `true` if the set contains a value.
329331
///
330332
/// The value may be any borrowed form of the set's value type, but `Hash` and `Eq` on the
@@ -473,7 +475,7 @@ where
473475

474476
impl<T, S, const N: usize> Clone for IndexSet<T, S, N>
475477
where
476-
T: Eq + Hash + Clone,
478+
T: Clone,
477479
S: Clone,
478480
{
479481
fn clone(&self) -> Self {
@@ -485,8 +487,7 @@ where
485487

486488
impl<T, S, const N: usize> fmt::Debug for IndexSet<T, S, N>
487489
where
488-
T: Eq + Hash + fmt::Debug,
489-
S: BuildHasher,
490+
T: fmt::Debug,
490491
{
491492
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
492493
f.debug_set().entries(self.iter()).finish()
@@ -495,8 +496,7 @@ where
495496

496497
impl<T, S, const N: usize> Default for IndexSet<T, S, N>
497498
where
498-
T: Eq + Hash,
499-
S: BuildHasher + Default,
499+
S: Default,
500500
{
501501
fn default() -> Self {
502502
IndexSet {

0 commit comments

Comments
 (0)