Skip to content

Commit 3d91197

Browse files
authored
Constify OpArgState methods (#7616)
1 parent 2827eca commit 3d91197

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

  • crates/compiler-core/src/bytecode

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ impl OpArgByte {
1919
pub const fn new(value: u8) -> Self {
2020
Self(value)
2121
}
22+
23+
/// Returns the inner value as a [`u8`].
24+
#[must_use]
25+
pub const fn as_u8(self) -> u8 {
26+
self.0
27+
}
28+
29+
/// Returns the inner value as a [`u32`].
30+
#[must_use]
31+
pub const fn as_u32(self) -> u32 {
32+
self.0 as u32
33+
}
2234
}
2335

2436
impl From<u8> for OpArgByte {
@@ -29,7 +41,7 @@ impl From<u8> for OpArgByte {
2941

3042
impl From<OpArgByte> for u8 {
3143
fn from(value: OpArgByte) -> Self {
32-
value.0
44+
value.as_u8()
3345
}
3446
}
3547

@@ -93,7 +105,7 @@ pub struct OpArgState {
93105

94106
impl OpArgState {
95107
#[inline(always)]
96-
pub fn get(&mut self, ins: CodeUnit) -> (Instruction, OpArg) {
108+
pub const fn get(&mut self, ins: CodeUnit) -> (Instruction, OpArg) {
97109
let arg = self.extend(ins.arg);
98110
if !matches!(ins.op, Instruction::ExtendedArg) {
99111
self.reset();
@@ -102,9 +114,9 @@ impl OpArgState {
102114
}
103115

104116
#[inline(always)]
105-
pub fn extend(&mut self, arg: OpArgByte) -> OpArg {
106-
self.state = (self.state << 8) | u32::from(arg.0);
107-
self.state.into()
117+
pub const fn extend(&mut self, arg: OpArgByte) -> OpArg {
118+
self.state = (self.state << 8) | arg.as_u32();
119+
OpArg::new(self.state)
108120
}
109121

110122
#[inline(always)]

0 commit comments

Comments
 (0)