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
// Copyright (c) 2020-2022  David Sorokin <david.sorokin@gmail.com>, based in Yoshkar-Ola, Russia
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::cell::UnsafeCell;
use std::ptr;

use crate::simulation;
use crate::simulation::point::Point;
use crate::simulation::event::Event;

/// A mutable cell computation synchronized with the event queue.
pub struct RefComp<T> {

    /// The underlying cell.
    cell: UnsafeCell<T>
}

impl<T> RefComp<T> {

    /// Create a new mutable cell by the initial value.
    #[inline]
    pub fn new(val: T) -> RefComp<T> {
        RefComp { cell: UnsafeCell::new(val) }
    }

    /// Read the contents of the mutable cell at the specified time point.
    #[inline]
    pub fn read_at(&self, _p: &Point) -> T
        where T: Clone
    {
        let p = self.cell.get();
        unsafe {
            (*p).clone()
        }
    }

    /// Read the contents of the mutable cell within the `Event` computation.
    #[inline]
    pub fn read(comp: Rc<Self>) -> Read<T>
        where T: Clone
    {
        Read { comp: comp }
    }

    /// Swap the contents of the mutable cell at the specified time point.
    #[inline]
    pub fn swap_at(&self, val: T, _p: &Point) -> T {
        let mut t = val;
        unsafe {
            ptr::swap(&mut t, self.cell.get());
        }
        t
    }

    /// Swap the contents of the mutable cell within the `Event` computation.
    #[inline]
    pub fn swap(comp: Rc<Self>, val: T) -> Swap<T> {
        Swap { comp: comp, val: val }
    }

    /// Write into the contents of the mutable cell at the specified time point.
    #[inline]
    pub fn write_at(&self, val: T, _p: &Point) {
        let p = self.cell.get();
        unsafe {
            *p = val;
        }
    }

    /// Write into the contents of the mutable cell within the `Event` computation.
    #[inline]
    pub fn write(comp: Rc<Self>, val: T) -> Write<T> {
        Write { comp: comp, val: val }
    }

    /// Mutate the contents of the mutable cell at the specified time point.
    #[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);
        }
    }

    /// Mutate the contents of the mutable cell within the `Event` computation.
    #[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) }
        }
    }
}

/// It reads the contents of the mutable cell within the `Event` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Read<T> {

    /// The corresponding reference.
    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))
    }
}

/// It swaps the contents of the mutable cell within the `Event` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Swap<T> {

    /// The corresponding reference.
    comp: Rc<RefComp<T>>,

    /// A new value.
    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))
    }
}

/// It writes into the contents of the mutable cell within the `Event` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Write<T> {

    /// The corresponding reference.
    comp: Rc<RefComp<T>>,

    /// A new value.
    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))
    }
}

/// It mutates the contents of the mutable cell within the `Event` computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Modify<T, F> {

    /// The corresponding reference.
    comp: Rc<RefComp<T>>,

    /// A mutator.
    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))
    }
}