1
1
// TODO: For now, lib is symlinked to example to ease local development.
2
2
// But the final plan is to provide a stub file once we know what the interface will be.
3
3
4
+
5
+ use std:: cell:: Cell as MutCell ;
6
+
4
7
pub trait Cell < T > {
5
8
fn value ( & self ) -> & T ;
9
+ fn epoch ( & self ) -> usize ;
6
10
}
7
11
8
12
pub struct Reactor ;
9
13
10
14
pub struct InputCell < T > {
11
15
val : T ,
16
+ epoch : usize ,
12
17
}
13
18
14
- pub struct Compute1Cell < ' a , T : ' a , U , F : Fn ( & T ) -> U > {
19
+ pub struct Compute1Cell < ' a , T : ' a , U : Copy , F : Fn ( & T ) -> U > {
15
20
compute : F ,
16
21
cell : & ' a Cell < T > ,
17
- val : U ,
22
+ epoch : MutCell < usize > ,
23
+ val : MutCell < U > ,
18
24
}
19
25
20
26
impl Reactor {
@@ -25,14 +31,15 @@ impl Reactor {
25
31
pub fn create_input < T > ( & self , initial : T ) -> InputCell < T > {
26
32
InputCell {
27
33
val : initial,
34
+ epoch : 0 ,
28
35
}
29
36
}
30
37
31
- pub fn create_compute1 < ' a , T , U , F > ( & self , cell : & ' a Cell < T > , compute : F ) -> Compute1Cell < ' a , T , U , F >
32
- where F : Fn ( & T ) -> U {
38
+ pub fn create_compute1 < ' a , T , U : Copy , F : Fn ( & T ) -> U > ( & self , cell : & ' a Cell < T > , compute : F ) -> Compute1Cell < ' a , T , U , F > {
33
39
Compute1Cell {
34
- val : compute ( cell. value ( ) ) ,
40
+ val : MutCell :: new ( compute ( cell. value ( ) ) ) ,
35
41
cell : cell,
42
+ epoch : MutCell :: new ( cell. epoch ( ) ) ,
36
43
compute : compute,
37
44
}
38
45
}
@@ -42,6 +49,10 @@ impl <T> Cell<T> for InputCell<T> {
42
49
fn value ( & self ) -> & T {
43
50
& self . val
44
51
}
52
+
53
+ fn epoch ( & self ) -> usize {
54
+ self . epoch
55
+ }
45
56
}
46
57
47
58
impl < T > InputCell < T > {
@@ -50,8 +61,16 @@ impl <T> InputCell<T> {
50
61
}
51
62
}
52
63
53
- impl < ' a , T , U , F : Fn ( & T ) -> U > Cell < U > for Compute1Cell < ' a , T , U , F > {
64
+ impl < ' a , T , U : Copy , F : Fn ( & T ) -> U > Cell < U > for Compute1Cell < ' a , T , U , F > {
54
65
fn value ( & self ) -> & U {
55
- & self . val
66
+ if self . epoch ( ) < self . cell . epoch ( ) {
67
+ self . epoch . set ( self . cell . epoch ( ) ) ;
68
+ self . val . set ( ( self . compute ) ( self . cell . value ( ) ) ) ;
69
+ }
70
+ & self . val . get ( ) . clone ( )
71
+ }
72
+
73
+ fn epoch ( & self ) -> usize {
74
+ self . epoch . get ( )
56
75
}
57
76
}
0 commit comments