libperl_sys/
conv_opcode.rs1use std::convert::TryFrom;
2
3use crate::perl_core::{op, opcode};
4
5impl TryFrom<*const op> for opcode {
6 type Error = &'static str;
7
8 fn try_from(op: *const op) -> Result<Self, Self::Error> {
9 if op.is_null() {
10 return Err("Null OP*")
11 }
12 opcode::try_from(unsafe {
13 (*op).op_type()
14 })
15 }
16}
17
18impl TryFrom<u32> for opcode {
19 type Error = &'static str;
20
21 fn try_from(oc: u32) -> Result<Self, Self::Error> {
22 if oc <= opcode::OP_max as u32 {
23 let e = unsafe {std::mem::transmute(oc)};
24 Ok(e)
25 } else {
26 Err("Invalid opcode")
27 }
28 }
29}
30
31impl TryFrom<u16> for opcode {
32 type Error = &'static str;
33
34 fn try_from(oc: u16) -> Result<Self, Self::Error> {
35 if oc <= opcode::OP_max as u16 {
36 let e = unsafe {std::mem::transmute(oc as u32)};
37 Ok(e)
38 } else {
39 Err("Invalid opcode")
40 }
41 }
42}