1use std::borrow::Borrow;
2use std::hash::{Hash, Hasher};
3use std::ops::Deref;
4use std::sync::Arc;
5
6#[repr(transparent)]
8#[derive(Debug)]
9pub struct Key<K>(Arc<K>)
10where
11 K: Eq + Hash;
12
13impl<K> Key<K>
14where
15 K: Eq + Hash,
16{
17 pub(crate) fn new(key: K) -> Self {
19 Self(Arc::new(key))
20 }
21}
22
23impl<K> Clone for Key<K>
24where
25 K: Eq + Hash,
26{
27 fn clone(&self) -> Self {
28 Self(self.0.clone())
29 }
30}
31
32impl<K> PartialEq<Self> for Key<K>
33where
34 K: Eq + Hash,
35{
36 fn eq(&self, other: &Self) -> bool {
37 self.0 == other.0
38 }
39}
40
41impl<K> Eq for Key<K> where K: Eq + Hash {}
42
43impl<K> Hash for Key<K>
44where
45 K: Eq + Hash,
46{
47 fn hash<H: Hasher>(&self, state: &mut H) {
48 self.0.hash(state)
49 }
50}
51
52impl<K> AsRef<K> for Key<K>
53where
54 K: Eq + Hash,
55{
56 fn as_ref(&self) -> &K {
57 self.0.as_ref()
58 }
59}
60
61impl<K> Borrow<K> for Key<K>
62where
63 K: Eq + Hash,
64{
65 fn borrow(&self) -> &K {
66 self.0.as_ref()
67 }
68}
69
70impl Borrow<str> for Key<String> {
71 fn borrow(&self) -> &str {
72 self.0.as_ref().borrow()
73 }
74}
75
76impl Borrow<[u8]> for Key<String> {
77 fn borrow(&self) -> &[u8] {
78 self.0.as_ref().as_bytes()
79 }
80}
81
82impl<T> Borrow<[T]> for Key<Vec<T>>
83where
84 T: Eq + Hash,
85{
86 fn borrow(&self) -> &[T] {
87 self.0.as_ref().as_slice()
88 }
89}
90
91impl<K> Deref for Key<K>
92where
93 K: Eq + Hash,
94{
95 type Target = K;
96
97 fn deref(&self) -> &Self::Target {
98 self.0.as_ref()
99 }
100}