Skip to content

Commit fdc0d4b

Browse files
committed
make OpPos wrap usize instead of u32
1 parent 98c706a commit fdc0d4b

File tree

2 files changed

+11
-39
lines changed

2 files changed

+11
-39
lines changed

crates/wasmi/src/engine/translator/func/instrs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl OpEncoder {
7979
/// Returns the next [`OpPos`].
8080
#[must_use]
8181
pub fn next_instr(&self) -> OpPos {
82-
OpPos::from_usize(self.ops.len())
82+
OpPos::from(self.ops.len())
8383
}
8484

8585
/// Pushes an [`Op::ConsumeFuel`] instruction to `self`.
@@ -230,17 +230,17 @@ impl OpEncoder {
230230
/// # Panics
231231
///
232232
/// If `instr` is out of bounds for `self`.
233-
pub fn get(&self, instr: OpPos) -> &Op {
234-
&self.ops[instr.into_usize()]
233+
pub fn get(&self, pos: OpPos) -> &Op {
234+
&self.ops[usize::from(pos)]
235235
}
236236

237237
/// Returns an exclusive reference to the [`Op`] associated to [`OpPos`].
238238
///
239239
/// # Panics
240240
///
241241
/// If `instr` is out of bounds for `self`.
242-
fn get_mut(&mut self, instr: OpPos) -> &mut Op {
243-
&mut self.ops[instr.into_usize()]
242+
fn get_mut(&mut self, pos: OpPos) -> &mut Op {
243+
&mut self.ops[usize::from(pos)]
244244
}
245245

246246
/// Resets the [`OpPos`] last created via [`OpEncoder::push_instr`].

crates/wasmi/src/engine/translator/utils.rs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,45 +163,17 @@ impl BumpFuelConsumption for Op {
163163

164164
/// A reference to an encoded [`Op`].
165165
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
166-
pub struct OpPos(u32);
166+
pub struct OpPos(usize);
167167

168-
impl From<u32> for OpPos {
169-
fn from(index: u32) -> Self {
168+
impl From<usize> for OpPos {
169+
fn from(index: usize) -> Self {
170170
Self(index)
171171
}
172172
}
173173

174-
impl From<OpPos> for u32 {
175-
fn from(instr: OpPos) -> Self {
176-
instr.0
177-
}
178-
}
179-
180-
impl OpPos {
181-
/// Creates an [`OpPos`] from the given `usize` value.
182-
///
183-
/// # Note
184-
///
185-
/// This intentionally is an API intended for test purposes only.
186-
///
187-
/// # Panics
188-
///
189-
/// If the `value` exceeds limitations for [`OpPos`].
190-
pub fn from_usize(value: usize) -> Self {
191-
let Ok(index) = u32::try_from(value) else {
192-
panic!("out of bounds index {value} for `OpPos`")
193-
};
194-
Self(index)
195-
}
196-
197-
/// Returns an `usize` representation of the instruction index.
198-
pub fn into_usize(self) -> usize {
199-
match usize::try_from(self.0) {
200-
Ok(index) => index,
201-
Err(error) => {
202-
panic!("out of bound index {} for `OpPos`: {error}", self.0)
203-
}
204-
}
174+
impl From<OpPos> for usize {
175+
fn from(pos: OpPos) -> Self {
176+
pos.0
205177
}
206178
}
207179

0 commit comments

Comments
 (0)