Bits and pieces. gist
Created
January 13, 2016 20:11
-
-
Save steveklabnik/84bb1c641a766eb39f60 to your computer and use it in GitHub Desktop.
Code
This file contains hidden or 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
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