| 1 | use super::{Entry, Equivalent, IndexedEntry, IterMut2, OccupiedEntry, OrderMap, VacantEntry}; |
| 2 | use core::hash::{BuildHasher, Hash}; |
| 3 | use indexmap::map::MutableEntryKey as _; |
| 4 | use indexmap::map::MutableKeys as _; |
| 5 | |
| 6 | /// Opt-in mutable access to [`OrderMap`] keys. |
| 7 | /// |
| 8 | /// These methods expose `&mut K`, mutable references to the key as it is stored |
| 9 | /// in the map. |
| 10 | /// You are allowed to modify the keys in the map **if the modification |
| 11 | /// does not change the key's hash and equality**. |
| 12 | /// |
| 13 | /// If keys are modified erroneously, you can no longer look them up. |
| 14 | /// This is sound (memory safe) but a logical error hazard (just like |
| 15 | /// implementing `PartialEq`, `Eq`, or `Hash` incorrectly would be). |
| 16 | /// |
| 17 | /// `use` this trait to enable its methods for `OrderMap`. |
| 18 | /// |
| 19 | /// This trait is sealed and cannot be implemented for types outside this crate. |
| 20 | #[expect(private_bounds)] |
| 21 | pub trait MutableKeys: Sealed { |
| 22 | type Key; |
| 23 | type Value; |
| 24 | |
| 25 | /// Return item index, mutable reference to key and value |
| 26 | /// |
| 27 | /// Computes in **O(1)** time (average). |
| 28 | fn get_full_mut2<Q>(&mut self, key: &Q) -> Option<(usize, &mut Self::Key, &mut Self::Value)> |
| 29 | where |
| 30 | Q: ?Sized + Hash + Equivalent<Self::Key>; |
| 31 | |
| 32 | /// Return mutable reference to key and value at an index. |
| 33 | /// |
| 34 | /// Valid indices are `0 <= index < self.len()`. |
| 35 | /// |
| 36 | /// Computes in **O(1)** time. |
| 37 | fn get_index_mut2(&mut self, index: usize) -> Option<(&mut Self::Key, &mut Self::Value)>; |
| 38 | |
| 39 | /// Return an iterator over the key-value pairs of the map, in their order |
| 40 | fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value>; |
| 41 | |
| 42 | /// Scan through each key-value pair in the map and keep those where the |
| 43 | /// closure `keep` returns `true`. |
| 44 | /// |
| 45 | /// The elements are visited in order, and remaining elements keep their |
| 46 | /// order. |
| 47 | /// |
| 48 | /// Computes in **O(n)** time (average). |
| 49 | fn retain2<F>(&mut self, keep: F) |
| 50 | where |
| 51 | F: FnMut(&mut Self::Key, &mut Self::Value) -> bool; |
| 52 | } |
| 53 | |
| 54 | /// Opt-in mutable access to [`OrderMap`] keys. |
| 55 | /// |
| 56 | /// See [`MutableKeys`] for more information. |
| 57 | impl<K, V, S> MutableKeys for OrderMap<K, V, S> |
| 58 | where |
| 59 | S: BuildHasher, |
| 60 | { |
| 61 | type Key = K; |
| 62 | type Value = V; |
| 63 | |
| 64 | fn get_full_mut2<Q>(&mut self, key: &Q) -> Option<(usize, &mut K, &mut V)> |
| 65 | where |
| 66 | Q: ?Sized + Hash + Equivalent<K>, |
| 67 | { |
| 68 | self.inner.get_full_mut2(key) |
| 69 | } |
| 70 | |
| 71 | fn get_index_mut2(&mut self, index: usize) -> Option<(&mut K, &mut V)> { |
| 72 | self.inner.get_index_mut2(index) |
| 73 | } |
| 74 | |
| 75 | fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value> { |
| 76 | self.inner.iter_mut2() |
| 77 | } |
| 78 | |
| 79 | fn retain2<F>(&mut self, keep: F) |
| 80 | where |
| 81 | F: FnMut(&mut K, &mut V) -> bool, |
| 82 | { |
| 83 | self.inner.retain2(keep); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Opt-in mutable access to [`Entry`] keys. |
| 88 | /// |
| 89 | /// These methods expose `&mut K`, mutable references to the key as it is stored |
| 90 | /// in the map. |
| 91 | /// You are allowed to modify the keys in the map **if the modification |
| 92 | /// does not change the key's hash and equality**. |
| 93 | /// |
| 94 | /// If keys are modified erroneously, you can no longer look them up. |
| 95 | /// This is sound (memory safe) but a logical error hazard (just like |
| 96 | /// implementing `PartialEq`, `Eq`, or `Hash` incorrectly would be). |
| 97 | /// |
| 98 | /// `use` this trait to enable its methods for `Entry`. |
| 99 | /// |
| 100 | /// This trait is sealed and cannot be implemented for types outside this crate. |
| 101 | #[expect(private_bounds)] |
| 102 | pub trait MutableEntryKey: Sealed { |
| 103 | type Key; |
| 104 | |
| 105 | /// Gets a mutable reference to the entry's key, either within the map if occupied, |
| 106 | /// or else the new key that was used to find the entry. |
| 107 | fn key_mut(&mut self) -> &mut Self::Key; |
| 108 | } |
| 109 | |
| 110 | /// Opt-in mutable access to [`Entry`] keys. |
| 111 | /// |
| 112 | /// See [`MutableEntryKey`] for more information. |
| 113 | impl<K, V> MutableEntryKey for Entry<'_, K, V> { |
| 114 | type Key = K; |
| 115 | fn key_mut(&mut self) -> &mut Self::Key { |
| 116 | match self { |
| 117 | Entry::Occupied(e) => e.key_mut(), |
| 118 | Entry::Vacant(e) => e.key_mut(), |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// Opt-in mutable access to [`OccupiedEntry`] keys. |
| 124 | /// |
| 125 | /// See [`MutableEntryKey`] for more information. |
| 126 | impl<K, V> MutableEntryKey for OccupiedEntry<'_, K, V> { |
| 127 | type Key = K; |
| 128 | fn key_mut(&mut self) -> &mut Self::Key { |
| 129 | self.inner.key_mut() |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// Opt-in mutable access to [`VacantEntry`] keys. |
| 134 | /// |
| 135 | /// See [`MutableEntryKey`] for more information. |
| 136 | impl<K, V> MutableEntryKey for VacantEntry<'_, K, V> { |
| 137 | type Key = K; |
| 138 | fn key_mut(&mut self) -> &mut Self::Key { |
| 139 | self.inner.key_mut() |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// Opt-in mutable access to [`IndexedEntry`] keys. |
| 144 | /// |
| 145 | /// See [`MutableEntryKey`] for more information. |
| 146 | impl<K, V> MutableEntryKey for IndexedEntry<'_, K, V> { |
| 147 | type Key = K; |
| 148 | fn key_mut(&mut self) -> &mut Self::Key { |
| 149 | self.inner.key_mut() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | trait Sealed {} |
| 154 | |
| 155 | impl<K, V, S> Sealed for OrderMap<K, V, S> {} |
| 156 | impl<K, V> Sealed for Entry<'_, K, V> {} |
| 157 | impl<K, V> Sealed for OccupiedEntry<'_, K, V> {} |
| 158 | impl<K, V> Sealed for VacantEntry<'_, K, V> {} |
| 159 | impl<K, V> Sealed for IndexedEntry<'_, K, V> {} |