zone_alloc/
registry_container.rs1#[cfg(not(feature = "std"))]
2extern crate alloc;
3
4#[cfg(feature = "std")]
5extern crate core;
6
7#[cfg(not(feature = "std"))]
8use alloc::vec::Vec;
9use core::{
10 borrow::Borrow,
11 ops::{
12 Deref,
13 DerefMut,
14 },
15};
16
17use hashbrown::HashMap;
18
19pub trait Key = core::cmp::Eq + core::hash::Hash;
21
22pub(crate) trait RegistryContainer<K, V> {
25 type Item;
26 fn new() -> Self;
27 fn with_capacity(size: usize) -> Self;
28 fn len(&self) -> usize;
29 fn is_empty(&self) -> bool;
30 fn reserve(&mut self, additional: usize);
31 fn get<'a, 'b>(&'a self, key: &K) -> Option<&'b V>
32 where
33 'a: 'b;
34 #[allow(unused)]
35 fn get_mut<'a, 'b>(&'a mut self, key: &K) -> Option<&'b mut V>
36 where
37 'a: 'b;
38}
39
40pub(crate) trait KeyedRegistryContainer<K, V>:
43 RegistryContainer<K, V, Item = (K, V)>
44{
45 #[allow(unused)]
46 fn contains_key<R>(&self, key: &R) -> bool
47 where
48 K: Borrow<R>,
49 R: Key + ?Sized;
50 fn get<'a, 'b, R>(&'a self, key: &R) -> Option<&'b V>
51 where
52 'a: 'b,
53 K: Borrow<R>,
54 R: Key + ?Sized;
55 #[allow(unused)]
56 fn get_mut<'a, 'b, R>(&'a mut self, key: &R) -> Option<&'b mut V>
57 where
58 'a: 'b,
59 K: Borrow<R>,
60 R: Key + ?Sized;
61}
62
63impl<T> RegistryContainer<usize, T> for Vec<T> {
64 type Item = T;
65
66 fn new() -> Self {
67 Vec::new()
68 }
69
70 fn with_capacity(size: usize) -> Self {
71 Vec::with_capacity(size)
72 }
73
74 fn len(&self) -> usize {
75 self.len()
76 }
77
78 fn is_empty(&self) -> bool {
79 self.is_empty()
80 }
81
82 fn reserve(&mut self, additional: usize) {
83 self.reserve(additional)
84 }
85
86 fn get<'a, 'b>(&'a self, key: &usize) -> Option<&'b T>
87 where
88 'a: 'b,
89 {
90 self.deref().get(*key)
91 }
92
93 fn get_mut<'a, 'b>(&'a mut self, key: &usize) -> Option<&'b mut T>
94 where
95 'a: 'b,
96 {
97 self.deref_mut().get_mut(*key)
98 }
99}
100
101impl<K, V> RegistryContainer<K, V> for HashMap<K, V>
102where
103 K: Key,
104{
105 type Item = (K, V);
106
107 fn new() -> Self {
108 HashMap::new()
109 }
110
111 fn with_capacity(size: usize) -> Self {
112 HashMap::with_capacity(size)
113 }
114
115 fn len(&self) -> usize {
116 self.len()
117 }
118
119 fn is_empty(&self) -> bool {
120 self.is_empty()
121 }
122
123 fn reserve(&mut self, additional: usize) {
124 self.reserve(additional)
125 }
126
127 fn get<'a, 'b>(&'a self, key: &K) -> Option<&'b V>
128 where
129 'a: 'b,
130 {
131 self.get(key)
132 }
133
134 fn get_mut<'a, 'b>(&'a mut self, key: &K) -> Option<&'b mut V>
135 where
136 'a: 'b,
137 {
138 self.get_mut(key)
139 }
140}
141
142impl<K, V> KeyedRegistryContainer<K, V> for HashMap<K, V>
143where
144 K: Key,
145{
146 fn contains_key<R>(&self, key: &R) -> bool
147 where
148 K: Borrow<R>,
149 R: Key + ?Sized,
150 {
151 self.contains_key(key)
152 }
153
154 fn get<'a, 'b, R>(&'a self, key: &R) -> Option<&'b V>
155 where
156 'a: 'b,
157 K: Borrow<R>,
158 R: Key + ?Sized,
159 {
160 self.get(key)
161 }
162
163 fn get_mut<'a, 'b, R>(&'a mut self, key: &R) -> Option<&'b mut V>
164 where
165 'a: 'b,
166 K: Borrow<R>,
167 R: Key + ?Sized,
168 {
169 self.get_mut(key)
170 }
171}