Skip to content

Commit 93c38d8

Browse files
committed
const Opcode.name() method
1 parent c4caae2 commit 93c38d8

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

crates/compiler-core/src/bytecode/instruction.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ macro_rules! define_opcodes {
3333

3434
impl $opcode_name {
3535
#[must_use]
36-
pub const fn as_instruction(&self) -> $instr_name {
36+
$opcode_vis const fn as_instruction(&self) -> $instr_name {
3737
match self {
3838
$(
3939
Self::$op_name => $instr_name::$op_name $({ $arg_name: Arg::marker() })?,
4040
)*
4141
}
4242
}
43+
44+
#[must_use]
45+
$opcode_vis const fn name(&self) -> &str {
46+
match self {
47+
$(Self::$op_name => $op_display,)*
48+
}
49+
}
4350
}
4451

4552
impl From<$opcode_name> for $instr_name {
@@ -70,9 +77,7 @@ macro_rules! define_opcodes {
7077

7178
impl ::core::fmt::Display for $opcode_name {
7279
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
73-
match self {
74-
$(Self::$op_name => write!(f, $op_display),)*
75-
}
80+
write!(f, "{}", self.name())
7681
}
7782
}
7883

@@ -87,7 +92,7 @@ macro_rules! define_opcodes {
8792

8893
impl $instr_name {
8994
#[must_use]
90-
pub const fn opcode(&self) -> $opcode_name {
95+
$instr_vis const fn opcode(&self) -> $opcode_name {
9196
match self {
9297
$(
9398
Self::$op_name $({ $arg_name: _ })? => $opcode_name::$op_name,

0 commit comments

Comments
 (0)