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
use std::rc::Rc;
use std::cell::UnsafeCell;
use std::ptr;
use crate::simulation;
use crate::simulation::point::Point;
use crate::simulation::event::Event;
pub struct RefComp<T> {
cell: UnsafeCell<T>
}
impl<T> RefComp<T> {
#[inline]
pub fn new(val: T) -> RefComp<T> {
RefComp { cell: UnsafeCell::new(val) }
}
#[inline]
pub fn read_at(&self, _p: &Point) -> T
where T: Clone
{
let p = self.cell.get();
unsafe {
(*p).clone()
}
}
#[inline]
pub fn read(comp: Rc<Self>) -> Read<T>
where T: Clone
{
Read { comp: comp }
}
#[inline]
pub fn swap_at(&self, val: T, _p: &Point) -> T {
let mut t = val;
unsafe {
ptr::swap(&mut t, self.cell.get());
}
t
}
#[inline]
pub fn swap(comp: Rc<Self>, val: T) -> Swap<T> {
Swap { comp: comp, val: val }
}
#[inline]
pub fn write_at(&self, val: T, _p: &Point) {
let p = self.cell.get();
unsafe {
*p = val;
}
}
#[inline]
pub fn write(comp: Rc<Self>, val: T) -> Write<T> {
Write { comp: comp, val: val }
}
#[inline]
pub fn modify_at<F>(&self, f: F, _p: &Point)
where F: FnOnce(&T) -> T,
T: Clone
{
let p = self.cell.get();
unsafe {
let t = (*p).clone();
*p = f(&t);
}
}
#[inline]
pub fn modify<F>(comp: Rc<Self>, f: F) -> Modify<T, F>
where F: FnOnce(&T) -> T,
T: Clone
{
Modify { comp: comp, f: f }
}
}
impl<T> PartialEq for RefComp<T> {
fn eq(&self, other: &Self) -> bool {
self.cell.get() == other.cell.get()
}
}
impl<T> Eq for RefComp<T> {}
impl<T: Clone> Clone for RefComp<T> {
fn clone(&self) -> Self {
let p = self.cell.get();
unsafe {
let val = (*p).clone();
RefComp { cell: UnsafeCell::new(val) }
}
}
}
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Read<T> {
comp: Rc<RefComp<T>>
}
impl<T: Clone> Event for Read<T> {
type Item = T;
#[inline]
fn call_event(self, p: &Point) -> simulation::Result<Self::Item> {
let Read { comp } = self;
Result::Ok(comp.read_at(p))
}
}
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Swap<T> {
comp: Rc<RefComp<T>>,
val: T
}
impl<T> Event for Swap<T> {
type Item = T;
#[inline]
fn call_event(self, p: &Point) -> simulation::Result<Self::Item> {
let Swap { comp, val } = self;
Result::Ok(comp.swap_at(val, p))
}
}
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Write<T> {
comp: Rc<RefComp<T>>,
val: T
}
impl<T> Event for Write<T> {
type Item = ();
#[inline]
fn call_event(self, p: &Point) -> simulation::Result<Self::Item> {
let Write { comp, val } = self;
Result::Ok(comp.write_at(val, p))
}
}
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Modify<T, F> {
comp: Rc<RefComp<T>>,
f: F
}
impl<T, F> Event for Modify<T, F>
where F: FnOnce(&T) -> T,
T: Clone
{
type Item = ();
#[inline]
fn call_event(self, p: &Point) -> simulation::Result<Self::Item> {
let Modify { comp, f } = self;
Result::Ok(comp.modify_at(f, p))
}
}