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
use std::cell::UnsafeCell;
use std::ops::{DerefMut,Deref};
use std::borrow::{BorrowMut, Borrow};

#[cfg(nightly)]
use std::mem;

#[cfg(not(nightly))]
#[derive(Debug)]
pub struct Lazy<T>{
	value: UnsafeCell<T>,
	has_changed: UnsafeCell<bool>
}

impl<T: Clone> Clone for Lazy<T>{
	fn clone(&self) -> Lazy<T> {
		unsafe{
			Lazy{
				value: UnsafeCell::new((*self.value.get()).clone()),
				has_changed: UnsafeCell::new(*self.has_changed.get()),
			}
		}
	}
}

#[cfg(not(nightly))]
impl<T> Lazy<T>{
	pub fn new(v: T) -> Lazy<T>{
		Lazy{ value: UnsafeCell::new(v), has_changed: UnsafeCell::new(false) }
	}

	pub fn new_dirty(v: T) -> Lazy<T>{
		Lazy{ 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;
		}
	}

}

#[cfg(not(nightly))]
impl<T> Borrow<T> for Lazy<T>{
	fn borrow(&self) -> &T{
		unsafe{
			&*self.value.get()
		}
	}
}

#[cfg(not(nightly))]
impl<T> BorrowMut<T> for Lazy<T>{
	fn borrow_mut(&mut self) -> &mut T{
		unsafe{
			&mut *self.value.get()
		}
	}
}

#[cfg(not(nightly))]
impl<T> Deref for Lazy<T>{
	type Target = T;
	fn deref(&self) -> &T{
		unsafe{
			&*self.value.get()
		}
	}
}

#[cfg(not(nightly))]
impl<T> DerefMut for Lazy<T>{
	fn deref_mut(&mut self) -> &mut T{
		unsafe{
			&mut *self.value.get()
		}
	}
}

#[cfg(not(nightly))]
impl<T> AsRef<T> for Lazy<T>{
	fn as_ref(&self) -> &T{
		unsafe{
			&*self.value.get()
		}
	}
}

#[cfg(not(nightly))]
impl<T> AsMut<T> for Lazy<T>{
	fn as_mut(&mut self) -> &mut T{
		unsafe{
			&mut *self.value.get()
		}
	}
}

#[cfg(not(nightly))]
impl<T> From<T> for Lazy<T>{
	fn from(t: T) -> Lazy<T>{
		Lazy::new(t)
	}
}


#[cfg(nightly)]
pub struct Lazy<T>{
	func: UnsafeCell<Option<Box<FnOnce() -> ()>>>,
	value_cache: UnsafeCell<Option<T>>,
}

#[cfg(nightly)]
impl<T> Lazy<T>{
	pub fn defer<'a, F>(f: F)-> Lazy<T>
		where F: FnOnce() -> T + 'a{
		unsafe{
			let b: Box<FnOnce() -> T + 'a> = Box::new(f);
			let raw: *mut FnOnce() -> T = Box::into_raw(b);
			Lazy{ func: UnsafeCell::new(Some(Box::from_raw(raw as *mut (FnOnce() -> () + 'static)))), value_cache: UnsafeCell::new(None) }
		}
	}

	pub fn defer_with_input<'a, F>(input: T, f: F)-> Lazy<T>
		where F: FnOnce(T) -> T + 'a,
			  T: 'a{
		unsafe{
			let b: Box<FnOnce() -> T + 'a> = Box::new(move || (f)(input));
			let raw: *mut FnOnce() -> T = Box::into_raw(b);
			Lazy{ func: UnsafeCell::new(Some(Box::from_raw(raw as *mut (FnOnce() -> () + 'static)))), value_cache: UnsafeCell::new(None) }
		}
	}

	pub fn force(&self){
		unsafe{
			if (*self.value_cache.get()).is_none(){
				let mut f2 = None;
				mem::swap(&mut *self.func.get(), &mut f2);
				let f = Box::from_raw(Box::into_raw(f2.unwrap()) as *mut FnOnce() -> T);
				//*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();
		unsafe{
			let mut t = &mut *self.value_cache.get();
			let mut t2 = Some(mem::uninitialized::<T>());
			mem::swap(t, &mut t2);
			t2.unwrap()
		}
	}
}

#[cfg(nightly)]
impl<T> Borrow<T> for Lazy<T>{
	fn borrow(&self) -> &T{
		self.force();
		unsafe{
			(*self.value_cache.get()).as_ref().unwrap()
		}
	}
}

#[cfg(nightly)]
impl<T> BorrowMut<T> for Lazy<T>{
	fn borrow_mut(&mut self) -> &mut T{
		self.force();
		unsafe{
			(*self.value_cache.get()).as_mut().unwrap()
		}
	}
}

#[cfg(nightly)]
impl<T> Deref for Lazy<T>{
	type Target = T;
	fn deref(&self) -> &T{
		self.force();
		unsafe{
			(*self.value_cache.get()).as_ref().unwrap()
		}
	}
}

#[cfg(nightly)]
impl<T> DerefMut for Lazy<T>{
	fn deref_mut(&mut self) -> &mut T{
		self.force();
		unsafe{
			(*self.value_cache.get()).as_mut().unwrap()
		}
	}
}

#[cfg(nightly)]
impl<T> From<T> for Lazy<T>{
	fn from(t: T) -> Lazy<T>{
		Lazy::computed(t)
	}
}