Skip to content

Commit

Permalink
Replaced num_traits::FromPrimitive with a inline implementation using…
Browse files Browse the repository at this point in the history
… transmute
  • Loading branch information
patbuc committed Nov 20, 2023
1 parent 05c9553 commit 6ecff6d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ edition = "2021"

[dependencies]
colored = "2.0.4"
enum-primitive-derive = "0.2.2"
num-traits = "0.2.17"

[features]
disassemble = []
6 changes: 2 additions & 4 deletions src/vm/block/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/vm/block/disassembler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::vm::opcodes::OpCode;
use crate::vm::Block;
use num_traits::FromPrimitive;

#[cfg(feature = "disassemble")]
impl Block {
Expand All @@ -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),
Expand Down
11 changes: 9 additions & 2 deletions src/vm/opcodes.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) }
}
}
3 changes: 1 addition & 2 deletions src/vm/virtual_machine.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit 6ecff6d

Please sign in to comment.