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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::marker::{GcDeref, GcDrop, GcSafe};
use crate::{Finalize, Scan, Scanner};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::hash::BuildHasher;
use std::mem::forget;
use std::ptr::read;

// For pretty much all simple collections, the collection inherets the properites of what it contains
// (with respect to GcDeref, GcDrop and GcSafe)

// BTREEMAP
unsafe impl<K, V> GcDeref for BTreeMap<K, V>
where
    K: GcDeref,
    V: GcDeref,
{
}
unsafe impl<K, V> GcDrop for BTreeMap<K, V>
where
    K: GcDrop,
    V: GcDrop,
{
}
unsafe impl<K, V> GcSafe for BTreeMap<K, V>
where
    K: GcSafe,
    V: GcSafe,
{
}

unsafe impl<K: Scan, V: Scan> Scan for BTreeMap<K, V> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        for (k, v) in self {
            scanner.scan(k);
            scanner.scan(v);
        }
    }
}

unsafe impl<K: Finalize, V: Finalize> Finalize for BTreeMap<K, V> {
    unsafe fn finalize(&mut self) {
        let map = read(self);
        for mut e in map {
            e.finalize();
            forget(e);
        }
    }
}

// BTREESET
unsafe impl<T> GcDeref for BTreeSet<T> where T: GcDeref {}
unsafe impl<T> GcDrop for BTreeSet<T> where T: GcDrop {}
unsafe impl<T> GcSafe for BTreeSet<T> where T: GcSafe {}

unsafe impl<T: Scan> Scan for BTreeSet<T> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        for e in self {
            scanner.scan(e)
        }
    }
}

unsafe impl<T: Finalize> Finalize for BTreeSet<T> {
    unsafe fn finalize(&mut self) {
        let set = read(self);
        for mut e in set {
            e.finalize();
            forget(e);
        }
    }
}

// HASHMAP
unsafe impl<K, V, S: BuildHasher> GcDeref for HashMap<K, V, S>
where
    K: GcDeref,
    V: GcDeref,
    S: GcDeref,
{
}
unsafe impl<K, V, S: BuildHasher> GcDrop for HashMap<K, V, S>
where
    K: GcDrop,
    V: GcDrop,
    S: GcDrop,
{
}
unsafe impl<K, V, S: BuildHasher> GcSafe for HashMap<K, V, S>
where
    K: GcSafe,
    V: GcSafe,
    S: GcSafe,
{
}

unsafe impl<K: Scan, V: Scan, S: BuildHasher + GcSafe> Scan for HashMap<K, V, S> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        for (k, v) in self {
            scanner.scan(k);
            scanner.scan(v);
        }
    }
}

unsafe impl<K: Finalize, V: Finalize, S: BuildHasher> Finalize for HashMap<K, V, S> {
    unsafe fn finalize(&mut self) {
        let map = read(self);
        for mut e in map {
            e.finalize();
            forget(e);
        }
    }
}

// HASHSET
unsafe impl<T, S: BuildHasher> GcDeref for HashSet<T, S>
where
    T: GcDeref,
    S: GcDeref,
{
}
unsafe impl<T, S: BuildHasher> GcDrop for HashSet<T, S>
where
    T: GcDrop,
    S: GcDrop,
{
}
unsafe impl<T, S: BuildHasher> GcSafe for HashSet<T, S>
where
    T: GcSafe,
    S: GcSafe,
{
}

unsafe impl<T: Scan, S: BuildHasher + GcSafe> Scan for HashSet<T, S> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        for e in self {
            scanner.scan(e)
        }
    }
}

unsafe impl<T: Finalize, S: BuildHasher> Finalize for HashSet<T, S> {
    unsafe fn finalize(&mut self) {
        let set = read(self);
        for mut e in set {
            e.finalize();
            forget(e);
        }
    }
}

// TUPLES
unsafe impl<A, B> GcDeref for (A, B)
where
    A: GcDeref,
    B: GcDeref,
{
}
unsafe impl<A, B> GcDrop for (A, B)
where
    A: GcDrop,
    B: GcDrop,
{
}
unsafe impl<A, B> GcSafe for (A, B)
where
    A: GcSafe,
    B: GcSafe,
{
}

unsafe impl<A: Scan, B: Scan> Scan for (A, B) {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        self.0.scan(scanner);
        self.1.scan(scanner);
    }
}

unsafe impl<A: Finalize, B: Finalize> Finalize for (A, B) {
    unsafe fn finalize(&mut self) {
        let (mut a, mut b) = read(self);
        a.finalize();
        b.finalize();
        forget(a);
        forget(b);
    }
}

// VEC
unsafe impl<T> GcDeref for Vec<T> where T: GcDeref {}
unsafe impl<T> GcDrop for Vec<T> where T: GcDrop {}
unsafe impl<T> GcSafe for Vec<T> where T: GcSafe {}

unsafe impl<T: Scan> Scan for Vec<T> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        for e in self {
            scanner.scan(e)
        }
    }
}

unsafe impl<T: Finalize> Finalize for Vec<T> {
    unsafe fn finalize(&mut self) {
        let set = read(self);
        for mut e in set {
            e.finalize();
            forget(e);
        }
    }
}