Skip to content

Commit f4b8be8

Browse files
committed
Fix `TryFrom<u8>
1 parent 8adcd36 commit f4b8be8

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

crates/compiler-core/src/bytecode.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -872,21 +872,46 @@ pub enum Instruction {
872872

873873
const _: () = assert!(mem::size_of::<Instruction>() == 1);
874874

875-
impl From<Instruction> for u8 {
876-
#[inline]
877-
fn from(ins: Instruction) -> Self {
878-
// SAFETY: there's no padding bits
879-
unsafe { core::mem::transmute::<Instruction, Self>(ins) }
880-
}
881-
}
882-
883875
impl TryFrom<u8> for Instruction {
884876
type Error = MarshalError;
885877

886878
#[inline]
887879
fn try_from(value: u8) -> Result<Self, MarshalError> {
888-
// TODO: Use `num_enum`
889-
Ok(unsafe { core::mem::transmute::<u8, Self>(value) })
880+
Ok(match value {
881+
1
882+
| 5..=7
883+
| 9
884+
| 10
885+
| 14..=16
886+
| 18..=20
887+
| 24
888+
| 26..=32
889+
| 36
890+
| 37
891+
| 39
892+
| 40
893+
| 45
894+
| 47..=52
895+
| 54..=56
896+
| 58..=61
897+
| 63..=67
898+
| 69
899+
| 71..=76
900+
| 80
901+
| 82..=85
902+
| 91
903+
| 95..=97
904+
| 100
905+
| 101
906+
| 103
907+
| 105
908+
| 106
909+
| 108..=110
910+
| 113..=118
911+
| 149
912+
| 222..=255 => unsafe { core::mem::transmute::<u8, Self>(value) },
913+
_ => return Err(MarshalError::InvalidBytecode),
914+
})
890915
}
891916
}
892917

0 commit comments

Comments
 (0)