Skip to content

Commit c5f4eab

Browse files
committed
Change objint to impl style with properties
1 parent 2bdaa02 commit c5f4eab

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

derive/src/pyclass.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,25 @@ impl ClassItem {
102102
}
103103
let py_name = py_name.unwrap_or_else(|| {
104104
let item_ident = sig.ident.to_string();
105-
if item_ident.starts_with("set_") {
106-
let name = &item_ident["set_".len()..];
107-
if name.is_empty() {
108-
panic!(
109-
"A #[pyproperty(setter)] fn with a set_* name have something \
110-
after \"set_\""
111-
)
105+
if setter {
106+
if item_ident.starts_with("set_") {
107+
let name = &item_ident["set_".len()..];
108+
if name.is_empty() {
109+
panic!(
110+
"A #[pyproperty(setter)] fn with a set_* name have something \
111+
after \"set_\""
112+
)
113+
} else {
114+
name.to_string()
115+
}
112116
} else {
113-
name.to_string()
114-
}
115-
} else {
116-
panic!(
117+
panic!(
117118
"A #[pyproperty(setter)] fn must either have a `name` parameter or a \
118119
fn name along the lines of \"set_*\""
119120
)
121+
}
122+
} else {
123+
item_ident
120124
}
121125
});
122126
item = Some(ClassItem::Property {
@@ -195,7 +199,7 @@ pub fn impl_pyimpl(attr: AttributeArgs, item: Item) -> TokenStream2 {
195199
quote! {
196200
class.set_str_attr(
197201
#name,
198-
#rp_path::obj::objproperty::PropertyBuilder
202+
#rp_path::obj::objproperty::PropertyBuilder::new(ctx)
199203
.add_getter(Self::#getter)
200204
#add_setter
201205
.create(),

vm/src/obj/objint.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use super::objstr::{PyString, PyStringRef};
1717
use super::objtype;
1818
use crate::obj::objtype::PyClassRef;
1919

20+
#[pyclass(__inside_vm)]
2021
#[derive(Debug)]
2122
pub struct PyInt {
2223
value: BigInt,
@@ -95,7 +96,9 @@ impl_try_from_object_int!(
9596
(u64, to_u64),
9697
);
9798

99+
#[pyimpl(__inside_vm)]
98100
impl PyInt {
101+
#[pymethod(name = "__eq__")]
99102
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
100103
if objtype::isinstance(&other, &vm.ctx.int_type()) {
101104
vm.ctx.new_bool(self.value == *get_value(&other))
@@ -104,6 +107,7 @@ impl PyInt {
104107
}
105108
}
106109

110+
#[pymethod(name = "__ne__")]
107111
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
108112
if objtype::isinstance(&other, &vm.ctx.int_type()) {
109113
vm.ctx.new_bool(self.value != *get_value(&other))
@@ -112,6 +116,7 @@ impl PyInt {
112116
}
113117
}
114118

119+
#[pymethod(name = "__lt__")]
115120
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
116121
if objtype::isinstance(&other, &vm.ctx.int_type()) {
117122
vm.ctx.new_bool(self.value < *get_value(&other))
@@ -120,6 +125,7 @@ impl PyInt {
120125
}
121126
}
122127

128+
#[pymethod(name = "__le__")]
123129
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
124130
if objtype::isinstance(&other, &vm.ctx.int_type()) {
125131
vm.ctx.new_bool(self.value <= *get_value(&other))
@@ -128,6 +134,7 @@ impl PyInt {
128134
}
129135
}
130136

137+
#[pymethod(name = "__gt__")]
131138
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
132139
if objtype::isinstance(&other, &vm.ctx.int_type()) {
133140
vm.ctx.new_bool(self.value > *get_value(&other))
@@ -136,6 +143,7 @@ impl PyInt {
136143
}
137144
}
138145

146+
#[pymethod(name = "__ge__")]
139147
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
140148
if objtype::isinstance(&other, &vm.ctx.int_type()) {
141149
vm.ctx.new_bool(self.value >= *get_value(&other))
@@ -144,6 +152,7 @@ impl PyInt {
144152
}
145153
}
146154

155+
#[pymethod(name = "__add__")]
147156
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
148157
if objtype::isinstance(&other, &vm.ctx.int_type()) {
149158
vm.ctx.new_int((&self.value) + get_value(&other))
@@ -152,6 +161,7 @@ impl PyInt {
152161
}
153162
}
154163

164+
#[pymethod(name = "__sub__")]
155165
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
156166
if objtype::isinstance(&other, &vm.ctx.int_type()) {
157167
vm.ctx.new_int((&self.value) - get_value(&other))
@@ -160,6 +170,7 @@ impl PyInt {
160170
}
161171
}
162172

173+
#[pymethod(name = "__rsub__")]
163174
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
164175
if objtype::isinstance(&other, &vm.ctx.int_type()) {
165176
vm.ctx.new_int(get_value(&other) - (&self.value))
@@ -168,6 +179,7 @@ impl PyInt {
168179
}
169180
}
170181

182+
#[pymethod(name = "__mul__")]
171183
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
172184
if objtype::isinstance(&other, &vm.ctx.int_type()) {
173185
vm.ctx.new_int((&self.value) * get_value(&other))
@@ -176,6 +188,7 @@ impl PyInt {
176188
}
177189
}
178190

191+
#[pymethod(name = "__truediv__")]
179192
fn truediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
180193
if objtype::isinstance(&other, &vm.ctx.int_type()) {
181194
div_ints(vm, &self.value, &get_value(&other))
@@ -184,6 +197,7 @@ impl PyInt {
184197
}
185198
}
186199

200+
#[pymethod(name = "__rtruediv__")]
187201
fn rtruediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
188202
if objtype::isinstance(&other, &vm.ctx.int_type()) {
189203
div_ints(vm, &get_value(&other), &self.value)
@@ -192,6 +206,7 @@ impl PyInt {
192206
}
193207
}
194208

209+
#[pymethod(name = "__floordiv__")]
195210
fn floordiv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
196211
if objtype::isinstance(&other, &vm.ctx.int_type()) {
197212
let v2 = get_value(&other);
@@ -205,6 +220,7 @@ impl PyInt {
205220
}
206221
}
207222

223+
#[pymethod(name = "__lshift__")]
208224
fn lshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
209225
if !objtype::isinstance(&other, &vm.ctx.int_type()) {
210226
return Ok(vm.ctx.not_implemented());
@@ -224,6 +240,7 @@ impl PyInt {
224240
}
225241
}
226242

243+
#[pymethod(name = "__rshift__")]
227244
fn rshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
228245
if !objtype::isinstance(&other, &vm.ctx.int_type()) {
229246
return Ok(vm.ctx.not_implemented());
@@ -243,6 +260,7 @@ impl PyInt {
243260
}
244261
}
245262

263+
#[pymethod(name = "__xor__")]
246264
fn xor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
247265
if objtype::isinstance(&other, &vm.ctx.int_type()) {
248266
vm.ctx.new_int((&self.value) ^ get_value(&other))
@@ -251,6 +269,7 @@ impl PyInt {
251269
}
252270
}
253271

272+
#[pymethod(name = "__rxor__")]
254273
fn rxor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
255274
if objtype::isinstance(&other, &vm.ctx.int_type()) {
256275
vm.ctx.new_int(get_value(&other) ^ (&self.value))
@@ -259,6 +278,7 @@ impl PyInt {
259278
}
260279
}
261280

281+
#[pymethod(name = "__or__")]
262282
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
263283
if objtype::isinstance(&other, &vm.ctx.int_type()) {
264284
vm.ctx.new_int((&self.value) | get_value(&other))
@@ -267,6 +287,7 @@ impl PyInt {
267287
}
268288
}
269289

290+
#[pymethod(name = "__and__")]
270291
fn and(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
271292
if objtype::isinstance(&other, &vm.ctx.int_type()) {
272293
let v2 = get_value(&other);
@@ -276,6 +297,7 @@ impl PyInt {
276297
}
277298
}
278299

300+
#[pymethod(name = "__pow__")]
279301
fn pow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
280302
if objtype::isinstance(&other, &vm.ctx.int_type()) {
281303
let v2 = get_value(&other).to_u32().unwrap();
@@ -288,6 +310,7 @@ impl PyInt {
288310
}
289311
}
290312

313+
#[pymethod(name = "__mod__")]
291314
fn mod_(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
292315
if objtype::isinstance(&other, &vm.ctx.int_type()) {
293316
let v2 = get_value(&other);
@@ -301,6 +324,7 @@ impl PyInt {
301324
}
302325
}
303326

327+
#[pymethod(name = "__divmod__")]
304328
fn divmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
305329
if objtype::isinstance(&other, &vm.ctx.int_type()) {
306330
let v2 = get_value(&other);
@@ -317,20 +341,24 @@ impl PyInt {
317341
}
318342
}
319343

344+
#[pymethod(name = "__neg__")]
320345
fn neg(&self, _vm: &VirtualMachine) -> BigInt {
321346
-(&self.value)
322347
}
323348

349+
#[pymethod(name = "__hash__")]
324350
fn hash(&self, _vm: &VirtualMachine) -> u64 {
325351
let mut hasher = std::collections::hash_map::DefaultHasher::new();
326352
self.value.hash(&mut hasher);
327353
hasher.finish()
328354
}
329355

356+
#[pymethod(name = "__abs__")]
330357
fn abs(&self, _vm: &VirtualMachine) -> BigInt {
331358
self.value.abs()
332359
}
333360

361+
#[pymethod(name = "__round__")]
334362
fn round(
335363
zelf: PyRef<Self>,
336364
_precision: OptionalArg<PyObjectRef>,
@@ -339,45 +367,55 @@ impl PyInt {
339367
zelf
340368
}
341369

370+
#[pymethod(name = "__int__")]
342371
fn int(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
343372
zelf
344373
}
345374

375+
#[pymethod(name = "__pos__")]
346376
fn pos(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
347377
zelf
348378
}
349379

380+
#[pymethod(name = "__float__")]
350381
fn float(&self, vm: &VirtualMachine) -> PyResult<PyFloat> {
351382
self.value
352383
.to_f64()
353384
.map(PyFloat::from)
354385
.ok_or_else(|| vm.new_overflow_error("int too large to convert to float".to_string()))
355386
}
356387

388+
#[pymethod(name = "__trunc__")]
357389
fn trunc(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
358390
zelf
359391
}
360392

393+
#[pymethod(name = "__floor__")]
361394
fn floor(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
362395
zelf
363396
}
364397

398+
#[pymethod(name = "__ceil__")]
365399
fn ceil(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
366400
zelf
367401
}
368402

403+
#[pymethod(name = "__index__")]
369404
fn index(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
370405
zelf
371406
}
372407

408+
#[pymethod(name = "__invert__")]
373409
fn invert(&self, _vm: &VirtualMachine) -> BigInt {
374410
!(&self.value)
375411
}
376412

413+
#[pymethod(name = "__repr__")]
377414
fn repr(&self, _vm: &VirtualMachine) -> String {
378415
self.value.to_string()
379416
}
380417

418+
#[pymethod(name = "__format__")]
381419
fn format(&self, spec: PyStringRef, vm: &VirtualMachine) -> PyResult<String> {
382420
let format_spec = FormatSpec::parse(&spec.value);
383421
match format_spec.format_int(&self.value) {
@@ -386,22 +424,27 @@ impl PyInt {
386424
}
387425
}
388426

427+
#[pymethod(name = "__bool__")]
389428
fn bool(&self, _vm: &VirtualMachine) -> bool {
390429
!self.value.is_zero()
391430
}
392431

432+
#[pymethod]
393433
fn bit_length(&self, _vm: &VirtualMachine) -> usize {
394434
self.value.bits()
395435
}
396436

437+
#[pymethod]
397438
fn conjugate(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
398439
zelf
399440
}
400441

442+
#[pyproperty]
401443
fn real(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyIntRef {
402444
zelf
403445
}
404446

447+
#[pyproperty]
405448
fn imag(&self, _vm: &VirtualMachine) -> usize {
406449
0
407450
}

0 commit comments

Comments
 (0)