From 5586d6eff1c7dafa11cdfe4f4ad96afb76c0fb67 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 8 Jan 2017 14:01:45 -0800 Subject: [PATCH] [2016][rust][23.1] more canonical way of getting index from Register --- 2016/rust/src/assembunny.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/2016/rust/src/assembunny.rs b/2016/rust/src/assembunny.rs index 9871625..6e31e6f 100644 --- a/2016/rust/src/assembunny.rs +++ b/2016/rust/src/assembunny.rs @@ -86,28 +86,20 @@ impl Registers { pub fn new() -> Self { Registers([0; 5]) } - - fn index(r: Register) -> usize { - match r { - Register::PC => 0, - Register::A => 1, - Register::B => 2, - Register::C => 3, - Register::D => 4, - } - } } impl ops::Index for Registers { type Output = isize; fn index(&self, _index: Register) -> &isize { - self.0.index(Self::index(_index)) + let index: u8 = _index.into(); + self.0.index(index as usize) } } impl ops::IndexMut for Registers { fn index_mut(&mut self, _index: Register) -> &mut isize { - self.0.index_mut(Self::index(_index)) + let index: u8 = _index.into(); + self.0.index_mut(index as usize) } } @@ -154,6 +146,18 @@ pub enum Register { D, } +impl From for u8 { + fn from(r: Register) -> u8 { + match r { + Register::PC => 0, + Register::A => 1, + Register::B => 2, + Register::C => 3, + Register::D => 4, + } + } +} + #[derive(Clone, Copy, Debug, PartialEq)] pub enum Instruction { Cpy(Variable, Variable),