1use std::marker::PhantomData;
4
5pub struct Handle<T> {
7 pub id: u64,
9 _marker: PhantomData<T>,
10}
11
12impl<T> Clone for Handle<T> {
13 fn clone(&self) -> Self {
14 *self
15 }
16}
17
18impl<T> Copy for Handle<T> {}
19
20impl<T> std::fmt::Debug for Handle<T> {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 f.debug_struct("Handle").field("id", &self.id).finish()
23 }
24}
25
26impl<T> PartialEq for Handle<T> {
27 fn eq(&self, other: &Self) -> bool {
28 self.id == other.id
29 }
30}
31
32impl<T> Eq for Handle<T> {}
33
34impl<T> std::hash::Hash for Handle<T> {
35 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
36 self.id.hash(state);
37 }
38}
39
40impl<T> Handle<T> {
41 pub fn new(id: u64) -> Self {
43 Self {
44 id,
45 _marker: PhantomData,
46 }
47 }
48
49 pub fn id(&self) -> u64 {
51 self.id
52 }
53}
54
55#[derive(Default)]
57pub struct HandleAllocator {
58 next_id: u64,
59}
60
61impl HandleAllocator {
62 pub fn new() -> Self {
64 Self { next_id: 0 }
65 }
66
67 pub fn allocate<T>(&mut self) -> Handle<T> {
69 let id = self.next_id;
70 self.next_id += 1;
71 Handle::new(id)
72 }
73}