Skip to content

Instantly share code, notes, and snippets.

@andy-morris
Forked from duckinator/code.rs
Last active October 11, 2016 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy-morris/e4011785b2003dcf0f3f80a6397fcb14 to your computer and use it in GitHub Desktop.
Save andy-morris/e4011785b2003dcf0f3f80a6397fcb14 to your computer and use it in GitHub Desktop.
#[derive(Clone, Copy)]
struct Op(fn(u16, u16));
// lets you call foo() instead of foo.0() (where foo: Op)
impl std::ops::Deref for Op {
type Target = fn(u16, u16);
fn deref(&self) -> &fn(u16, u16) { &self.0 }
}
fn op_mov(one: u16, two: u16) {
}
fn op_add(one: u16, two: u16) {
}
fn op_nand(one: u16, two: u16) {
}
fn op_shl(one: u16, two: u16) {
}
fn op_shr(one: u16, two: u16) {
}
fn op_jz(one: u16, two: u16) {
}
fn op_lt(one: u16, two: u16) {
}
fn op_gt(one: u16, two: u16) {
}
fn op_in(one: u16, two: u16) {
}
fn op_out(one: u16, two: u16) {
}
fn main() {
let methods: std::collections::HashMap<u16, Op> =
[(0b0000, Op(op_mov)),
(0b0001, Op(op_add)),
(0b0010, Op(op_nand)),
(0b0011, Op(op_shl)),
(0b0100, Op(op_shr)),
(0b0101, Op(op_jz)),
(0b0110, Op(op_lt)),
(0b0111, Op(op_gt)),
// No 0b1000-0b1101.
(0b1110, Op(op_in)),
(0b1111, Op(op_out))].iter().cloned().collect();
methods.get(&0b0000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment