Coverage Report

Created: 2025-12-28 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/indexmap-2.5.0/src/map/mutable.rs
Line
Count
Source
1
use core::hash::{BuildHasher, Hash};
2
3
use super::{
4
    Bucket, Entries, Entry, Equivalent, IndexMap, IndexedEntry, IterMut2, OccupiedEntry,
5
    VacantEntry,
6
};
7
8
/// Opt-in mutable access to [`IndexMap`] keys.
9
///
10
/// These methods expose `&mut K`, mutable references to the key as it is stored
11
/// in the map.
12
/// You are allowed to modify the keys in the map **if the modification
13
/// does not change the key’s hash and equality**.
14
///
15
/// If keys are modified erroneously, you can no longer look them up.
16
/// This is sound (memory safe) but a logical error hazard (just like
17
/// implementing `PartialEq`, `Eq`, or `Hash` incorrectly would be).
18
///
19
/// `use` this trait to enable its methods for `IndexMap`.
20
///
21
/// This trait is sealed and cannot be implemented for types outside this crate.
22
pub trait MutableKeys: private::Sealed {
23
    type Key;
24
    type Value;
25
26
    /// Return item index, mutable reference to key and value
27
    ///
28
    /// Computes in **O(1)** time (average).
29
    fn get_full_mut2<Q>(&mut self, key: &Q) -> Option<(usize, &mut Self::Key, &mut Self::Value)>
30
    where
31
        Q: ?Sized + Hash + Equivalent<Self::Key>;
32
33
    /// Return mutable reference to key and value at an index.
34
    ///
35
    /// Valid indices are *0 <= index < self.len()*
36
    ///
37
    /// Computes in **O(1)** time.
38
    fn get_index_mut2(&mut self, index: usize) -> Option<(&mut Self::Key, &mut Self::Value)>;
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
43
    /// Scan through each key-value pair in the map and keep those where the
44
    /// closure `keep` returns `true`.
45
    ///
46
    /// The elements are visited in order, and remaining elements keep their
47
    /// order.
48
    ///
49
    /// Computes in **O(n)** time (average).
50
    fn retain2<F>(&mut self, keep: F)
51
    where
52
        F: FnMut(&mut Self::Key, &mut Self::Value) -> bool;
53
}
54
55
/// Opt-in mutable access to [`IndexMap`] keys.
56
///
57
/// See [`MutableKeys`] for more information.
58
impl<K, V, S> MutableKeys for IndexMap<K, V, S>
59
where
60
    S: BuildHasher,
61
{
62
    type Key = K;
63
    type Value = V;
64
65
0
    fn get_full_mut2<Q>(&mut self, key: &Q) -> Option<(usize, &mut K, &mut V)>
66
0
    where
67
0
        Q: ?Sized + Hash + Equivalent<K>,
68
    {
69
0
        if let Some(i) = self.get_index_of(key) {
70
0
            let entry = &mut self.as_entries_mut()[i];
71
0
            Some((i, &mut entry.key, &mut entry.value))
72
        } else {
73
0
            None
74
        }
75
0
    }
76
77
0
    fn get_index_mut2(&mut self, index: usize) -> Option<(&mut K, &mut V)> {
78
0
        self.as_entries_mut().get_mut(index).map(Bucket::muts)
79
0
    }
80
81
0
    fn iter_mut2(&mut self) -> IterMut2<'_, Self::Key, Self::Value> {
82
0
        IterMut2::new(self.as_entries_mut())
83
0
    }
84
85
0
    fn retain2<F>(&mut self, keep: F)
86
0
    where
87
0
        F: FnMut(&mut K, &mut V) -> bool,
88
    {
89
0
        self.core.retain_in_order(keep);
90
0
    }
91
}
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
110
    /// Gets a mutable reference to the entry's key, either within the map if occupied,
111
    /// or else the new key that was used to find the entry.
112
    fn key_mut(&mut self) -> &mut Self::Key;
113
}
114
115
/// Opt-in mutable access to [`Entry`] keys.
116
///
117
/// See [`MutableEntryKey`] for more information.
118
impl<K, V> MutableEntryKey for Entry<'_, K, V> {
119
    type Key = K;
120
0
    fn key_mut(&mut self) -> &mut Self::Key {
121
0
        match self {
122
0
            Entry::Occupied(e) => e.key_mut(),
123
0
            Entry::Vacant(e) => e.key_mut(),
124
        }
125
0
    }
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
0
    fn key_mut(&mut self) -> &mut Self::Key {
134
0
        self.key_mut()
135
0
    }
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
0
    fn key_mut(&mut self) -> &mut Self::Key {
144
0
        self.key_mut()
145
0
    }
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
0
    fn key_mut(&mut self) -> &mut Self::Key {
154
0
        self.key_mut()
155
0
    }
156
}
157
158
mod private {
159
    pub trait Sealed {}
160
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> {}
166
}