Skip to content

Instantly share code, notes, and snippets.

@mstewartgallus
Created April 21, 2013 04:13
Show Gist options
  • Save mstewartgallus/5428464 to your computer and use it in GitHub Desktop.
Save mstewartgallus/5428464 to your computer and use it in GitHub Desktop.
Buggy OneShot Function Implementation
// Rust lacks closures that can only be used once so they are emulated here.
priv trait OneShot <T> {
fn run (~self) -> T;
}
priv struct AnonymousOneShot <S, T> (S, ~fn (S) -> T);
impl <S, T> OneShot <T> for AnonymousOneShot <S, T> {
fn run (~self) -> T {
let ~AnonymousOneShot (x, f) = self;
f (x)
}
}
priv fn one_shot <S, T> (x : S, f : ~fn (S) -> T) -> ~OneShot <T> {
~AnonymousOneShot::<S, T> (x, f) as ~OneShot <T>
}
fn main () {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment