Skip to content

Commit aec3363

Browse files
int: take self by ref
1 parent 48cca16 commit aec3363

1 file changed

Lines changed: 115 additions & 83 deletions

File tree

vm/src/obj/objint.rs

Lines changed: 115 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -95,108 +95,104 @@ impl_try_from_object_int!(
9595
(u64, to_u64),
9696
);
9797

98-
impl PyIntRef {
99-
fn pass_value(self, _vm: &VirtualMachine) -> Self {
100-
self
101-
}
102-
103-
fn eq(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
98+
impl PyInt {
99+
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
104100
if objtype::isinstance(&other, &vm.ctx.int_type()) {
105101
vm.ctx.new_bool(self.value == *get_value(&other))
106102
} else {
107103
vm.ctx.not_implemented()
108104
}
109105
}
110106

111-
fn ne(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
107+
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
112108
if objtype::isinstance(&other, &vm.ctx.int_type()) {
113109
vm.ctx.new_bool(self.value != *get_value(&other))
114110
} else {
115111
vm.ctx.not_implemented()
116112
}
117113
}
118114

119-
fn lt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
115+
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
120116
if objtype::isinstance(&other, &vm.ctx.int_type()) {
121117
vm.ctx.new_bool(self.value < *get_value(&other))
122118
} else {
123119
vm.ctx.not_implemented()
124120
}
125121
}
126122

127-
fn le(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
123+
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
128124
if objtype::isinstance(&other, &vm.ctx.int_type()) {
129125
vm.ctx.new_bool(self.value <= *get_value(&other))
130126
} else {
131127
vm.ctx.not_implemented()
132128
}
133129
}
134130

135-
fn gt(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
131+
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
136132
if objtype::isinstance(&other, &vm.ctx.int_type()) {
137133
vm.ctx.new_bool(self.value > *get_value(&other))
138134
} else {
139135
vm.ctx.not_implemented()
140136
}
141137
}
142138

143-
fn ge(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
139+
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
144140
if objtype::isinstance(&other, &vm.ctx.int_type()) {
145141
vm.ctx.new_bool(self.value >= *get_value(&other))
146142
} else {
147143
vm.ctx.not_implemented()
148144
}
149145
}
150146

151-
fn add(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
147+
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
152148
if objtype::isinstance(&other, &vm.ctx.int_type()) {
153149
vm.ctx.new_int((&self.value) + get_value(&other))
154150
} else {
155151
vm.ctx.not_implemented()
156152
}
157153
}
158154

159-
fn sub(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
155+
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
160156
if objtype::isinstance(&other, &vm.ctx.int_type()) {
161157
vm.ctx.new_int((&self.value) - get_value(&other))
162158
} else {
163159
vm.ctx.not_implemented()
164160
}
165161
}
166162

167-
fn rsub(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
163+
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
168164
if objtype::isinstance(&other, &vm.ctx.int_type()) {
169165
vm.ctx.new_int(get_value(&other) - (&self.value))
170166
} else {
171167
vm.ctx.not_implemented()
172168
}
173169
}
174170

175-
fn mul(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
171+
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
176172
if objtype::isinstance(&other, &vm.ctx.int_type()) {
177173
vm.ctx.new_int((&self.value) * get_value(&other))
178174
} else {
179175
vm.ctx.not_implemented()
180176
}
181177
}
182178

183-
fn truediv(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
179+
fn truediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
184180
if objtype::isinstance(&other, &vm.ctx.int_type()) {
185181
div_ints(vm, &self.value, &get_value(&other))
186182
} else {
187183
Ok(vm.ctx.not_implemented())
188184
}
189185
}
190186

191-
fn rtruediv(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
187+
fn rtruediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
192188
if objtype::isinstance(&other, &vm.ctx.int_type()) {
193189
div_ints(vm, &get_value(&other), &self.value)
194190
} else {
195191
Ok(vm.ctx.not_implemented())
196192
}
197193
}
198194

199-
fn floordiv(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
195+
fn floordiv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
200196
if objtype::isinstance(&other, &vm.ctx.int_type()) {
201197
let v2 = get_value(&other);
202198
if *v2 != BigInt::zero() {
@@ -209,7 +205,7 @@ impl PyIntRef {
209205
}
210206
}
211207

212-
fn lshift(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
208+
fn lshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
213209
if !objtype::isinstance(&other, &vm.ctx.int_type()) {
214210
return Ok(vm.ctx.not_implemented());
215211
}
@@ -228,7 +224,7 @@ impl PyIntRef {
228224
}
229225
}
230226

231-
fn rshift(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
227+
fn rshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
232228
if !objtype::isinstance(&other, &vm.ctx.int_type()) {
233229
return Ok(vm.ctx.not_implemented());
234230
}
@@ -247,31 +243,31 @@ impl PyIntRef {
247243
}
248244
}
249245

250-
fn xor(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
246+
fn xor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
251247
if objtype::isinstance(&other, &vm.ctx.int_type()) {
252248
vm.ctx.new_int((&self.value) ^ get_value(&other))
253249
} else {
254250
vm.ctx.not_implemented()
255251
}
256252
}
257253

258-
fn rxor(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
254+
fn rxor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
259255
if objtype::isinstance(&other, &vm.ctx.int_type()) {
260256
vm.ctx.new_int(get_value(&other) ^ (&self.value))
261257
} else {
262258
vm.ctx.not_implemented()
263259
}
264260
}
265261

266-
fn or(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
262+
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
267263
if objtype::isinstance(&other, &vm.ctx.int_type()) {
268264
vm.ctx.new_int((&self.value) | get_value(&other))
269265
} else {
270266
vm.ctx.not_implemented()
271267
}
272268
}
273269

274-
fn and(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
270+
fn and(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
275271
if objtype::isinstance(&other, &vm.ctx.int_type()) {
276272
let v2 = get_value(&other);
277273
vm.ctx.new_int((&self.value) & v2)
@@ -280,7 +276,7 @@ impl PyIntRef {
280276
}
281277
}
282278

283-
fn pow(self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
279+
fn pow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
284280
if objtype::isinstance(&other, &vm.ctx.int_type()) {
285281
let v2 = get_value(&other).to_u32().unwrap();
286282
vm.ctx.new_int(self.value.pow(v2))
@@ -292,7 +288,7 @@ impl PyIntRef {
292288
}
293289
}
294290

295-
fn mod_(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
291+
fn mod_(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
296292
if objtype::isinstance(&other, &vm.ctx.int_type()) {
297293
let v2 = get_value(&other);
298294
if *v2 != BigInt::zero() {
@@ -305,7 +301,7 @@ impl PyIntRef {
305301
}
306302
}
307303

308-
fn divmod(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
304+
fn divmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
309305
if objtype::isinstance(&other, &vm.ctx.int_type()) {
310306
let v2 = get_value(&other);
311307
if *v2 != BigInt::zero() {
@@ -321,53 +317,89 @@ impl PyIntRef {
321317
}
322318
}
323319

324-
fn neg(self, _vm: &VirtualMachine) -> BigInt {
320+
fn neg(&self, _vm: &VirtualMachine) -> BigInt {
325321
-(&self.value)
326322
}
327323

328-
fn hash(self, _vm: &VirtualMachine) -> u64 {
324+
fn hash(&self, _vm: &VirtualMachine) -> u64 {
329325
let mut hasher = std::collections::hash_map::DefaultHasher::new();
330326
self.value.hash(&mut hasher);
331327
hasher.finish()
332328
}
333329

334-
fn abs(self, _vm: &VirtualMachine) -> BigInt {
330+
fn abs(&self, _vm: &VirtualMachine) -> BigInt {
335331
self.value.abs()
336332
}
337333

338-
fn round(self, _precision: OptionalArg<PyObjectRef>, _vm: &VirtualMachine) -> Self {
339-
self
334+
fn round(
335+
zelf: PyRef<Self>,
336+
_precision: OptionalArg<PyObjectRef>,
337+
_vm: &VirtualMachine,
338+
) -> PyIntRef {
339+
zelf
340+
}
341+
342+
fn int(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
343+
zelf
340344
}
341345

342-
fn float(self, _vm: &VirtualMachine) -> f64 {
343-
self.value.to_f64().unwrap()
346+
fn pos(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
347+
zelf
344348
}
345349

346-
fn invert(self, _vm: &VirtualMachine) -> BigInt {
350+
fn float(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
351+
zelf
352+
}
353+
354+
fn trunc(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
355+
zelf
356+
}
357+
358+
fn floor(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
359+
zelf
360+
}
361+
362+
fn ceil(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
363+
zelf
364+
}
365+
366+
fn index(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
367+
zelf
368+
}
369+
370+
fn invert(&self, _vm: &VirtualMachine) -> BigInt {
347371
!(&self.value)
348372
}
349373

350-
fn repr(self, _vm: &VirtualMachine) -> String {
374+
fn repr(&self, _vm: &VirtualMachine) -> String {
351375
self.value.to_string()
352376
}
353377

354-
fn format(self, spec: PyStringRef, vm: &VirtualMachine) -> PyResult<String> {
378+
fn format(&self, spec: PyStringRef, vm: &VirtualMachine) -> PyResult<String> {
355379
let format_spec = FormatSpec::parse(&spec.value);
356380
match format_spec.format_int(&self.value) {
357381
Ok(string) => Ok(string),
358382
Err(err) => Err(vm.new_value_error(err.to_string())),
359383
}
360384
}
361385

362-
fn bool(self, _vm: &VirtualMachine) -> bool {
386+
fn bool(&self, _vm: &VirtualMachine) -> bool {
363387
!self.value.is_zero()
364388
}
365389

366-
fn bit_length(self, _vm: &VirtualMachine) -> usize {
390+
fn bit_length(&self, _vm: &VirtualMachine) -> usize {
367391
self.value.bits()
368392
}
369393

370-
fn imag(self, _vm: &VirtualMachine) -> usize {
394+
fn conjugate(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
395+
zelf
396+
}
397+
398+
fn real(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
399+
zelf
400+
}
401+
402+
fn imag(&self, _vm: &VirtualMachine) -> usize {
371403
0
372404
}
373405
}
@@ -484,49 +516,49 @@ Base 0 means to interpret the base from the string as an integer literal.
484516
let int_type = &context.int_type;
485517
extend_class!(context, int_type, {
486518
"__doc__" => context.new_str(int_doc.to_string()),
487-
"__eq__" => context.new_rustfunc(PyIntRef::eq),
488-
"__ne__" => context.new_rustfunc(PyIntRef::ne),
489-
"__lt__" => context.new_rustfunc(PyIntRef::lt),
490-
"__le__" => context.new_rustfunc(PyIntRef::le),
491-
"__gt__" => context.new_rustfunc(PyIntRef::gt),
492-
"__ge__" => context.new_rustfunc(PyIntRef::ge),
493-
"__abs__" => context.new_rustfunc(PyIntRef::abs),
494-
"__add__" => context.new_rustfunc(PyIntRef::add),
495-
"__radd__" => context.new_rustfunc(PyIntRef::add),
496-
"__and__" => context.new_rustfunc(PyIntRef::and),
497-
"__divmod__" => context.new_rustfunc(PyIntRef::divmod),
498-
"__float__" => context.new_rustfunc(PyIntRef::float),
499-
"__round__" => context.new_rustfunc(PyIntRef::round),
500-
"__ceil__" => context.new_rustfunc(PyIntRef::pass_value),
501-
"__floor__" => context.new_rustfunc(PyIntRef::pass_value),
502-
"__index__" => context.new_rustfunc(PyIntRef::pass_value),
503-
"__trunc__" => context.new_rustfunc(PyIntRef::pass_value),
504-
"__int__" => context.new_rustfunc(PyIntRef::pass_value),
505-
"__floordiv__" => context.new_rustfunc(PyIntRef::floordiv),
506-
"__hash__" => context.new_rustfunc(PyIntRef::hash),
507-
"__lshift__" => context.new_rustfunc(PyIntRef::lshift),
508-
"__rshift__" => context.new_rustfunc(PyIntRef::rshift),
519+
"__eq__" => context.new_rustfunc(PyInt::eq),
520+
"__ne__" => context.new_rustfunc(PyInt::ne),
521+
"__lt__" => context.new_rustfunc(PyInt::lt),
522+
"__le__" => context.new_rustfunc(PyInt::le),
523+
"__gt__" => context.new_rustfunc(PyInt::gt),
524+
"__ge__" => context.new_rustfunc(PyInt::ge),
525+
"__abs__" => context.new_rustfunc(PyInt::abs),
526+
"__add__" => context.new_rustfunc(PyInt::add),
527+
"__radd__" => context.new_rustfunc(PyInt::add),
528+
"__and__" => context.new_rustfunc(PyInt::and),
529+
"__divmod__" => context.new_rustfunc(PyInt::divmod),
530+
"__float__" => context.new_rustfunc(PyInt::float),
531+
"__round__" => context.new_rustfunc(PyInt::round),
532+
"__ceil__" => context.new_rustfunc(PyInt::ceil),
533+
"__floor__" => context.new_rustfunc(PyInt::floor),
534+
"__index__" => context.new_rustfunc(PyInt::index),
535+
"__trunc__" => context.new_rustfunc(PyInt::trunc),
536+
"__int__" => context.new_rustfunc(PyInt::int),
537+
"__floordiv__" => context.new_rustfunc(PyInt::floordiv),
538+
"__hash__" => context.new_rustfunc(PyInt::hash),
539+
"__lshift__" => context.new_rustfunc(PyInt::lshift),
540+
"__rshift__" => context.new_rustfunc(PyInt::rshift),
509541
"__new__" => context.new_rustfunc(int_new),
510-
"__mod__" => context.new_rustfunc(PyIntRef::mod_),
511-
"__mul__" => context.new_rustfunc(PyIntRef::mul),
512-
"__rmul__" => context.new_rustfunc(PyIntRef::mul),
513-
"__or__" => context.new_rustfunc(PyIntRef::or),
514-
"__neg__" => context.new_rustfunc(PyIntRef::neg),
515-
"__pos__" => context.new_rustfunc(PyIntRef::pass_value),
516-
"__pow__" => context.new_rustfunc(PyIntRef::pow),
517-
"__repr__" => context.new_rustfunc(PyIntRef::repr),
518-
"__sub__" => context.new_rustfunc(PyIntRef::sub),
519-
"__rsub__" => context.new_rustfunc(PyIntRef::rsub),
520-
"__format__" => context.new_rustfunc(PyIntRef::format),
521-
"__truediv__" => context.new_rustfunc(PyIntRef::truediv),
522-
"__rtruediv__" => context.new_rustfunc(PyIntRef::rtruediv),
523-
"__xor__" => context.new_rustfunc(PyIntRef::xor),
524-
"__rxor__" => context.new_rustfunc(PyIntRef::rxor),
525-
"__bool__" => context.new_rustfunc(PyIntRef::bool),
526-
"__invert__" => context.new_rustfunc(PyIntRef::invert),
527-
"bit_length" => context.new_rustfunc(PyIntRef::bit_length),
528-
"conjugate" => context.new_rustfunc(PyIntRef::pass_value),
529-
"real" => context.new_property(PyIntRef::pass_value),
530-
"imag" => context.new_property(PyIntRef::imag)
542+
"__mod__" => context.new_rustfunc(PyInt::mod_),
543+
"__mul__" => context.new_rustfunc(PyInt::mul),
544+
"__rmul__" => context.new_rustfunc(PyInt::mul),
545+
"__or__" => context.new_rustfunc(PyInt::or),
546+
"__neg__" => context.new_rustfunc(PyInt::neg),
547+
"__pos__" => context.new_rustfunc(PyInt::pos),
548+
"__pow__" => context.new_rustfunc(PyInt::pow),
549+
"__repr__" => context.new_rustfunc(PyInt::repr),
550+
"__sub__" => context.new_rustfunc(PyInt::sub),
551+
"__rsub__" => context.new_rustfunc(PyInt::rsub),
552+
"__format__" => context.new_rustfunc(PyInt::format),
553+
"__truediv__" => context.new_rustfunc(PyInt::truediv),
554+
"__rtruediv__" => context.new_rustfunc(PyInt::rtruediv),
555+
"__xor__" => context.new_rustfunc(PyInt::xor),
556+
"__rxor__" => context.new_rustfunc(PyInt::rxor),
557+
"__bool__" => context.new_rustfunc(PyInt::bool),
558+
"__invert__" => context.new_rustfunc(PyInt::invert),
559+
"bit_length" => context.new_rustfunc(PyInt::bit_length),
560+
"conjugate" => context.new_rustfunc(PyInt::conjugate),
561+
"real" => context.new_property(PyInt::real),
562+
"imag" => context.new_property(PyInt::imag)
531563
});
532564
}

0 commit comments

Comments
 (0)