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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::{hash, rc::Rc};
use super::{Token, Unsync};
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct RcToken(UniqueId);
impl RcToken {
pub fn new() -> Self {
RcToken(UniqueId::new())
}
pub fn id(&self) -> RcTokenId {
RcTokenId(self.0.clone())
}
}
impl Default for RcToken {
fn default() -> Self {
Self::new()
}
}
unsafe impl Token<RcTokenId> for RcToken {
fn eq_id(&self, id: &RcTokenId) -> bool {
self.0 == id.0
}
}
unsafe impl Unsync for RcToken {}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct RcTokenId(UniqueId);
#[derive(Debug, Clone)]
struct UniqueId(Rc<()>);
impl PartialEq for UniqueId {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for UniqueId {}
impl hash::Hash for UniqueId {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
(&*self.0 as *const ()).hash(state)
}
}
impl UniqueId {
pub fn new() -> Self {
UniqueId(Rc::new(()))
}
}
#[test]
fn unique_id_hash() {
let id1 = UniqueId::new();
let id2 = id1.clone();
let mut hm = std::collections::HashSet::new();
assert!(hm.insert(id1));
assert!(!hm.insert(id2));
}