Skip to content

Commit f335d60

Browse files
committed
add Py<PyStr>
1 parent 709a2c8 commit f335d60

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

vm/src/dictdatatype.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::common::{
1010
use crate::{
1111
builtins::{PyInt, PyStr, PyStrRef},
1212
convert::ToPyObject,
13-
AsObject, PyObject, PyObjectRef, PyRefExact, PyResult, VirtualMachine,
13+
AsObject, Py, PyObject, PyObjectRef, PyRefExact, PyResult, VirtualMachine,
1414
};
1515
use num_traits::ToPrimitive;
1616
use std::{fmt, mem::size_of, ops::ControlFlow};
@@ -735,6 +735,30 @@ impl DictKey for PyObject {
735735
}
736736
}
737737

738+
impl DictKey for Py<PyStr> {
739+
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
740+
Ok(self.hash(vm))
741+
}
742+
743+
fn key_is(&self, other: &PyObject) -> bool {
744+
self.is(other)
745+
}
746+
747+
fn key_eq(&self, vm: &VirtualMachine, other_key: &PyObject) -> PyResult<bool> {
748+
if self.is(other_key) {
749+
Ok(true)
750+
} else if let Some(pystr) = str_exact(other_key, vm) {
751+
Ok(pystr.as_str() == self.as_str())
752+
} else {
753+
vm.bool_eq(self.as_object(), other_key)
754+
}
755+
}
756+
757+
fn key_as_isize(&self, vm: &VirtualMachine) -> PyResult<isize> {
758+
self.as_object().key_as_isize(vm)
759+
}
760+
}
761+
738762
impl DictKey for PyStrRef {
739763
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
740764
Ok(self.hash(vm))

0 commit comments

Comments
 (0)