|
1 | 1 | use crate::{
|
2 |
| - core::{FuelCostsProvider, Typed, TypedVal, ValType}, |
| 2 | + core::{FuelCostsProvider, Typed, TypedVal, UntypedVal, ValType}, |
3 | 3 | ir::{Const16, Instruction, Sign},
|
4 | 4 | Error,
|
5 | 5 | ExternRef,
|
6 | 6 | FuncRef,
|
7 | 7 | };
|
| 8 | +use core::num::NonZero; |
8 | 9 |
|
9 | 10 | macro_rules! impl_typed_for {
|
10 | 11 | ( $( $ty:ident ),* $(,)? ) => {
|
@@ -39,35 +40,45 @@ impl_typed_for! {
|
39 | 40 | ///
|
40 | 41 | /// This trait provides some utility methods useful for translation.
|
41 | 42 | 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>> |
43 | 51 | {
|
| 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 | + |
44 | 60 | /// Returns `true` if `self` is equal to zero (0).
|
45 | 61 | fn eq_zero(self) -> bool;
|
46 | 62 | }
|
47 | 63 |
|
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>; |
59 | 69 |
|
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 | + } |
65 | 73 |
|
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 | + }; |
70 | 80 | }
|
| 81 | +impl_wasm_integer!(i32, u32, i64, u64); |
71 | 82 |
|
72 | 83 | /// A WebAssembly float. Either `f32` or `f64`.
|
73 | 84 | ///
|
|
0 commit comments