fixed_map/set/storage/
hashbrown.rs

1use core::hash::Hash;
2use core::iter;
3
4use crate::set::SetStorage;
5
6/// [`SetStorage`] for dynamically stored types, using [`hashbrown::HashSet`].
7///
8/// This allows for dynamic types such as `&'static str` or `u32` to be used as
9/// a [`Key`][crate::Key].
10///
11/// # Examples
12///
13/// ```
14/// use fixed_map::{Key, Set};
15///
16/// #[derive(Clone, Copy, Key)]
17/// enum MyKey {
18///     First(u32),
19///     Second,
20/// }
21///
22/// let mut map = Set::new();
23/// map.insert(MyKey::First(1));
24/// assert_eq!(map.contains(MyKey::First(1)), true);
25/// assert_eq!(map.contains(MyKey::First(2)), false);
26/// assert_eq!(map.contains(MyKey::Second), false);
27/// ```
28#[repr(transparent)]
29pub struct HashbrownSetStorage<T> {
30    inner: ::hashbrown::HashSet<T>,
31}
32
33impl<T> Clone for HashbrownSetStorage<T>
34where
35    T: Clone,
36{
37    #[inline]
38    fn clone(&self) -> Self {
39        HashbrownSetStorage {
40            inner: self.inner.clone(),
41        }
42    }
43}
44
45impl<T> PartialEq for HashbrownSetStorage<T>
46where
47    T: Eq + Hash,
48{
49    #[inline]
50    fn eq(&self, other: &Self) -> bool {
51        self.inner.eq(&other.inner)
52    }
53}
54
55impl<T> Eq for HashbrownSetStorage<T> where T: Eq + Hash {}
56
57impl<T> SetStorage<T> for HashbrownSetStorage<T>
58where
59    T: Copy + Eq + Hash,
60{
61    type Iter<'this> = iter::Copied<::hashbrown::hash_set::Iter<'this, T>> where T: 'this;
62    type IntoIter = ::hashbrown::hash_set::IntoIter<T>;
63
64    #[inline]
65    fn empty() -> Self {
66        Self {
67            inner: ::hashbrown::HashSet::new(),
68        }
69    }
70
71    #[inline]
72    fn len(&self) -> usize {
73        self.inner.len()
74    }
75
76    #[inline]
77    fn is_empty(&self) -> bool {
78        self.inner.is_empty()
79    }
80
81    #[inline]
82    fn insert(&mut self, value: T) -> bool {
83        self.inner.insert(value)
84    }
85
86    #[inline]
87    fn contains(&self, value: T) -> bool {
88        self.inner.contains(&value)
89    }
90
91    #[inline]
92    fn remove(&mut self, value: T) -> bool {
93        self.inner.remove(&value)
94    }
95
96    #[inline]
97    fn retain<F>(&mut self, mut func: F)
98    where
99        F: FnMut(T) -> bool,
100    {
101        self.inner.retain(|&value| func(value));
102    }
103
104    #[inline]
105    fn clear(&mut self) {
106        self.inner.clear();
107    }
108
109    #[inline]
110    fn iter(&self) -> Self::Iter<'_> {
111        self.inner.iter().copied()
112    }
113
114    #[inline]
115    fn into_iter(self) -> Self::IntoIter {
116        self.inner.into_iter()
117    }
118}