Skip to content

Commit 29073cf

Browse files
committed
clean up dict
1 parent cf82d6f commit 29073cf

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

vm/src/builtins/dict.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ impl PyDict {
203203
#[allow(clippy::len_without_is_empty)]
204204
#[pyclass(
205205
with(
206+
Py,
207+
PyRef,
206208
Constructor,
207209
Initializer,
208210
AsMapping,
@@ -270,32 +272,11 @@ impl PyDict {
270272
self.entries.clear()
271273
}
272274

273-
#[pymethod]
274-
fn keys(zelf: PyRef<Self>) -> PyDictKeys {
275-
PyDictKeys::new(zelf)
276-
}
277-
278-
#[pymethod]
279-
fn values(zelf: PyRef<Self>) -> PyDictValues {
280-
PyDictValues::new(zelf)
281-
}
282-
283-
#[pymethod]
284-
fn items(zelf: PyRef<Self>) -> PyDictItems {
285-
PyDictItems::new(zelf)
286-
}
287-
288275
#[pymethod(magic)]
289276
fn setitem(&self, key: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
290277
self.inner_setitem(&*key, value, vm)
291278
}
292279

293-
#[pymethod(magic)]
294-
#[cfg_attr(feature = "flame-it", flame("PyDictRef"))]
295-
fn getitem(zelf: PyRef<Self>, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
296-
zelf.inner_getitem(&*key, vm)
297-
}
298-
299280
#[pymethod]
300281
fn get(
301282
&self,
@@ -396,17 +377,44 @@ impl PyDict {
396377
Ok((key, value))
397378
}
398379

399-
#[pymethod(magic)]
400-
fn reversed(zelf: PyRef<Self>) -> PyDictReverseKeyIterator {
401-
PyDictReverseKeyIterator::new(zelf)
402-
}
403-
404380
#[pyclassmethod(magic)]
405381
fn class_getitem(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
406382
PyGenericAlias::new(cls, args, vm)
407383
}
408384
}
409385

386+
#[pyclass]
387+
impl Py<PyDict> {
388+
#[pymethod(magic)]
389+
#[cfg_attr(feature = "flame-it", flame("PyDictRef"))]
390+
fn getitem(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
391+
self.inner_getitem(&*key, vm)
392+
}
393+
}
394+
395+
#[pyclass]
396+
impl PyRef<PyDict> {
397+
#[pymethod]
398+
fn keys(self) -> PyDictKeys {
399+
PyDictKeys::new(self)
400+
}
401+
402+
#[pymethod]
403+
fn values(self) -> PyDictValues {
404+
PyDictValues::new(self)
405+
}
406+
407+
#[pymethod]
408+
fn items(self) -> PyDictItems {
409+
PyDictItems::new(self)
410+
}
411+
412+
#[pymethod(magic)]
413+
fn reversed(self) -> PyDictReverseKeyIterator {
414+
PyDictReverseKeyIterator::new(self)
415+
}
416+
}
417+
410418
impl Constructor for PyDict {
411419
type Args = FuncArgs;
412420

@@ -847,9 +855,9 @@ macro_rules! dict_view {
847855

848856
#[allow(clippy::redundant_closure_call)]
849857
#[pymethod(magic)]
850-
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyTupleRef {
858+
fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef {
851859
let iter = builtins_iter(vm).to_owned();
852-
let internal = zelf.internal.lock();
860+
let internal = self.internal.lock();
853861
let entries = match &internal.status {
854862
IterStatus::Active(dict) => dict
855863
.into_iter()
@@ -917,9 +925,9 @@ macro_rules! dict_view {
917925

918926
#[allow(clippy::redundant_closure_call)]
919927
#[pymethod(magic)]
920-
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyTupleRef {
928+
fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef {
921929
let iter = builtins_reversed(vm).to_owned();
922-
let internal = zelf.internal.lock();
930+
let internal = self.internal.lock();
923931
// TODO: entries must be reversed too
924932
let entries = match &internal.status {
925933
IterStatus::Active(dict) => dict
@@ -1240,7 +1248,7 @@ impl PyDictItems {
12401248
return Ok(false);
12411249
}
12421250
let value = needle.fast_getitem(1);
1243-
let found = PyDict::getitem(zelf.dict().clone(), key, vm)?;
1251+
let found = zelf.dict().getitem(key, vm)?;
12441252
vm.identical_or_equal(&found, &value)
12451253
}
12461254
#[pygetset]

0 commit comments

Comments
 (0)