Skip to content

Commit 3a61c53

Browse files
committed
refactor WasmInteger trait
1 parent 94fff37 commit 3a61c53

File tree

1 file changed

+33
-22
lines changed
  • crates/wasmi/src/engine/translator

1 file changed

+33
-22
lines changed

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

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{
2-
core::{FuelCostsProvider, Typed, TypedVal, ValType},
2+
core::{FuelCostsProvider, Typed, TypedVal, UntypedVal, ValType},
33
ir::{Const16, Instruction, Sign},
44
Error,
55
ExternRef,
66
FuncRef,
77
};
8+
use core::num::NonZero;
89

910
macro_rules! impl_typed_for {
1011
( $( $ty:ident ),* $(,)? ) => {
@@ -39,35 +40,45 @@ impl_typed_for! {
3940
///
4041
/// This trait provides some utility methods useful for translation.
4142
pub trait WasmInteger:
42-
Copy + Eq + From<TypedVal> + Into<TypedVal> + TryInto<Const16<Self>>
43+
Copy
44+
+ Eq
45+
+ Typed
46+
+ From<TypedVal>
47+
+ Into<TypedVal>
48+
+ From<UntypedVal>
49+
+ Into<UntypedVal>
50+
+ TryInto<Const16<Self>>
4351
{
52+
/// The non-zero type of the [`WasmInteger`].
53+
type NonZero: Copy + Into<Self> + TryInto<Const16<Self::NonZero>> + Into<UntypedVal>;
54+
55+
/// Returns `self` as [`Self::NonZero`] if possible.
56+
///
57+
/// Returns `None` if `self` is zero.
58+
fn non_zero(self) -> Option<Self::NonZero>;
59+
4460
/// Returns `true` if `self` is equal to zero (0).
4561
fn eq_zero(self) -> bool;
4662
}
4763

48-
impl WasmInteger for i32 {
49-
fn eq_zero(self) -> bool {
50-
self == 0
51-
}
52-
}
53-
54-
impl WasmInteger for u32 {
55-
fn eq_zero(self) -> bool {
56-
self == 0
57-
}
58-
}
64+
macro_rules! impl_wasm_integer {
65+
($($ty:ty),*) => {
66+
$(
67+
impl WasmInteger for $ty {
68+
type NonZero = NonZero<Self>;
5969

60-
impl WasmInteger for i64 {
61-
fn eq_zero(self) -> bool {
62-
self == 0
63-
}
64-
}
70+
fn non_zero(self) -> Option<Self::NonZero> {
71+
Self::NonZero::new(self)
72+
}
6573

66-
impl WasmInteger for u64 {
67-
fn eq_zero(self) -> bool {
68-
self == 0
69-
}
74+
fn eq_zero(self) -> bool {
75+
self == 0
76+
}
77+
}
78+
)*
79+
};
7080
}
81+
impl_wasm_integer!(i32, u32, i64, u64);
7182

7283
/// A WebAssembly float. Either `f32` or `f64`.
7384
///

0 commit comments

Comments
 (0)