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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use Invariant;
pub use ;
pub use Gc;
/// Rebind a [`Gc`] object to a [`Root`].
///
/// Gc pointers lifetime can be freely rebound to the lifetime of the arena at any time.
/// This macro has two primary uses:
///
///
/// The first is to rebind a `Gc` pointer returned a function which takes a mutable reference
/// to the arena. A pointer returned by such a function is bound to a mutable arena which prevents
/// one from allocating new bc pointer for as long as the returned value lives. By rebinding
/// the returned `Gc` pointer the pointer will be bound to a immutable borrow which does allow
/// allocating new values.
///
/// The second use is to rebind a `Gc` pointer from a rooted pointer. Once a pointer is rooted the
/// pointer will only remain alive for the duration of the scope in which the pointer is rooted if
/// the pointer needs to escape that lifetime rebinding the rooted pointer again to the arena
/// allows it to life for the lifetime of the arena.
///
///
/// # Example
/// ```
/// use dreck::{Gc,Root,Owner,rebind, root};
///
/// fn use_and_collect<'r,'own>(
/// owner: &Owner<'own>,
/// root: &'r mut Root<'own>,
/// ptr: Gc<'r,'own,u32>) -> Gc<'r,'own,u32>{
///
/// if *ptr.borrow(owner) == 0{
/// root!(&root,ptr);
/// // `ptr` is now rooted and its lifetime is as long as the current scope.
/// root.collect(owner);
/// // Rebind ptr to root increasing its lifetime to be the same as root.
/// return rebind!(root,ptr);
/// }
/// ptr
/// }
///
/// ```
/// Root a [`Gc`] struct detaching it from their [`Root`]
///
/// It is not possible to run garbage collection as long as there are dangeling Gc pointers which
/// borrow the [`Root`].
///
/// This macro roots a pointer thus detaching it from the [`Root`] and allowing one to run garbage
/// collection.
///
/// # Example
/// ```
/// use dreck::{new_root,rebind, root};
///
/// new_root!(owner,root);
///
/// let ptr = root.add(1);
///
/// // Not allowed as ptr is dangling.
/// // root.collect()
///
/// root!(&root,ptr);
/// // ptr is rooted and detached from the root borrow allowing garbage collection.
/// root.collect(owner);
/// assert_eq!(*ptr.borrow(owner),1);
///
/// ```
/// Create a new [`Owner`] and [`Root`]
///
/// This macro is the only way to safely create a [`Root`] object.
/// A trait for marking gc live pointers.
///
/// # Safety
///
/// This trait can result to lots of unpredictable undefined behaviour if not implemented correctly
/// as such, one should be very carefull when implementing this trait.
///
/// The implementation must uphold the following guarentees:
/// - `needs_trace` returns true if the type can contain `Gc` pointers.
/// - `trace` marks all pointers contained in the type and calls `trace` all types contained in this type which implement `Trace`.
///
/// # Example
///
/// ```
/// use dreck::{Gc,Trace, Tracer};
/// pub struct Container<'gc, 'own> {
/// text: Gc<'gc, 'own, String>,
/// vec: Vec<Gc<'gc,'own,i32>>
/// }
///
/// unsafe impl<'gc, 'own> Trace<'own> for Container<'gc, 'own> {
/// fn needs_trace() -> bool
/// where
/// Self: Sized,
/// {
/// true
/// }
///
/// fn trace<'a>(&self, trace: Tracer<'a,'own>) {
/// // you can also call `self.text.trace(trace)`.
/// trace.mark(self.text);
/// self.vec.trace(trace);
/// }
/// }
/// ```
pub unsafe