Hi, I'm still pretty new to Rust and I've been fighting the borrow-checker for about two solid days now on this.
I'm writing an 8086 Emulator and want to keep all the info about each instruction and its constants close together.
Thus, I had the idea of putting each instruction execution into a closure that I can call upon, whenever that specific instruction comes up.
I come from Java, where this is just a lambda for a functional interface.
I did get it to work by just passing a function pointer to a function declared elsewhere, but I want to avoid that at all costs, since I don't want to split the information about each instruction and its data.
(Both the current fields of the Instruction and the arguments to the closure are not final, yet)
pub(crate) struct Instruction {
pub code: u8,
pub name: &'static str,
pub args: [&'static str; 2],
pub execute: &'static Executor,
}
type Executor = dyn FnMut(&mut Computer) -> &mut Computer;
pub(crate) static INSTRUCTIONS: &'static [Instruction] = &[
Instruction { code: 0x00, name: "ADD", args: ["Eb", "Gb"], execute: &|computer| { computer } },
Instruction { code: 0x01, name: "ADD", args: ["Ev", "Gv"], execute: &|computer| { computer } },
So my question is: Is there any way to make this work without splitting info about the Instruction too far from the code for it?
Because if not, then I don think either me or the language are not ready for one another and I'm just gonna move back to Java again.