1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use super::{Entries, IterMut2, KeysMut, Slot, VecMap};
use core::borrow::Borrow;

/// Opt-in mutable access to keys.
///
/// These methods expose `&mut K`, mutable references to the key as it is stored in the map.
///
/// You are allowed to modify the keys in the map **as long as the modified key does not compare
/// equal to another key already in the map**.
///
/// If keys are modified erroneously, you are no longer able to look up values for duplicate keys
/// directly. This is sound (memory safe) but a logical error hazard (just like implementing
/// `PartialEq`, `Eq`, or `Hash` incorrectly would be).
///
/// This trait is sealed and cannot be implemented for types outside this crate.
pub trait MutableKeys: private::Sealed {
    /// The map's key type.
    type Key;

    /// The map's value type.
    type Value;

    /// Return the index, a mutable reference to the key and a mutable reference to the value
    /// stored for `key`, if it is present, else `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecmap::map::{MutableKeys, VecMap};
    ///
    /// let mut map = VecMap::new();
    /// map.insert(1, "a");
    ///
    /// if let Some((_, k, v)) = map.get_full_mut2(&1) {
    ///     *k = 2;
    ///     *v = "b";
    /// }
    /// assert_eq!(map.get(&1), None);
    /// assert_eq!(map.get(&2), Some(&"b"));
    /// ```
    fn get_full_mut2<Q>(&mut self, key: &Q) -> Option<(usize, &mut Self::Key, &mut Self::Value)>
    where
        Self::Key: Borrow<Q>,
        Q: Eq + ?Sized;

    /// Return a mutable reference to the key and a mutable reference to the value stored at
    /// `index`, if it is present, else `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecmap::map::{MutableKeys, VecMap};
    ///
    /// let mut map = VecMap::new();
    /// map.insert(1, "a");
    /// if let Some((k, v)) = map.get_index_mut2(0) {
    ///     *k = 2;
    ///     *v = "b";
    /// }
    /// assert_eq!(map[0], "b");
    /// assert_eq!(map.get(&1), None);
    /// assert_eq!(map.get(&2), Some(&"b"));
    /// ```
    fn get_index_mut2(&mut self, index: usize) -> Option<(&mut Self::Key, &mut Self::Value)>;

    /// Retains only the elements specified by the predicate.
    ///
    /// In other words, remove all pairs `(k, v)` for which `f(&mut k, &mut v)` returns `false`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecmap::map::{MutableKeys, VecMap};
    ///
    /// let mut map: VecMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
    /// map.retain2(|k, v| {
    ///     std::mem::swap(k, v);  
    ///     *v % 2 == 0
    /// });
    /// assert_eq!(map.len(), 4);
    /// assert_eq!(map, VecMap::from([(0, 0), (20, 2), (40, 4), (60, 6)]));
    /// ```
    fn retain2<F>(&mut self, f: F)
    where
        F: FnMut(&mut Self::Key, &mut Self::Value) -> bool;

    /// An iterator visiting all key-value pairs in insertion order, with mutable references to the
    /// values. The iterator element type is `(&'a mut K, &'a mut V)`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecmap::map::{MutableKeys, VecMap};
    ///
    /// let mut map = VecMap::from([
    ///     ('a', 1),
    ///     ('b', 2),
    ///     ('c', 3),
    /// ]);
    ///
    /// // Update all values
    /// for (key, val) in map.iter_mut2() {
    ///     *key = key.to_ascii_uppercase();
    ///     *val *= 2;
    /// }
    ///
    /// assert_eq!(map, VecMap::from([('A', 2), ('B', 4), ('C', 6)]));
    /// ```
    fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value>;

    /// An iterator visiting all keys mutably in insertion order. The iterator element type is `&'a
    /// mut K`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecmap::map::{MutableKeys, VecMap};
    ///
    /// let mut map = VecMap::from([
    ///     (1, "a"),
    ///     (2, "b"),
    ///     (3, "c"),
    /// ]);
    ///
    /// for key in map.keys_mut() {
    ///     *key = *key * 10;
    /// }
    ///
    /// assert_eq!(map, VecMap::from([(10, "a"), (20, "b"), (30, "c")]));
    /// ```
    fn keys_mut(&mut self) -> KeysMut<'_, Self::Key, Self::Value>;
}

impl<K, V> MutableKeys for VecMap<K, V> {
    type Key = K;
    type Value = V;

    fn get_full_mut2<Q>(&mut self, key: &Q) -> Option<(usize, &mut K, &mut V)>
    where
        K: Borrow<Q>,
        Q: Eq + ?Sized,
    {
        self.get_index_of(key).map(|index| {
            let (key, value) = self.base[index].muts();
            (index, key, value)
        })
    }

    fn get_index_mut2(&mut self, index: usize) -> Option<(&mut K, &mut V)> {
        self.base.get_mut(index).map(Slot::muts)
    }

    fn retain2<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut K, &mut V) -> bool,
    {
        self.base.retain_mut(|slot| {
            let (key, value) = slot.muts();
            f(key, value)
        });
    }

    fn iter_mut2(&mut self) -> IterMut2<'_, K, V> {
        IterMut2::new(self.as_entries_mut())
    }

    fn keys_mut(&mut self) -> KeysMut<'_, K, V> {
        KeysMut::new(self.as_entries_mut())
    }
}

mod private {
    pub trait Sealed {}

    impl<K, V> Sealed for super::VecMap<K, V> {}
}