Skip to content

Commit b3c1c65

Browse files
authored
impl From<u8> for InstructionType (#18)
1 parent 03a15d4 commit b3c1c65

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

crates/brainfuck_vm/src/instruction.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ impl Display for InstructionType {
6666
}
6767
}
6868

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

@@ -114,21 +114,21 @@ mod tests {
114114
// Test from_u8 implementation
115115
#[test]
116116
fn test_instruction_type_from_u8() {
117-
assert_eq!(InstructionType::from_u8(b'>'), InstructionType::Right);
118-
assert_eq!(InstructionType::from_u8(b'<'), InstructionType::Left);
119-
assert_eq!(InstructionType::from_u8(b'+'), InstructionType::Plus);
120-
assert_eq!(InstructionType::from_u8(b'-'), InstructionType::Minus);
121-
assert_eq!(InstructionType::from_u8(b'.'), InstructionType::PutChar);
122-
assert_eq!(InstructionType::from_u8(b','), InstructionType::ReadChar);
123-
assert_eq!(InstructionType::from_u8(b'['), InstructionType::JumpIfZero);
124-
assert_eq!(InstructionType::from_u8(b']'), InstructionType::JumpIfNotZero);
117+
assert_eq!(InstructionType::from(b'>'), InstructionType::Right);
118+
assert_eq!(InstructionType::from(b'<'), InstructionType::Left);
119+
assert_eq!(InstructionType::from(b'+'), InstructionType::Plus);
120+
assert_eq!(InstructionType::from(b'-'), InstructionType::Minus);
121+
assert_eq!(InstructionType::from(b'.'), InstructionType::PutChar);
122+
assert_eq!(InstructionType::from(b','), InstructionType::ReadChar);
123+
assert_eq!(InstructionType::from(b'['), InstructionType::JumpIfZero);
124+
assert_eq!(InstructionType::from(b']'), InstructionType::JumpIfNotZero);
125125
}
126126

127127
// Test from_u8 with invalid input (should panic)
128128
#[test]
129129
#[should_panic(expected = "Invalid instruction")]
130130
fn test_instruction_type_from_u8_invalid() {
131-
InstructionType::from_u8(b'x');
131+
let _ = InstructionType::from(b'x');
132132
}
133133

134134
// Test Instruction struct creation

crates/brainfuck_vm/src/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Machine {
119119
self.program.code[(self.state.registers.ip + BaseField::one()).0 as usize]
120120
};
121121
self.write_trace();
122-
let ins_type = InstructionType::from_u8(self.state.registers.ci.0 as u8);
122+
let ins_type = InstructionType::from(self.state.registers.ci.0 as u8);
123123
self.execute_instruction(&ins_type)?;
124124
self.next_clock_cycle();
125125
}

0 commit comments

Comments
 (0)