Debian Codegraph @

src/set/mutable.rs

raw symbols SCIP
1 use super::{Equivalent, OrderSet};
2 use core::hash::{BuildHasher, Hash};
3 use indexmap::set::MutableValues as _;
4
5 /// Opt-in mutable access to [`OrderSet`] values.
6 ///
7 /// These methods expose `&mut T`, mutable references to the value as it is stored
8 /// in the set.
9 /// You are allowed to modify the values in the set **if the modification
10 /// does not change the value's hash and equality**.
11 ///
12 /// If values are modified erroneously, you can no longer look them up.
13 /// This is sound (memory safe) but a logical error hazard (just like
14 /// implementing `PartialEq`, `Eq`, or `Hash` incorrectly would be).
15 ///
16 /// `use` this trait to enable its methods for `OrderSet`.
17 ///
18 /// This trait is sealed and cannot be implemented for types outside this crate.
19 #[expect(private_bounds)]
20 pub trait MutableValues: Sealed {
21 type Value;
22
23 /// Return item index and mutable reference to the value
24 ///
25 /// Computes in **O(1)** time (average).
26 fn get_full_mut2<Q>(&mut self, value: &Q) -> Option<(usize, &mut Self::Value)>
27 where
28 Q: ?Sized + Hash + Equivalent<Self::Value>;
29
30 /// Return mutable reference to the value at an index.
31 ///
32 /// Valid indices are `0 <= index < self.len()`.
33 ///
34 /// Computes in **O(1)** time.
35 fn get_index_mut2(&mut self, index: usize) -> Option<&mut Self::Value>;
36
37 /// Scan through each value in the set and keep those where the
38 /// closure `keep` returns `true`.
39 ///
40 /// The values are visited in order, and remaining values keep their order.
41 ///
42 /// Computes in **O(n)** time (average).
43 fn retain2<F>(&mut self, keep: F)
44 where
45 F: FnMut(&mut Self::Value) -> bool;
46 }
47
48 /// Opt-in mutable access to [`OrderSet`] values.
49 ///
50 /// See [`MutableValues`] for more information.
51 impl<T, S> MutableValues for OrderSet<T, S>
52 where
53 S: BuildHasher,
54 {
55 type Value = T;
56
57 fn get_full_mut2<Q>(&mut self, value: &Q) -> Option<(usize, &mut T)>
58 where
59 Q: ?Sized + Hash + Equivalent<T>,
60 {
61 self.inner.get_full_mut2(value)
62 }
63
64 fn get_index_mut2(&mut self, index: usize) -> Option<&mut T> {
65 self.inner.get_index_mut2(index)
66 }
67
68 fn retain2<F>(&mut self, keep: F)
69 where
70 F: FnMut(&mut T) -> bool,
71 {
72 self.inner.retain2(keep);
73 }
74 }
75
76 trait Sealed {}
77
78 impl<T, S> Sealed for OrderSet<T, S> {}