Skip to content

Instantly share code, notes, and snippets.

@jmcarthur
Created September 28, 2013 18:56
Show Gist options
  • Save jmcarthur/6745227 to your computer and use it in GitHub Desktop.
Save jmcarthur/6745227 to your computer and use it in GitHub Desktop.
Trying to get familiar with Rust's pointers and closures by implementing a thunk data type
enum ThunkData<T> {
Val (T),
Fun (~fn() -> T)
}
struct Thunk<T> {
thunkData: ThunkData<T>
}
impl<T : Clone> Thunk<T> {
fn force (&mut self) -> &T {
match self.thunkData {
Val (x) => return x,
Fun (f) => {
let x = f ();
self.thunkData = Val (x);
return x;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment