Skip to content

Commit 29e9753

Browse files
committed
Even newer style class definitions.
1 parent 2d127b9 commit 29e9753

1 file changed

Lines changed: 22 additions & 29 deletions

File tree

vm/src/obj/objdict.rs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use crate::pyobject::{
88
};
99
use crate::vm::{ReprGuard, VirtualMachine};
1010

11-
use crate::dictdatatype;
12-
1311
use super::objiter;
1412
use super::objstr;
13+
use crate::dictdatatype;
1514
use crate::obj::objtype::PyClassRef;
15+
use crate::pyobject::PyClassImpl;
1616

1717
pub type DictContentType = dictdatatype::Dict;
1818

@@ -248,17 +248,20 @@ impl ItemProtocol for PyDictRef {
248248
}
249249

250250
macro_rules! dict_iterator {
251-
( $name: ident, $iter_name: ident, $class: ident, $iter_class: ident, $result_fn: expr) => {
251+
( $name: ident, $iter_name: ident, $class: ident, $iter_class: ident, $class_name: literal, $iter_class_name: literal, $result_fn: expr) => {
252+
#[pyclass(name = $class_name, __inside_vm)]
252253
#[derive(Debug)]
253254
struct $name {
254255
pub dict: PyDictRef,
255256
}
256257

258+
#[pyimpl(__inside_vm)]
257259
impl $name {
258260
fn new(dict: PyDictRef) -> Self {
259261
$name { dict: dict }
260262
}
261263

264+
#[pymethod(name = "__iter__")]
262265
fn iter(&self, _vm: &VirtualMachine) -> $iter_name {
263266
$iter_name::new(self.dict.clone())
264267
}
@@ -270,12 +273,14 @@ macro_rules! dict_iterator {
270273
}
271274
}
272275

276+
#[pyclass(name = $iter_class_name, __inside_vm)]
273277
#[derive(Debug)]
274278
struct $iter_name {
275279
pub dict: PyDictRef,
276280
pub position: Cell<usize>,
277281
}
278282

283+
#[pyimpl(__inside_vm)]
279284
impl $iter_name {
280285
fn new(dict: PyDictRef) -> Self {
281286
$iter_name {
@@ -284,6 +289,7 @@ macro_rules! dict_iterator {
284289
}
285290
}
286291

292+
#[pymethod(name = "__next__")]
287293
fn next(&self, vm: &VirtualMachine) -> PyResult {
288294
match self.dict.entries.borrow().next_entry(self.position.get()) {
289295
Some((new_position, key, value)) => {
@@ -294,6 +300,7 @@ macro_rules! dict_iterator {
294300
}
295301
}
296302

303+
#[pymethod(name = "__iter__")]
297304
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
298305
zelf
299306
}
@@ -312,6 +319,8 @@ dict_iterator! {
312319
PyDictKeyIterator,
313320
dictkeys_type,
314321
dictkeyiterator_type,
322+
"dictkeys",
323+
"dictkeyiterator",
315324
|_vm: &VirtualMachine, key: &PyObjectRef, _value: &PyObjectRef| key.clone()
316325
}
317326

@@ -320,6 +329,8 @@ dict_iterator! {
320329
PyDictValueIterator,
321330
dictvalues_type,
322331
dictvalueiterator_type,
332+
"dictvalues",
333+
"dictvalueiterator",
323334
|_vm: &VirtualMachine, _key: &PyObjectRef, value: &PyObjectRef| value.clone()
324335
}
325336

@@ -328,6 +339,8 @@ dict_iterator! {
328339
PyDictItemIterator,
329340
dictitems_type,
330341
dictitemiterator_type,
342+
"dictitems",
343+
"dictitemiterator",
331344
|vm: &VirtualMachine, key: &PyObjectRef, value: &PyObjectRef|
332345
vm.ctx.new_tuple(vec![key.clone(), value.clone()])
333346
}
@@ -354,30 +367,10 @@ pub fn init(context: &PyContext) {
354367
"update" => context.new_rustfunc(PyDictRef::update),
355368
});
356369

357-
extend_class!(context, &context.dictkeys_type, {
358-
"__iter__" => context.new_rustfunc(PyDictKeys::iter),
359-
});
360-
361-
extend_class!(context, &context.dictkeyiterator_type, {
362-
"__next__" => context.new_rustfunc(PyDictKeyIterator::next),
363-
"__iter__" => context.new_rustfunc(PyDictKeyIterator::iter),
364-
});
365-
366-
extend_class!(context, &context.dictvalues_type, {
367-
"__iter__" => context.new_rustfunc(PyDictValues::iter),
368-
});
369-
370-
extend_class!(context, &context.dictvalueiterator_type, {
371-
"__next__" => context.new_rustfunc(PyDictValueIterator::next),
372-
"__iter__" => context.new_rustfunc(PyDictValueIterator::iter),
373-
});
374-
375-
extend_class!(context, &context.dictitems_type, {
376-
"__iter__" => context.new_rustfunc(PyDictItems::iter),
377-
});
378-
379-
extend_class!(context, &context.dictitemiterator_type, {
380-
"__next__" => context.new_rustfunc(PyDictItemIterator::next),
381-
"__iter__" => context.new_rustfunc(PyDictItemIterator::iter),
382-
});
370+
PyDictKeys::extend_class(context, &context.dictkeys_type);
371+
PyDictKeyIterator::extend_class(context, &context.dictkeyiterator_type);
372+
PyDictValues::extend_class(context, &context.dictvalues_type);
373+
PyDictValueIterator::extend_class(context, &context.dictvalueiterator_type);
374+
PyDictItems::extend_class(context, &context.dictitems_type);
375+
PyDictItemIterator::extend_class(context, &context.dictitemiterator_type);
383376
}

0 commit comments

Comments
 (0)