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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::cell::UnsafeCell;
use std::ops::{DerefMut,Deref};
use std::borrow::{BorrowMut, Borrow};
#[cfg(feature = "serialize")]
use serde::ser::SerializeStruct;
#[cfg(any(feature = "serialize", feature="ecs"))]
use serde_derive::Deserialize;
#[derive(Debug)]
pub struct LazyUpdate<T>{
value: UnsafeCell<T>,
has_changed: UnsafeCell<bool>
}
impl<T: Clone> Clone for LazyUpdate<T>{
fn clone(&self) -> LazyUpdate<T> {
unsafe{
LazyUpdate{
value: UnsafeCell::new((*self.value.get()).clone()),
has_changed: UnsafeCell::new(*self.has_changed.get()),
}
}
}
}
#[cfg(feature = "serialize")]
impl <T: serde::Serialize> serde::Serialize for LazyUpdate<T>{
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>{
unsafe{
let mut state = serializer.serialize_struct("Lazy", 2)?;
state.serialize_field("value", &*self.value.get())?;
state.serialize_field("has_changed", &*self.has_changed.get())?;
state.end()
}
}
}
#[cfg(feature = "serialize")]
impl<'a, T: serde::Deserialize<'a>> serde::Deserialize<'a> for LazyUpdate<T>{
fn deserialize<S: serde::Deserializer<'a>>(deserializer: S) -> Result<Self, <S as serde::Deserializer<'a>>::Error>{
#[derive(Deserialize)]
struct Value<T>{
value: T,
has_changed: bool
}
let value = Value::deserialize(deserializer);
value.map(|v|{
LazyUpdate{
value: UnsafeCell::new(v.value),
has_changed: UnsafeCell::new(v.has_changed)
}
})
}
}
impl<T> LazyUpdate<T>{
pub fn new(v: T) -> LazyUpdate<T>{
LazyUpdate{ value: UnsafeCell::new(v), has_changed: UnsafeCell::new(false) }
}
pub fn new_dirty(v: T) -> LazyUpdate<T>{
LazyUpdate{ value: UnsafeCell::new(v), has_changed: UnsafeCell::new(true) }
}
pub fn set_changed(&self){
unsafe{ (*self.has_changed.get()) = true };
}
pub fn update<F>(&self, mut f: F)
where F: FnMut(&mut T)
{
unsafe{
if *self.has_changed.get(){
f(&mut *self.value.get());
}
*self.has_changed.get() = false;
}
}
}
impl<T> Borrow<T> for LazyUpdate<T>{
fn borrow(&self) -> &T{
unsafe{
&*self.value.get()
}
}
}
impl<T> BorrowMut<T> for LazyUpdate<T>{
fn borrow_mut(&mut self) -> &mut T{
unsafe{
&mut *self.value.get()
}
}
}
impl<T> Deref for LazyUpdate<T>{
type Target = T;
fn deref(&self) -> &T{
unsafe{
&*self.value.get()
}
}
}
impl<T> DerefMut for LazyUpdate<T>{
fn deref_mut(&mut self) -> &mut T{
unsafe{
&mut *self.value.get()
}
}
}
impl<T> AsRef<T> for LazyUpdate<T>{
fn as_ref(&self) -> &T{
unsafe{
&*self.value.get()
}
}
}
impl<T> AsMut<T> for LazyUpdate<T>{
fn as_mut(&mut self) -> &mut T{
unsafe{
&mut *self.value.get()
}
}
}
impl<T> From<T> for LazyUpdate<T>{
fn from(t: T) -> LazyUpdate<T>{
LazyUpdate::new(t)
}
}
pub struct Lazy<T>{
func: UnsafeCell<Option<Box<dyn FnOnce() -> T>>>,
value_cache: UnsafeCell<Option<T>>,
}
impl<T> Lazy<T>{
pub fn defer<F>(f: F)-> Lazy<T>
where F: FnOnce() -> T + 'static
{
let b: Box<dyn FnOnce() -> T> = Box::new(f);
Lazy{
func: UnsafeCell::new(Some(b)),
value_cache: UnsafeCell::new(None)
}
}
pub fn defer_with_input<F,I>(input: I, f: F)-> Lazy<T>
where
F: FnOnce(I) -> T + 'static,
I: 'static,
{
let b: Box<dyn FnOnce() -> T> = Box::new(move || (f)(input));
Lazy{
func: UnsafeCell::new(Some(b)),
value_cache: UnsafeCell::new(None)
}
}
pub fn force(&self){
unsafe{
if (*self.value_cache.get()).is_none(){
let f = (*self.func.get()).take().unwrap();
*self.value_cache.get() = Some(f());
}
}
}
pub fn computed(t: T) -> Lazy<T> {
Lazy{
func: UnsafeCell::new(None),
value_cache: UnsafeCell::new(Some(t))
}
}
pub fn unwrap(self) -> T{
self.force();
self.value_cache.into_inner().unwrap()
}
}
impl<T> Borrow<T> for Lazy<T>{
fn borrow(&self) -> &T{
self.force();
unsafe{
(*self.value_cache.get()).as_ref().unwrap()
}
}
}
impl<T> BorrowMut<T> for Lazy<T>{
fn borrow_mut(&mut self) -> &mut T{
self.force();
unsafe{
(*self.value_cache.get()).as_mut().unwrap()
}
}
}
impl<T> Deref for Lazy<T>{
type Target = T;
fn deref(&self) -> &T{
self.force();
unsafe{
(*self.value_cache.get()).as_ref().unwrap()
}
}
}
impl<T> DerefMut for Lazy<T>{
fn deref_mut(&mut self) -> &mut T{
self.force();
unsafe{
(*self.value_cache.get()).as_mut().unwrap()
}
}
}
impl<T> From<T> for Lazy<T>{
fn from(t: T) -> Lazy<T>{
Lazy::computed(t)
}
}