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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::{
borrow::{Borrow, BorrowMut},
collections::HashSet,
hash::BuildHasherDefault,
ops::{Deref, DerefMut},
};
use hashbrown::HashMap;
use jrsonnet_gcmodule::{Trace, Tracer};
use rustc_hash::{FxHashSet, FxHasher};
#[derive(Debug, Clone)]
pub struct TraceBox<T: ?Sized>(pub Box<T>);
#[macro_export]
macro_rules! tb {
($v:expr) => {
$crate::gc::TraceBox(Box::new($v))
};
}
impl<T: ?Sized + Trace> Trace for TraceBox<T> {
fn trace(&self, tracer: &mut Tracer<'_>) {
self.0.trace(tracer);
}
fn is_type_tracked() -> bool {
true
}
}
impl<T: ?Sized> From<Box<T>> for TraceBox<T> {
fn from(inner: Box<T>) -> Self {
Self(inner)
}
}
impl<T: ?Sized> Deref for TraceBox<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: Trace + ?Sized> DerefMut for TraceBox<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T: ?Sized> Borrow<T> for TraceBox<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl<T: ?Sized> BorrowMut<T> for TraceBox<T> {
fn borrow_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T: ?Sized> AsRef<T> for TraceBox<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T: ?Sized> AsMut<T> for TraceBox<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
#[derive(Clone)]
pub struct GcHashSet<V>(pub FxHashSet<V>);
impl<V> GcHashSet<V> {
pub fn new() -> Self {
Self(HashSet::default())
}
pub fn with_capacity(capacity: usize) -> Self {
Self(FxHashSet::with_capacity_and_hasher(
capacity,
BuildHasherDefault::default(),
))
}
}
impl<V> Trace for GcHashSet<V>
where
V: Trace,
{
fn trace(&self, tracer: &mut Tracer<'_>) {
for v in &self.0 {
v.trace(tracer);
}
}
}
impl<V> Deref for GcHashSet<V> {
type Target = FxHashSet<V>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<V> DerefMut for GcHashSet<V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<V> Default for GcHashSet<V> {
fn default() -> Self {
Self::new()
}
}
pub struct GcHashMap<K, V>(pub HashMap<K, V, BuildHasherDefault<FxHasher>>);
impl<K, V> GcHashMap<K, V> {
pub fn new() -> Self {
Self(HashMap::default())
}
pub fn with_capacity(capacity: usize) -> Self {
Self(HashMap::with_capacity_and_hasher(
capacity,
BuildHasherDefault::default(),
))
}
}
impl<K, V> Trace for GcHashMap<K, V>
where
K: Trace,
V: Trace,
{
fn trace(&self, tracer: &mut Tracer<'_>) {
for (k, v) in &self.0 {
k.trace(tracer);
v.trace(tracer);
}
}
}
impl<K, V> Deref for GcHashMap<K, V> {
type Target = HashMap<K, V, BuildHasherDefault<FxHasher>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<K, V> DerefMut for GcHashMap<K, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<K, V> Default for GcHashMap<K, V> {
fn default() -> Self {
Self::new()
}
}