1use std::{
2 collections::{
3 HashMap, HashSet,
4 hash_map::{self, IntoKeys, IntoValues},
5 hash_set,
6 },
7 fmt::{self, Debug, Formatter},
8 ops::{Deref, DerefMut},
9};
10
11use uuid::Uuid;
12
13pub use self::hasher::{UuidBuildHasher, UuidHasher};
14
15mod hasher;
16#[cfg(test)]
17mod tests;
18
19#[derive(Default, Clone, PartialEq, Eq)]
29pub struct UuidMap<V>(HashMap<Uuid, V, UuidBuildHasher>);
30
31#[derive(Default, Clone, PartialEq, Eq)]
41pub struct UuidSet(HashSet<Uuid, UuidBuildHasher>);
42
43impl<V> UuidMap<V> {
44 #[inline]
48 pub fn new() -> Self {
49 Self(HashMap::with_hasher(UuidBuildHasher))
50 }
51
52 #[inline]
56 pub fn with_capacity(capacity: usize) -> Self {
57 Self(HashMap::with_capacity_and_hasher(capacity, UuidBuildHasher))
58 }
59
60 #[inline]
64 pub fn into_keys(self) -> IntoKeys<Uuid, V> {
65 self.0.into_keys()
66 }
67
68 #[inline]
72 pub fn into_values(self) -> IntoValues<Uuid, V> {
73 self.0.into_values()
74 }
75}
76
77impl UuidSet {
78 #[inline]
82 pub fn new() -> Self {
83 Self(HashSet::with_hasher(UuidBuildHasher))
84 }
85
86 #[inline]
90 pub fn with_capacity(capacity: usize) -> Self {
91 Self(HashSet::with_capacity_and_hasher(capacity, UuidBuildHasher))
92 }
93}
94
95impl<V> Deref for UuidMap<V> {
96 type Target = HashMap<Uuid, V, UuidBuildHasher>;
97
98 #[inline]
99 fn deref(&self) -> &Self::Target {
100 &self.0
101 }
102}
103
104impl<V> DerefMut for UuidMap<V> {
105 #[inline]
106 fn deref_mut(&mut self) -> &mut Self::Target {
107 &mut self.0
108 }
109}
110
111impl Deref for UuidSet {
112 type Target = HashSet<Uuid, UuidBuildHasher>;
113
114 #[inline]
115 fn deref(&self) -> &Self::Target {
116 &self.0
117 }
118}
119
120impl DerefMut for UuidSet {
121 #[inline]
122 fn deref_mut(&mut self) -> &mut Self::Target {
123 &mut self.0
124 }
125}
126
127impl<V: Debug> Debug for UuidMap<V> {
128 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
129 Debug::fmt(&self.0, f)
130 }
131}
132
133impl Debug for UuidSet {
134 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
135 Debug::fmt(&self.0, f)
136 }
137}
138
139impl<V> FromIterator<(Uuid, V)> for UuidMap<V> {
140 #[inline]
141 fn from_iter<T: IntoIterator<Item = (Uuid, V)>>(iter: T) -> Self {
142 Self(HashMap::from_iter(iter))
143 }
144}
145
146impl FromIterator<Uuid> for UuidSet {
147 #[inline]
148 fn from_iter<T: IntoIterator<Item = Uuid>>(iter: T) -> Self {
149 Self(HashSet::from_iter(iter))
150 }
151}
152
153impl<V> IntoIterator for UuidMap<V> {
154 type Item = (Uuid, V);
155 type IntoIter = hash_map::IntoIter<Uuid, V>;
156
157 #[inline]
158 fn into_iter(self) -> Self::IntoIter {
159 self.0.into_iter()
160 }
161}
162
163impl IntoIterator for UuidSet {
164 type Item = Uuid;
165 type IntoIter = hash_set::IntoIter<Uuid>;
166
167 #[inline]
168 fn into_iter(self) -> Self::IntoIter {
169 self.0.into_iter()
170 }
171}