1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::ops::AsProxy;
use crate::ops::Rem;
use rbs::Value;

fn op_rem_value(left: Value, rhs: Value) -> Value {
    match left {
        Value::I32(s) => {
            let rhs = rhs.i32();
            Value::I32(s % rhs)
        }
        Value::I64(s) => {
            let rhs = rhs.i64();
            Value::I64(s % rhs)
        }
        Value::U32(s) => {
            let rhs = rhs.u32();
            Value::U32(s % rhs)
        }
        Value::U64(s) => {
            let rhs = rhs.u64();
            Value::U64(s % rhs)
        }
        Value::F32(s) => {
            let rhs = rhs.f64() as f32;
            Value::F32(s % rhs)
        }
        Value::F64(s) => {
            let rhs = rhs.f64();
            Value::F64(s % rhs)
        }
        Value::Ext(_, e) => op_rem_value(*e, rhs),
        _ => Value::Null,
    }
}
//value
impl Rem<Value> for Value {
    type Output = Value;
    fn op_rem(self, rhs: Value) -> Self::Output {
        op_rem_value(self, rhs)
    }
}

impl Rem<&Value> for Value {
    type Output = Value;
    fn op_rem(self, rhs: &Value) -> Self::Output {
        op_rem_value(self, rhs.to_owned())
    }
}

impl Rem<&&Value> for Value {
    type Output = Value;
    fn op_rem(self, rhs: &&Value) -> Self::Output {
        op_rem_value(self, (*rhs).to_owned())
    }
}

impl Rem<Value> for &Value {
    type Output = Value;
    fn op_rem(self, rhs: Value) -> Self::Output {
        op_rem_value(self.to_owned(), rhs)
    }
}

impl Rem<&Value> for &Value {
    type Output = Value;
    fn op_rem(self, rhs: &Value) -> Self::Output {
        op_rem_value(self.to_owned(), rhs.to_owned())
    }
}

impl Rem<&&Value> for &Value {
    type Output = Value;
    fn op_rem(self, rhs: &&Value) -> Self::Output {
        op_rem_value(self.to_owned(), (*rhs).to_owned())
    }
}

fn op_rem_u64(value: &Value, other: u64) -> u64 {
    value.u64() % other
}

fn op_rem_i64(value: &Value, other: i64) -> i64 {
    value.i64() % other
}

fn op_rem_f64(value: &Value, other: f64) -> f64 {
    value.f64() % other
}

fn op_rem_u64_value(value: &Value, other: u64) -> u64 {
    other % value.u64()
}

fn op_rem_i64_value(value: &Value, other: i64) -> i64 {
    other % value.i64()
}

fn op_rem_f64_value(value: &Value, other: f64) -> f64 {
    other % value.f64()
}

macro_rules! impl_numeric_rem {
    ($($rem:ident,$rem_value:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
        $($(
            impl Rem<$ty> for Value {
                type Output = $return_ty;
                fn op_rem(self, other: $ty) -> Self::Output {
                    $rem(&self, other as _)
                }
            }

            impl Rem<Value> for $ty {
                type Output = $return_ty;
                fn op_rem(self, other: Value) -> Self::Output {
                    $rem_value(&other, self as _)
                }
            }

            impl Rem<&Value> for $ty {
                type Output = $return_ty;
                fn op_rem(self, other: &Value) -> Self::Output {
                    $rem_value(other, self as _)
                }
            }

           impl Rem<&&Value> for $ty {
                type Output = $return_ty;
                fn op_rem(self, other: &&Value) -> Self::Output {
                    $rem_value(*other, self as _)
                }
            }

            impl<'a> Rem<$ty> for &'a Value {
                type Output = $return_ty;
                fn op_rem(self, other: $ty) -> Self::Output {
                    $rem(self, other as _)
                }
            }
        )*)*
    }
}

impl_numeric_rem! {
    op_rem_u64,op_rem_u64_value[u8 u16 u32 u64] -> u64
    op_rem_i64,op_rem_i64_value[i8 i16 i32 i64 isize] -> i64
    op_rem_f64,op_rem_f64_value[f32 f64] -> f64
}

macro_rules! rem_self {
    ([$($ty:ty)*]) => {
        $(
impl Rem<$ty> for $ty{
         type Output = $ty;
      fn op_rem(self, rhs: $ty) -> Self::Output {
        self % rhs
      }
    }
impl Rem<&$ty> for $ty{
         type Output = $ty;
      fn op_rem(self, rhs: &$ty) -> Self::Output {
        self % *rhs
      }
    }
impl Rem<$ty> for &$ty{
         type Output = $ty;
      fn op_rem(self, rhs: $ty) -> Self::Output {
        *self % rhs
      }
    }
impl Rem<&$ty> for &$ty{
         type Output = $ty;
      fn op_rem(self, rhs: &$ty) -> Self::Output {
        *self % *rhs
      }
    }
        )*
    };
}
rem_self!([u8 u16 u32 u64]);
rem_self!([i8 i16 i32 i64 isize]);
rem_self!([f32 f64]);