mut_set/
lib.rs

1#![doc = include_str!("../README.md")]
2
3pub mod derive {
4    pub use mut_set_derive::item;
5}
6
7mod impl_hashset;
8mod impl_indexmap;
9use core::{
10    borrow::Borrow,
11    hash::{BuildHasher, Hash},
12    ops::Deref,
13};
14
15/// Extend  `HashSet`/`IndexSet` with `get_mut`/`iter_mut`
16pub trait MutSetExt<T: Item> {
17    type IterMut<'a>
18    where
19        Self: 'a;
20    fn get_mut<Q>(&mut self, value: &Q) -> Option<&mut T::IdReadonlyItem>
21    where
22        T: Borrow<Q>,
23        Q: ?Sized + Hash + Eq;
24    fn iter_mut(&mut self) -> Self::IterMut<'_>;
25}
26
27pub trait Item
28where
29    Self: Sized + Eq + Hash + Borrow<Self::Id>,
30{
31    type Id;
32    type IdReadonlyItem: Deref<Target = Self>;
33    fn id(&self) -> &Self::Id {
34        self.borrow()
35    }
36    fn id_readonly(&mut self) -> &mut Self::IdReadonlyItem {
37        unsafe { self.__unsafe_deref_mut() }
38    }
39    /// # Safety
40    ///
41    /// Only for internal usages
42    #[expect(clippy::mut_from_ref)]
43    unsafe fn __unsafe_deref_mut(&self) -> &mut Self::IdReadonlyItem;
44}
45
46#[derive(Debug, Clone, Copy, Default)]
47pub struct NoHashBuildHasher;
48impl BuildHasher for NoHashBuildHasher {
49    type Hasher = NoHashHasher;
50    #[inline]
51    fn build_hasher(&self) -> Self::Hasher {
52        NoHashHasher(0)
53    }
54}
55pub struct NoHashHasher(u64);
56impl core::hash::Hasher for NoHashHasher {
57    #[inline]
58    fn finish(&self) -> u64 {
59        self.0
60    }
61    #[inline]
62    fn write_u64(&mut self, i: u64) {
63        self.0 = i
64    }
65    #[inline]
66    fn write(&mut self, _bytes: &[u8]) {
67        unimplemented!()
68    }
69}