safe implementation of All -> Ordinary

This commit is contained in:
Kaz Wesley 2018-11-11 14:19:05 -08:00
parent 0bfef68851
commit 45234eb09a
1 changed files with 11 additions and 4 deletions

View File

@ -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<Self> {
match b {
$( All::$op => { Some(Ordinary::$op) } ),*
_ => None,
}
}
}
);
}