Skip to content

Instantly share code, notes, and snippets.

@pepyakin
Forked from anonymous/playground.rs
Last active May 31, 2016 13:23
Show Gist options
  • Save pepyakin/2d68af12a4e5804ca11c3ec3b8a2dd6d to your computer and use it in GitHub Desktop.
Save pepyakin/2d68af12a4e5804ca11c3ec3b8a2dd6d to your computer and use it in GitHub Desktop.
Shared via Rust Playground
struct Vm {
pc: usize,
}
impl Vm {
fn new() -> Vm {
Vm { pc: 0 }
}
fn execute<F>(&mut self, mut probe: F)
where F: FnMut(&mut Vm)
{
loop {
probe(self);
}
}
}
struct Beeper {
beeping: bool
}
impl Beeper {
fn new() -> Beeper {
Beeper { beeping: false }
}
fn toggle(&mut self) {
self.beeping != self.beeping;
}
}
fn main() {
let mut vm = Vm::new();
let mut beeper = Beeper::new();
vm.execute(|x: &mut Vm| {
println!("{}", x.pc);
beeper.toggle();
});
beeper.toggle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment