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
use std::cell::{Cell, UnsafeCell};
use std::marker::PhantomData;
type Id<'id> = PhantomData<Cell<&'id mut ()>>;
pub struct LCellOwner<'id> {
_id: Id<'id>,
}
impl<'id> LCellOwner<'id> {
pub fn scope<F>(f: F)
where
F: for<'scope_id> FnOnce(LCellOwner<'scope_id>),
{
f(Self { _id: PhantomData })
}
pub fn cell<T>(&self, value: T) -> LCell<'id, T> {
LCell::<T>::new(value)
}
#[inline]
pub fn ro<'a, T>(&'a self, lc: &'a LCell<'id, T>) -> &'a T {
unsafe { &*lc.value.get() }
}
#[inline]
pub fn rw<'a, T>(&'a mut self, lc: &'a LCell<'id, T>) -> &'a mut T {
unsafe { &mut *lc.value.get() }
}
#[inline]
pub fn rw2<'a, T, U>(
&'a mut self,
lc1: &'a LCell<'id, T>,
lc2: &'a LCell<'id, U>,
) -> (&'a mut T, &'a mut U) {
assert!(
lc1 as *const _ as usize != lc2 as *const _ as usize,
"Illegal to borrow same LCell twice with rw2()"
);
unsafe { (&mut *lc1.value.get(), &mut *lc2.value.get()) }
}
#[inline]
pub fn rw3<'a, T, U, V>(
&'a mut self,
lc1: &'a LCell<'id, T>,
lc2: &'a LCell<'id, U>,
lc3: &'a LCell<'id, V>,
) -> (&'a mut T, &'a mut U, &'a mut V) {
assert!(
(lc1 as *const _ as usize != lc2 as *const _ as usize)
&& (lc2 as *const _ as usize != lc3 as *const _ as usize)
&& (lc3 as *const _ as usize != lc1 as *const _ as usize),
"Illegal to borrow same LCell twice with rw3()"
);
unsafe {
(
&mut *lc1.value.get(),
&mut *lc2.value.get(),
&mut *lc3.value.get(),
)
}
}
}
pub struct LCell<'id, T> {
_id: Id<'id>,
value: UnsafeCell<T>,
}
impl<'id, T> LCell<'id, T> {
#[inline]
pub fn new(value: T) -> LCell<'id, T> {
LCell {
_id: PhantomData,
value: UnsafeCell::new(value),
}
}
}
#[cfg(test)]
mod tests {
use super::{LCell, LCellOwner};
use std::rc::Rc;
#[test]
fn lcell() {
LCellOwner::scope(|mut owner| {
let c1 = LCell::new(100u32);
let c2 = owner.cell(200u32);
(*owner.rw(&c1)) += 1;
(*owner.rw(&c2)) += 2;
let c1ref = owner.ro(&c1);
let c2ref = owner.ro(&c2);
let total = *c1ref + *c2ref;
assert_eq!(total, 303);
});
}
#[test]
#[should_panic]
fn lcell_rw2() {
LCellOwner::scope(|mut owner| {
let c1 = Rc::new(LCell::new(100u32));
let (mutref1, mutref2) = owner.rw2(&c1, &c1);
*mutref1 += 1;
*mutref2 += 1;
});
}
#[test]
#[should_panic]
fn lcell_rw3_1() {
LCellOwner::scope(|mut owner| {
let c1 = Rc::new(LCell::new(100u32));
let c2 = Rc::new(LCell::new(200u32));
let (mutref1, mutref2, mutref3) = owner.rw3(&c1, &c1, &c2);
*mutref1 += 1;
*mutref2 += 1;
*mutref3 += 1;
});
}
#[test]
#[should_panic]
fn lcell_rw3_2() {
LCellOwner::scope(|mut owner| {
let c1 = Rc::new(LCell::new(100u32));
let c2 = Rc::new(LCell::new(200u32));
let (mutref1, mutref2, mutref3) = owner.rw3(&c1, &c2, &c1);
*mutref1 += 1;
*mutref2 += 1;
*mutref3 += 1;
});
}
#[test]
#[should_panic]
fn lcell_rw3_3() {
LCellOwner::scope(|mut owner| {
let c1 = Rc::new(LCell::new(100u32));
let c2 = Rc::new(LCell::new(200u32));
let (mutref1, mutref2, mutref3) = owner.rw3(&c2, &c1, &c1);
*mutref1 += 1;
*mutref2 += 1;
*mutref3 += 1;
});
}
}