indexmap/set/
mutable.rs

1use core::hash::{BuildHasher, Hash};
2
3use super::{Equivalent, IndexSet};
4use crate::map::MutableKeys;
5
6/// Opt-in mutable access to [`IndexSet`] values.
7///
8/// These methods expose `&mut T`, mutable references to the value as it is stored
9/// in the set.
10/// You are allowed to modify the values in the set **if the modification
11/// does not change the value's hash and equality**.
12///
13/// If values 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 `IndexSet`.
18///
19/// This trait is sealed and cannot be implemented for types outside this crate.
20#[expect(private_bounds)]
21pub trait MutableValues: Sealed {
22    type Value;
23
24    /// Return item index and mutable reference to the value
25    ///
26    /// Computes in **O(1)** time (average).
27    fn get_full_mut2<Q>(&mut self, value: &Q) -> Option<(usize, &mut Self::Value)>
28    where
29        Q: ?Sized + Hash + Equivalent<Self::Value>;
30
31    /// Return mutable reference to the 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::Value>;
37
38    /// Scan through each value in the set and keep those where the
39    /// closure `keep` returns `true`.
40    ///
41    /// The values are visited in order, and remaining values keep their order.
42    ///
43    /// Computes in **O(n)** time (average).
44    fn retain2<F>(&mut self, keep: F)
45    where
46        F: FnMut(&mut Self::Value) -> bool;
47}
48
49/// Opt-in mutable access to [`IndexSet`] values.
50///
51/// See [`MutableValues`] for more information.
52impl<T, S> MutableValues for IndexSet<T, S>
53where
54    S: BuildHasher,
55{
56    type Value = T;
57
58    fn get_full_mut2<Q>(&mut self, value: &Q) -> Option<(usize, &mut T)>
59    where
60        Q: ?Sized + Hash + Equivalent<T>,
61    {
62        match self.map.get_full_mut2(value) {
63            Some((index, value, ())) => Some((index, value)),
64            None => None,
65        }
66    }
67
68    fn get_index_mut2(&mut self, index: usize) -> Option<&mut T> {
69        match self.map.get_index_mut2(index) {
70            Some((value, ())) => Some(value),
71            None => None,
72        }
73    }
74
75    fn retain2<F>(&mut self, mut keep: F)
76    where
77        F: FnMut(&mut T) -> bool,
78    {
79        self.map.retain2(move |value, ()| keep(value));
80    }
81}
82
83trait Sealed {}
84
85impl<T, S> Sealed for IndexSet<T, S> {}