Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(fnbox)]
- use std::boxed::FnBox;
- struct TaskRunner<'a> {
- cb: Option<Box<FnBox() + 'a>>,
- }
- impl<'a> TaskRunner<'a> {
- fn new() -> Self { TaskRunner { cb: None } }
- fn start_task(&mut self, cb: Box<FnBox() + 'a>) {
- self.cb = Some(cb);
- }
- fn finish_task(&mut self) {
- if let Some(cb) = self.cb.take() {
- cb.call_box(());
- }
- }
- }
- struct Manager<'a> {
- task_runner: TaskRunner<'a>,
- }
- impl<'a> Manager<'a> {
- fn new() -> Self {
- Manager {
- task_runner: TaskRunner::new(),
- }
- }
- fn do_next_thing(&self) {
- println!("Task finished! Going to do the next thing!");
- }
- fn run_task(&mut self) {
- self.task_runner.start_task(Box::new(|| {
- println!("Finished!");
- // The following does not compile - how are we even going to use the callback?
- self.do_next_thing();
- } ));
- self.task_runner.finish_task();
- }
- }
- fn main() {
- let mut manager = Manager::new();
- manager.run_task();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement