|
1 | 1 | use core::hash::{BuildHasher, Hash}; |
2 | 2 |
|
3 | | -use super::{Bucket, Entries, Equivalent, IndexMap}; |
| 3 | +use super::{ |
| 4 | + Bucket, Entries, Entry, Equivalent, IndexMap, IndexedEntry, IterMut2, OccupiedEntry, |
| 5 | + VacantEntry, |
| 6 | +}; |
4 | 7 |
|
5 | 8 | /// Opt-in mutable access to [`IndexMap`] keys. |
6 | 9 | /// |
@@ -34,6 +37,9 @@ pub trait MutableKeys: private::Sealed { |
34 | 37 | /// Computes in **O(1)** time. |
35 | 38 | fn get_index_mut2(&mut self, index: usize) -> Option<(&mut Self::Key, &mut Self::Value)>; |
36 | 39 |
|
| 40 | + /// Return an iterator over the key-value pairs of the map, in their order |
| 41 | + fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value>; |
| 42 | + |
37 | 43 | /// Scan through each key-value pair in the map and keep those where the |
38 | 44 | /// closure `keep` returns `true`. |
39 | 45 | /// |
|
72 | 78 | self.as_entries_mut().get_mut(index).map(Bucket::muts) |
73 | 79 | } |
74 | 80 |
|
| 81 | + fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value> { |
| 82 | + IterMut2::new(self.as_entries_mut()) |
| 83 | + } |
| 84 | + |
75 | 85 | fn retain2<F>(&mut self, keep: F) |
76 | 86 | where |
77 | 87 | F: FnMut(&mut K, &mut V) -> bool, |
|
80 | 90 | } |
81 | 91 | } |
82 | 92 |
|
| 93 | +/// Opt-in mutable access to [`Entry`] keys. |
| 94 | +/// |
| 95 | +/// These methods expose `&mut K`, mutable references to the key as it is stored |
| 96 | +/// in the map. |
| 97 | +/// You are allowed to modify the keys in the map **if the modification |
| 98 | +/// does not change the key’s hash and equality**. |
| 99 | +/// |
| 100 | +/// If keys are modified erroneously, you can no longer look them up. |
| 101 | +/// This is sound (memory safe) but a logical error hazard (just like |
| 102 | +/// implementing `PartialEq`, `Eq`, or `Hash` incorrectly would be). |
| 103 | +/// |
| 104 | +/// `use` this trait to enable its methods for `Entry`. |
| 105 | +/// |
| 106 | +/// This trait is sealed and cannot be implemented for types outside this crate. |
| 107 | +pub trait MutableEntryKey: private::Sealed { |
| 108 | + type Key; |
| 109 | + fn key_mut(&mut self) -> &mut Self::Key; |
| 110 | +} |
| 111 | + |
| 112 | +/// Opt-in mutable access to [`Entry`] keys. |
| 113 | +/// |
| 114 | +/// See [`MutableEntryKey`] for more information. |
| 115 | +impl<K, V> MutableEntryKey for Entry<'_, K, V> { |
| 116 | + type Key = K; |
| 117 | + |
| 118 | + /// Gets a mutable reference to the entry's key, either within the map if occupied, |
| 119 | + /// or else the new key that was used to find the entry. |
| 120 | + fn key_mut(&mut self) -> &mut Self::Key { |
| 121 | + match self { |
| 122 | + Entry::Occupied(e) => e.key_mut(), |
| 123 | + Entry::Vacant(e) => e.key_mut(), |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Opt-in mutable access to [`OccupiedEntry`] keys. |
| 129 | +/// |
| 130 | +/// See [`MutableEntryKey`] for more information. |
| 131 | +impl<K, V> MutableEntryKey for OccupiedEntry<'_, K, V> { |
| 132 | + type Key = K; |
| 133 | + fn key_mut(&mut self) -> &mut Self::Key { |
| 134 | + self.key_mut() |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// Opt-in mutable access to [`VacantEntry`] keys. |
| 139 | +/// |
| 140 | +/// See [`MutableEntryKey`] for more information. |
| 141 | +impl<K, V> MutableEntryKey for VacantEntry<'_, K, V> { |
| 142 | + type Key = K; |
| 143 | + fn key_mut(&mut self) -> &mut Self::Key { |
| 144 | + self.key_mut() |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/// Opt-in mutable access to [`IndexedEntry`] keys. |
| 149 | +/// |
| 150 | +/// See [`MutableEntryKey`] for more information. |
| 151 | +impl<K, V> MutableEntryKey for IndexedEntry<'_, K, V> { |
| 152 | + type Key = K; |
| 153 | + fn key_mut(&mut self) -> &mut Self::Key { |
| 154 | + self.key_mut() |
| 155 | + } |
| 156 | +} |
| 157 | + |
83 | 158 | mod private { |
84 | 159 | pub trait Sealed {} |
85 | 160 |
|
86 | 161 | impl<K, V, S> Sealed for super::IndexMap<K, V, S> {} |
| 162 | + impl<K, V> Sealed for super::Entry<'_, K, V> {} |
| 163 | + impl<K, V> Sealed for super::OccupiedEntry<'_, K, V> {} |
| 164 | + impl<K, V> Sealed for super::VacantEntry<'_, K, V> {} |
| 165 | + impl<K, V> Sealed for super::IndexedEntry<'_, K, V> {} |
87 | 166 | } |
0 commit comments