ordermap/map/
mutable.rs

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