Created
September 28, 2013 18:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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