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
#[cfg(doc)]
use crate::Gc;
use crate::locked::Unlock;
/// A marker type which indicates that any owning [`Gc`] has been marked as having been modified.
#[repr(transparent)]
pub struct Write<T: ?Sized>(T);
impl<T: ?Sized> Write<T> {
/// # Safety
/// The parent [`Gc`] must have been marked as mutated.
pub unsafe fn new_unchecked(value: &T) -> &Write<T> {
// Safety: Write is a thin wrapper around `T`.
unsafe { std::mem::transmute(value) }
}
pub fn new_static(value: &T) -> &Write<T>
where
T: 'static,
{
unsafe { Write::new_unchecked(value) }
}
pub fn into_inner(&self) -> &T {
&self.0
}
pub fn unlock(&self) -> &T::Unlocked
where
T: Unlock,
{
unsafe { self.0.unlock_unchecked() }
}
/// Projects a write permission into a write permision of the of the values contained by
/// `self`.
///
/// # Panics
/// When the closure returns a reference to a value not contained within the bounds of `self`.
pub fn project<U: ?Sized>(&self, f: impl for<'a> FnOnce(&'a T) -> &'a U) -> &Write<U> {
self.try_project(f).unwrap()
}
/// Projects a write permission into a write permision of the of the values contained by
/// `self`, returning an error if the closure returns a reference to a value not contained
/// within the bounds of `self`.
pub fn try_project<U: ?Sized>(
&self,
f: impl for<'a> FnOnce(&'a T) -> &'a U,
) -> Result<&Write<U>, WriteProjectError> {
let size = size_of_val(self) as isize;
let self_addr = (self as *const Write<T>).addr() as isize;
let proj = f(&self.0);
let proj_addr = (proj as *const U).addr() as isize;
if (0..size).contains(&(proj_addr - self_addr)) {
unsafe { Ok(Write::new_unchecked(proj)) }
} else {
Err(WriteProjectError)
}
}
/// Projects a write permission into a write permission of one of the containing objects fields.
///
/// # Safety
/// The given closure must return a reference to a value which is owned by self. The closure
/// *must not* dereference a [`Gc`], or in any way project into a value which is owned
/// by another garbage collected pointer, and which could itself contain a garbage collected
/// pointer.
///
/// # Examples
/// ```
/// # use ghost_gc::{locked::LockedCell, Gc, once_arena, Collect, Collector};
/// # once_arena(|mt| {
/// #
/// # unsafe impl<T: Collect> Collect for LinkedList<'_, T> {
/// # const NEEDS_TRACE: bool = T::NEEDS_TRACE;
/// #
/// # fn trace(&self, c: &Collector) {
/// # self.data.trace(c);
/// # match self.next.get() {
/// # Some(v) => v.trace(c),
/// # None => {}
/// # }
/// # }
/// # }
/// #
/// #[derive(Debug)]
/// struct LinkedList<'b, T> {
/// data: T,
/// next: LockedCell<Option<Gc<'b, Self>>>,
/// }
///
/// let head = Gc::new(LinkedList::<'_, u32> {
/// data: 0,
/// next: LockedCell::new(Some(Gc::new(LinkedList {
/// data: 1,
/// next: LockedCell::new(None)
/// }, mt)))
/// }, mt);
///
/// unsafe {
/// head.write().project_unchecked(|x| &x.data);
/// head.write().project_unchecked(|x| &x.next);
/// }
/// # });
/// ```
pub unsafe fn project_unchecked<U: ?Sized>(
&self,
f: impl for<'a> FnOnce(&'a T) -> &'a U,
) -> &Write<U> {
let self_ref: &T = &self.0;
let proj = f(self_ref);
unsafe { Write::new_unchecked(proj) }
}
}
#[derive(Debug)]
pub struct WriteProjectError;
#[cfg(test)]
mod tests {
use crate::Write;
#[test]
fn basic_projection() {
struct Test {
a: u32,
b: &'static str,
}
let t = Test {
a: 17,
b: "Hello, World!",
};
let w = Write::new_static(&t);
let _: &Write<u32> = w.project(|f| &f.a);
let _: &Write<&str> = w.project(|f| &f.b);
}
#[test]
#[should_panic]
fn incorrect_projection() {
struct Test {
_a: &'static str,
}
let t = Test {
_a: "Hello, World!",
};
let w = Write::new_static(&t);
let _ = w.project(|_| "Some other string.");
}
}