Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl From<u8> for InstructionType` #18

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions crates/brainfuck_vm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl Display for InstructionType {
}
}

impl InstructionType {
pub fn from_u8(ins: u8) -> Self {
Self::from_str(&(ins as char).to_string()).expect("Invalid instruction")
impl From<u8> for InstructionType {
fn from(value: u8) -> Self {
Self::from_str(&(value as char).to_string()).expect("Invalid instruction")
}
}

Expand Down Expand Up @@ -114,21 +114,21 @@ mod tests {
// Test from_u8 implementation
#[test]
fn test_instruction_type_from_u8() {
assert_eq!(InstructionType::from_u8(b'>'), InstructionType::Right);
assert_eq!(InstructionType::from_u8(b'<'), InstructionType::Left);
assert_eq!(InstructionType::from_u8(b'+'), InstructionType::Plus);
assert_eq!(InstructionType::from_u8(b'-'), InstructionType::Minus);
assert_eq!(InstructionType::from_u8(b'.'), InstructionType::PutChar);
assert_eq!(InstructionType::from_u8(b','), InstructionType::ReadChar);
assert_eq!(InstructionType::from_u8(b'['), InstructionType::JumpIfZero);
assert_eq!(InstructionType::from_u8(b']'), InstructionType::JumpIfNotZero);
assert_eq!(InstructionType::from(b'>'), InstructionType::Right);
assert_eq!(InstructionType::from(b'<'), InstructionType::Left);
assert_eq!(InstructionType::from(b'+'), InstructionType::Plus);
assert_eq!(InstructionType::from(b'-'), InstructionType::Minus);
assert_eq!(InstructionType::from(b'.'), InstructionType::PutChar);
assert_eq!(InstructionType::from(b','), InstructionType::ReadChar);
assert_eq!(InstructionType::from(b'['), InstructionType::JumpIfZero);
assert_eq!(InstructionType::from(b']'), InstructionType::JumpIfNotZero);
}

// Test from_u8 with invalid input (should panic)
#[test]
#[should_panic(expected = "Invalid instruction")]
fn test_instruction_type_from_u8_invalid() {
InstructionType::from_u8(b'x');
let _ = InstructionType::from(b'x');
}

// Test Instruction struct creation
Expand Down
2 changes: 1 addition & 1 deletion crates/brainfuck_vm/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Machine {
self.program.code[(self.state.registers.ip + BaseField::one()).0 as usize]
};
self.write_trace();
let ins_type = InstructionType::from_u8(self.state.registers.ci.0 as u8);
let ins_type = InstructionType::from(self.state.registers.ci.0 as u8);
self.execute_instruction(&ins_type)?;
self.next_clock_cycle();
}
Expand Down