-
-
Save pepyakin/2d68af12a4e5804ca11c3ec3b8a2dd6d to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains 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 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