Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created January 13, 2016 20:11
Show Gist options
  • Save steveklabnik/84bb1c641a766eb39f60 to your computer and use it in GitHub Desktop.
Save steveklabnik/84bb1c641a766eb39f60 to your computer and use it in GitHub Desktop.
Code

code

Bits and pieces. gist

struct CallbackHolder<'a> {
callbacks: Vec<&'a Fn()>,
}
impl<'a> CallbackHolder<'a> {
fn new() -> CallbackHolder<'a> {
return CallbackHolder{callbacks: vec![]};
}
fn register(&mut self, f: &'a Fn()) {
self.callbacks.push(f);
}
fn pump(&mut self) {
while let Some(f) = self.callbacks.pop() {
f();
}
}
}
fn main() {
let mut holder = CallbackHolder::new();
let c = &|| {
println!("hello from a callback!");
};
holder.register(c);
holder.pump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment