Skip to content

Commit

Permalink
feat: derive serde for OpCode, improve implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Mar 21, 2024
1 parent 4c15846 commit 74209f9
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,17 @@ opcodes! {
///
/// This is always a valid opcode, as declared in the [`opcode`][self] module or the
/// [`OPCODE_JUMPMAP`] constant.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(transparent)]
pub struct OpCode(u8);

impl fmt::Debug for OpCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "OpCode::{self}")
}
}

impl fmt::Display for OpCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let n = self.get();
Expand All @@ -463,13 +470,8 @@ impl OpCode {
}

/// Instantiate a new opcode from a u8 without checking if it is valid.
///
/// # Safety
///
/// All code using `Opcode` values assume that they are valid opcodes, so providing an invalid
/// opcode may cause undefined behavior.
#[inline]
pub unsafe fn new_unchecked(opcode: u8) -> Self {
pub fn new_unchecked(opcode: u8) -> Self {
Self(opcode)
}

Expand All @@ -488,20 +490,6 @@ impl OpCode {
pub const fn get(self) -> u8 {
self.0
}

#[inline]
#[deprecated(note = "use `new` instead")]
#[doc(hidden)]
pub const fn try_from_u8(opcode: u8) -> Option<Self> {
Self::new(opcode)
}

#[inline]
#[deprecated(note = "use `get` instead")]
#[doc(hidden)]
pub const fn u8(self) -> u8 {
self.get()
}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down

0 comments on commit 74209f9

Please sign in to comment.