uuid_map/
lib.rs

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/// A wrapper around an [`HashMap`] where the keys are UUIDv4s or UUIDv7s and don't
20/// require hashing.
21///
22/// This uses [`UuidHasher`] as the hasher, so that the random bits of UUIDv4s and
23/// UUIDv7s are used instead of hashing them.
24///
25/// ## Panics
26///
27/// This will panic if trying to use other UUID versions.
28#[derive(Default, Clone, PartialEq, Eq)]
29pub struct UuidMap<V>(HashMap<Uuid, V, UuidBuildHasher>);
30
31/// A wrapper around an [`HashSet`] where the keys are UUIDv4s or UUIDv7s and don't
32/// require hashing.
33///
34/// This uses [`UuidHasher`] as the hasher, so that the random bits of UUIDv4s and
35/// UUIDv7s are used instead of hashing them.
36///
37/// ## Panics
38///
39/// This will panic if trying to use other UUID versions.
40#[derive(Default, Clone, PartialEq, Eq)]
41pub struct UuidSet(HashSet<Uuid, UuidBuildHasher>);
42
43impl<V> UuidMap<V> {
44    /// Creates an empty [`UuidMap`].
45    ///
46    /// See [`HashMap::new()`].
47    #[inline]
48    pub fn new() -> Self {
49        Self(HashMap::with_hasher(UuidBuildHasher))
50    }
51
52    /// Creates an empty [`UuidMap`] with at least the specified capacity.
53    ///
54    /// See [`HashMap::with_capacity()`].
55    #[inline]
56    pub fn with_capacity(capacity: usize) -> Self {
57        Self(HashMap::with_capacity_and_hasher(capacity, UuidBuildHasher))
58    }
59
60    /// Creates a consuming iterator visiting all UUIDs in arbitrary order.
61    ///
62    /// See [`HashMap::into_keys()`].
63    #[inline]
64    pub fn into_keys(self) -> IntoKeys<Uuid, V> {
65        self.0.into_keys()
66    }
67
68    /// Creates a consuming iterator visiting all values in arbitrary order.
69    ///
70    /// See [`HashMap::into_values()`].
71    #[inline]
72    pub fn into_values(self) -> IntoValues<Uuid, V> {
73        self.0.into_values()
74    }
75}
76
77impl UuidSet {
78    /// Creates an empty [`UuidSet`].
79    ///
80    /// See [`HashSet::new()`].
81    #[inline]
82    pub fn new() -> Self {
83        Self(HashSet::with_hasher(UuidBuildHasher))
84    }
85
86    /// Creates an empty [`UuidSet`] with at least the specified capacity.
87    ///
88    /// See [`HashSet::with_capacity()`].
89    #[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}