shredder/std_impls/
collections.rs

1use crate::marker::{GcDeref, GcDrop, GcSafe};
2use crate::{Finalize, Scan, Scanner};
3use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
4use std::hash::BuildHasher;
5use std::mem::forget;
6use std::ptr::read;
7
8// For pretty much all simple collections, the collection inherets the properites of what it contains
9// (with respect to GcDeref, GcDrop and GcSafe)
10
11// BTREEMAP
12unsafe impl<K, V> GcDeref for BTreeMap<K, V>
13where
14    K: GcDeref,
15    V: GcDeref,
16{
17}
18unsafe impl<K, V> GcDrop for BTreeMap<K, V>
19where
20    K: GcDrop,
21    V: GcDrop,
22{
23}
24unsafe impl<K, V> GcSafe for BTreeMap<K, V>
25where
26    K: GcSafe,
27    V: GcSafe,
28{
29}
30
31unsafe impl<K: Scan, V: Scan> Scan for BTreeMap<K, V> {
32    #[inline]
33    fn scan(&self, scanner: &mut Scanner<'_>) {
34        for (k, v) in self {
35            scanner.scan(k);
36            scanner.scan(v);
37        }
38    }
39}
40
41unsafe impl<K: Finalize, V: Finalize> Finalize for BTreeMap<K, V> {
42    unsafe fn finalize(&mut self) {
43        let map = read(self);
44        for mut e in map {
45            e.finalize();
46            forget(e);
47        }
48    }
49}
50
51// BTREESET
52unsafe impl<T> GcDeref for BTreeSet<T> where T: GcDeref {}
53unsafe impl<T> GcDrop for BTreeSet<T> where T: GcDrop {}
54unsafe impl<T> GcSafe for BTreeSet<T> where T: GcSafe {}
55
56unsafe impl<T: Scan> Scan for BTreeSet<T> {
57    #[inline]
58    fn scan(&self, scanner: &mut Scanner<'_>) {
59        for e in self {
60            scanner.scan(e)
61        }
62    }
63}
64
65unsafe impl<T: Finalize> Finalize for BTreeSet<T> {
66    unsafe fn finalize(&mut self) {
67        let set = read(self);
68        for mut e in set {
69            e.finalize();
70            forget(e);
71        }
72    }
73}
74
75// HASHMAP
76unsafe impl<K, V, S: BuildHasher> GcDeref for HashMap<K, V, S>
77where
78    K: GcDeref,
79    V: GcDeref,
80    S: GcDeref,
81{
82}
83unsafe impl<K, V, S: BuildHasher> GcDrop for HashMap<K, V, S>
84where
85    K: GcDrop,
86    V: GcDrop,
87    S: GcDrop,
88{
89}
90unsafe impl<K, V, S: BuildHasher> GcSafe for HashMap<K, V, S>
91where
92    K: GcSafe,
93    V: GcSafe,
94    S: GcSafe,
95{
96}
97
98unsafe impl<K: Scan, V: Scan, S: BuildHasher + GcSafe> Scan for HashMap<K, V, S> {
99    #[inline]
100    fn scan(&self, scanner: &mut Scanner<'_>) {
101        for (k, v) in self {
102            scanner.scan(k);
103            scanner.scan(v);
104        }
105    }
106}
107
108unsafe impl<K: Finalize, V: Finalize, S: BuildHasher> Finalize for HashMap<K, V, S> {
109    unsafe fn finalize(&mut self) {
110        let map = read(self);
111        for mut e in map {
112            e.finalize();
113            forget(e);
114        }
115    }
116}
117
118// HASHSET
119unsafe impl<T, S: BuildHasher> GcDeref for HashSet<T, S>
120where
121    T: GcDeref,
122    S: GcDeref,
123{
124}
125unsafe impl<T, S: BuildHasher> GcDrop for HashSet<T, S>
126where
127    T: GcDrop,
128    S: GcDrop,
129{
130}
131unsafe impl<T, S: BuildHasher> GcSafe for HashSet<T, S>
132where
133    T: GcSafe,
134    S: GcSafe,
135{
136}
137
138unsafe impl<T: Scan, S: BuildHasher + GcSafe> Scan for HashSet<T, S> {
139    #[inline]
140    fn scan(&self, scanner: &mut Scanner<'_>) {
141        for e in self {
142            scanner.scan(e)
143        }
144    }
145}
146
147unsafe impl<T: Finalize, S: BuildHasher> Finalize for HashSet<T, S> {
148    unsafe fn finalize(&mut self) {
149        let set = read(self);
150        for mut e in set {
151            e.finalize();
152            forget(e);
153        }
154    }
155}
156
157// TUPLES
158unsafe impl<A, B> GcDeref for (A, B)
159where
160    A: GcDeref,
161    B: GcDeref,
162{
163}
164unsafe impl<A, B> GcDrop for (A, B)
165where
166    A: GcDrop,
167    B: GcDrop,
168{
169}
170unsafe impl<A, B> GcSafe for (A, B)
171where
172    A: GcSafe,
173    B: GcSafe,
174{
175}
176
177unsafe impl<A: Scan, B: Scan> Scan for (A, B) {
178    #[inline]
179    fn scan(&self, scanner: &mut Scanner<'_>) {
180        self.0.scan(scanner);
181        self.1.scan(scanner);
182    }
183}
184
185unsafe impl<A: Finalize, B: Finalize> Finalize for (A, B) {
186    unsafe fn finalize(&mut self) {
187        let (mut a, mut b) = read(self);
188        a.finalize();
189        b.finalize();
190        forget(a);
191        forget(b);
192    }
193}
194
195// VEC
196unsafe impl<T> GcDeref for Vec<T> where T: GcDeref {}
197unsafe impl<T> GcDrop for Vec<T> where T: GcDrop {}
198unsafe impl<T> GcSafe for Vec<T> where T: GcSafe {}
199
200unsafe impl<T: Scan> Scan for Vec<T> {
201    #[inline]
202    fn scan(&self, scanner: &mut Scanner<'_>) {
203        for e in self {
204            scanner.scan(e)
205        }
206    }
207}
208
209unsafe impl<T: Finalize> Finalize for Vec<T> {
210    unsafe fn finalize(&mut self) {
211        let set = read(self);
212        for mut e in set {
213            e.finalize();
214            forget(e);
215        }
216    }
217}