diff --git a/Cargo.toml b/Cargo.toml index 22c2f67..8453b46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,6 @@ edition = "2021" [dependencies] colored = "2.0.4" -enum-primitive-derive = "0.2.2" -num-traits = "0.2.17" [features] disassemble = [] \ No newline at end of file diff --git a/src/vm/block/block.rs b/src/vm/block/block.rs index b55be71..a3846b4 100644 --- a/src/vm/block/block.rs +++ b/src/vm/block/block.rs @@ -106,7 +106,6 @@ impl Block { mod tests { use crate::vm::opcodes::OpCode; use crate::vm::Block; - use num_traits::FromPrimitive; #[test] fn new_block_is_empty() { @@ -136,14 +135,13 @@ mod tests { assert_eq!(2 * 256 + 6, block.instructions.len()); assert_eq!( OpCode::Constant2, - OpCode::from_u8(block.instructions[2 * 256]).unwrap() + OpCode::from_u8(block.instructions[2 * 256]) ); assert_eq!(256, block.read_u16(2 * 256 + 1)); - assert_eq!( OpCode::Constant2, - OpCode::from_u8(block.instructions[2 * 256 + 3]).unwrap() + OpCode::from_u8(block.instructions[2 * 256 + 3]) ); let constant_index = block.read_u16(2 * 256 + 4) as usize; assert_eq!(257, constant_index); diff --git a/src/vm/block/disassembler.rs b/src/vm/block/disassembler.rs index 4ef8c8a..2a66556 100644 --- a/src/vm/block/disassembler.rs +++ b/src/vm/block/disassembler.rs @@ -1,6 +1,5 @@ use crate::vm::opcodes::OpCode; use crate::vm::Block; -use num_traits::FromPrimitive; #[cfg(feature = "disassemble")] impl Block { @@ -27,7 +26,7 @@ impl Block { print!("{:6} ", line.unwrap()); } - let instruction = OpCode::from_u8(self.instructions[offset]).unwrap(); + let instruction = OpCode::from_u8(self.instructions[offset]); return match instruction { OpCode::Return => self.simple_instruction(OpCode::Return, offset), OpCode::Constant => self.constant_instruction(instruction, offset), diff --git a/src/vm/opcodes.rs b/src/vm/opcodes.rs index 7de5c61..8a262be 100644 --- a/src/vm/opcodes.rs +++ b/src/vm/opcodes.rs @@ -1,7 +1,7 @@ -use enum_primitive_derive::Primitive; +use std::mem::transmute; #[repr(u8)] -#[derive(Debug, PartialEq, Primitive)] +#[derive(Debug, PartialEq)] pub(in crate::vm) enum OpCode { Return = 0x00, Constant = 0x01, @@ -13,3 +13,10 @@ pub(in crate::vm) enum OpCode { Multiply = 0x07, Divide = 0x08, } + +impl OpCode { + #[inline(always)] + pub(in crate::vm) fn from_u8(value: u8) -> OpCode { + unsafe { transmute(value) } + } +} diff --git a/src/vm/virtual_machine.rs b/src/vm/virtual_machine.rs index 663d2b8..40cdaac 100644 --- a/src/vm/virtual_machine.rs +++ b/src/vm/virtual_machine.rs @@ -1,7 +1,6 @@ use crate::compiler::Compiler; use crate::vm::opcodes::OpCode; use crate::vm::{Block, Result, Value, VirtualMachine}; -use num_traits::FromPrimitive; impl VirtualMachine { pub fn new() -> Self { @@ -23,7 +22,7 @@ impl VirtualMachine { loop { #[cfg(feature = "disassemble")] self.block.disassemble_instruction(self.ip); - match OpCode::from_u8(self.block.read_u8(self.ip)).unwrap() { + match OpCode::from_u8(self.block.read_u8(self.ip)) { OpCode::Return => { let value = self.pop(); VirtualMachine::print(value);