From 45234eb09a2ede827731bc5f22869e81fc653755 Mon Sep 17 00:00:00 2001 From: Kaz Wesley Date: Sun, 11 Nov 2018 14:19:05 -0800 Subject: [PATCH] safe implementation of All -> Ordinary --- src/blockdata/opcodes.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/blockdata/opcodes.rs b/src/blockdata/opcodes.rs index 88bf3f82..a7b66db5 100644 --- a/src/blockdata/opcodes.rs +++ b/src/blockdata/opcodes.rs @@ -22,9 +22,6 @@ #[cfg(feature = "serde")] use serde; -// Heavy stick to translate between opcode types -use std::mem::transmute; - use std::fmt; use consensus::encode::{self, Decoder, Encoder}; @@ -690,7 +687,7 @@ impl All { Class::PushBytes(self.0 as u32) // 60 opcodes } else { - Class::Ordinary(unsafe { transmute(self.0) }) + Class::Ordinary(Ordinary::try_from_all(*self).unwrap()) } } @@ -781,6 +778,16 @@ macro_rules! ordinary_opcode { pub enum Ordinary { $( $op = All::$op.0 ),* } + + impl Ordinary { + /// Try to create from an All + pub fn try_from_all(b: All) -> Option { + match b { + $( All::$op => { Some(Ordinary::$op) } ),* + _ => None, + } + } + } ); }