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
use core::cell::Cell;
use crate::{GcSafe, Trace, GcVisitor, NullTrace, TraceImmutable, GcDirectBarrier,};
#[derive(Default, Clone, Debug)]
#[repr(transparent)]
pub struct GcCell<T: Trace + Copy>(Cell<T>);
impl<T: Trace + Copy> GcCell<T> {
#[inline]
pub fn new(value: T) -> Self {
GcCell(Cell::new(value))
}
#[inline]
pub fn get_mut(&mut self) -> &mut T {
self.0.get_mut()
}
#[inline]
pub fn as_ptr(&self) -> *mut T {
self.0.as_ptr()
}
#[inline]
pub fn get(&self) -> T {
self.0.get()
}
}
impl<T: NullTrace + Copy> GcCell<T> {
#[inline]
pub fn set(&self, value: T) {
self.0.set(value)
}
}
unsafe impl<'gc, OwningRef, Value> GcDirectBarrier<'gc, OwningRef> for GcCell<Value>
where Value: GcDirectBarrier<'gc, OwningRef> + Copy {
#[inline]
unsafe fn write_barrier(
&self, owner: &OwningRef,
field_offset: usize
) {
self.get().write_barrier(owner, field_offset)
}
}
unsafe impl<T: Trace + Copy> Trace for GcCell<T> {
const NEEDS_TRACE: bool = T::NEEDS_TRACE;
#[inline]
fn visit<V: GcVisitor>(&mut self, visitor: &mut V) -> Result<(), V::Err> {
visitor.visit(self.get_mut())
}
}
unsafe impl<T: GcSafe + NullTrace + Copy> TraceImmutable for GcCell<T> {
#[inline]
fn visit_immutable<V: GcVisitor>(&self, visitor: &mut V) -> Result<(), <V as GcVisitor>::Err> {
let mut value = self.get();
visitor.visit(&mut value)?;
self.set(value);
Ok(())
}
}
unsafe impl<T: GcSafe + Copy + NullTrace> NullTrace for GcCell<T> {}
unsafe impl<T: GcSafe + Copy> GcSafe for GcCell<T> {
const NEEDS_DROP: bool = false;
}