@@ -42,51 +42,43 @@ impl_typed_for! {
42
42
pub trait WasmInteger :
43
43
Copy
44
44
+ Eq
45
+ + Typed
45
46
+ From < TypedVal >
46
47
+ Into < TypedVal >
47
48
+ From < UntypedVal >
48
49
+ Into < UntypedVal >
49
50
+ TryInto < Const16 < Self > >
50
- + Typed
51
51
{
52
52
/// The non-zero type of the [`WasmInteger`].
53
- type NonZero : Copy + TryFrom < Self > + Into < UntypedVal > + TryInto < Const16 < Self :: NonZero > > ;
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 > ;
54
59
55
60
/// Returns `true` if `self` is equal to zero (0).
56
61
fn eq_zero ( self ) -> bool ;
57
62
}
58
63
59
- impl WasmInteger for i32 {
60
- type NonZero = NonZero < Self > ;
61
-
62
- fn eq_zero ( self ) -> bool {
63
- self == 0
64
- }
65
- }
66
-
67
- impl WasmInteger for u32 {
68
- type NonZero = NonZero < Self > ;
69
-
70
- fn eq_zero ( self ) -> bool {
71
- self == 0
72
- }
73
- }
74
-
75
- impl WasmInteger for i64 {
76
- type NonZero = NonZero < Self > ;
77
-
78
- fn eq_zero ( self ) -> bool {
79
- self == 0
80
- }
81
- }
64
+ macro_rules! impl_wasm_integer {
65
+ ( $( $ty: ty) ,* ) => {
66
+ $(
67
+ impl WasmInteger for $ty {
68
+ type NonZero = NonZero <Self >;
82
69
83
- impl WasmInteger for u64 {
84
- type NonZero = NonZero < Self > ;
70
+ fn non_zero( self ) -> Option <Self :: NonZero > {
71
+ Self :: NonZero :: new( self )
72
+ }
85
73
86
- fn eq_zero ( self ) -> bool {
87
- self == 0
88
- }
74
+ fn eq_zero( self ) -> bool {
75
+ self == 0
76
+ }
77
+ }
78
+ ) *
79
+ } ;
89
80
}
81
+ impl_wasm_integer ! ( i32 , u32 , i64 , u64 ) ;
90
82
91
83
/// A WebAssembly float. Either `f32` or `f64`.
92
84
///
0 commit comments