1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use core::hash::Hash;
use core::iter;

use crate::set::SetStorage;

/// [`SetStorage`] for dynamically stored types, using [`hashbrown::HashSet`].
///
/// This allows for dynamic types such as `&'static str` or `u32` to be used as
/// a [`Key`][crate::Key].
///
/// # Examples
///
/// ```
/// use fixed_map::{Key, Set};
///
/// #[derive(Clone, Copy, Key)]
/// enum Key {
///     First(u32),
///     Second,
/// }
///
/// let mut map = Set::new();
/// map.insert(Key::First(1));
/// assert_eq!(map.contains(Key::First(1)), true);
/// assert_eq!(map.contains(Key::First(2)), false);
/// assert_eq!(map.contains(Key::Second), false);
/// ```
#[repr(transparent)]
pub struct HashbrownSetStorage<T> {
    inner: ::hashbrown::HashSet<T>,
}

impl<T> Clone for HashbrownSetStorage<T>
where
    T: Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        HashbrownSetStorage {
            inner: self.inner.clone(),
        }
    }
}

impl<T> PartialEq for HashbrownSetStorage<T>
where
    T: Eq + Hash,
{
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.inner.eq(&other.inner)
    }
}

impl<T> Eq for HashbrownSetStorage<T> where T: Eq + Hash {}

impl<T> SetStorage<T> for HashbrownSetStorage<T>
where
    T: Copy + Eq + Hash,
{
    type Iter<'this> = iter::Copied<::hashbrown::hash_set::Iter<'this, T>> where T: 'this;
    type IntoIter = ::hashbrown::hash_set::IntoIter<T>;

    #[inline]
    fn empty() -> Self {
        Self {
            inner: ::hashbrown::HashSet::new(),
        }
    }

    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    #[inline]
    fn insert(&mut self, value: T) -> bool {
        self.inner.insert(value)
    }

    #[inline]
    fn contains(&self, value: T) -> bool {
        self.inner.contains(&value)
    }

    #[inline]
    fn remove(&mut self, value: T) -> bool {
        self.inner.remove(&value)
    }

    #[inline]
    fn retain<F>(&mut self, mut func: F)
    where
        F: FnMut(T) -> bool,
    {
        self.inner.retain(|&value| func(value));
    }

    #[inline]
    fn clear(&mut self) {
        self.inner.clear();
    }

    #[inline]
    fn iter(&self) -> Self::Iter<'_> {
        self.inner.iter().copied()
    }

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter()
    }
}