Skip to content

Instantly share code, notes, and snippets.

Created May 31, 2016 13:22
Show Gist options
  • Save anonymous/a585b3422cbaa173dc31f388063fb5c4 to your computer and use it in GitHub Desktop.
Save anonymous/a585b3422cbaa173dc31f388063fb5c4 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(&mut |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