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
use std::{any::type_name, collections::HashMap, fmt::Debug};
pub struct HashMapId<T> {
id_seed: u64,
store: HashMap<u64, T>,
}
impl<T> HashMapId<T>
where
T: Send + Sync,
{
pub fn new() -> Self {
Self {
id_seed: 0,
store: HashMap::new(),
}
}
pub fn add(&mut self, item: T) -> u64 {
let id = self.id_seed;
self.store.insert(id, item);
self.id_seed += 1;
id
}
pub fn remove(&mut self, id: u64) -> Option<T> {
self.store.remove(&id)
}
pub fn get_mut(&mut self, id: u64) -> Option<&mut T> {
self.store.get_mut(&id)
}
pub fn get(&self, id: u64) -> Option<&T> {
self.store.get(&id)
}
}
impl<T> Default for HashMapId<T>
where
T: Send + Sync,
{
fn default() -> Self {
Self::new()
}
}
impl<T> Debug for HashMapId<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HashMapId")
.field("id_seed", &self.id_seed)
.field("type", &type_name::<T>())
.finish()
}
}