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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use once_cell::sync::OnceCell;
use std::hash::Hash;
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use weak_table::WeakHashSet;
pub(crate) struct InternCache<T: ?Sized> {
cache: OnceCell<Mutex<WeakHashSet<Weak<T>>>>,
}
impl<T: ?Sized> InternCache<T> {
pub(crate) const fn new() -> Self {
InternCache {
cache: OnceCell::new(),
}
}
}
impl<T: Eq + Hash + ?Sized> InternCache<T> {
fn cache(&self) -> MutexGuard<'_, WeakHashSet<Weak<T>>> {
let cache = self.cache.get_or_init(|| Mutex::new(WeakHashSet::new()));
cache.lock().expect("Poisoned lock lock for cache")
}
}
impl<T: Eq + Hash> InternCache<T> {
pub(crate) fn intern(&self, value: T) -> Arc<T> {
let mut cache = self.cache();
if let Some(pp) = cache.get(&value) {
pp
} else {
let arc = Arc::new(value);
cache.insert(Arc::clone(&arc));
arc
}
}
}
impl<T: Hash + Eq + ?Sized> InternCache<T> {
pub(crate) fn intern_ref<'a, V>(&self, value: &'a V) -> Arc<T>
where
V: Hash + Eq + ?Sized,
&'a V: Into<Arc<T>>,
T: std::borrow::Borrow<V>,
{
let mut cache = self.cache();
if let Some(arc) = cache.get(value) {
arc
} else {
let arc = value.into();
cache.insert(Arc::clone(&arc));
arc
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn interning_by_value() {
let c: InternCache<String> = InternCache::new();
let s1 = c.intern("abc".to_string());
let s2 = c.intern("def".to_string());
let s3 = c.intern("abc".to_string());
assert!(Arc::ptr_eq(&s1, &s3));
assert!(!Arc::ptr_eq(&s1, &s2));
assert_eq!(s2.as_ref(), "def");
assert_eq!(s3.as_ref(), "abc");
}
#[test]
fn interning_by_ref() {
let c: InternCache<str> = InternCache::new();
let s1 = c.intern_ref("abc");
let s2 = c.intern_ref("def");
let s3 = c.intern_ref("abc");
assert!(Arc::ptr_eq(&s1, &s3));
assert!(!Arc::ptr_eq(&s1, &s2));
assert_eq!(&*s2, "def");
assert_eq!(&*s3, "abc");
}
}