Skip to content

Commit c6b33aa

Browse files
committed
Refactor DictKey - remove PyStrRef
1 parent 8a2cd2b commit c6b33aa

File tree

7 files changed

+24
-88
lines changed

7 files changed

+24
-88
lines changed

vm/src/builtins/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl PyFunction {
242242
.filter(|(slot, _)| slot.is_none())
243243
{
244244
if let Some(defaults) = &get_defaults!().1 {
245-
if let Some(default) = defaults.get_item_opt(&*kwarg, vm)? {
245+
if let Some(default) = defaults.get_item_opt(&**kwarg, vm)? {
246246
*slot = Some(default);
247247
continue;
248248
}

vm/src/builtins/function/jitfunc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub(crate) fn get_jit_args<'a>(
188188
let arg_idx = i + func.code.arg_count;
189189
if !jit_args.is_set(arg_idx) {
190190
let default = kw_only_defaults
191-
.get_item(&*name, vm)
191+
.get_item(&**name, vm)
192192
.map_err(|_| ArgsError::NotAllArgsPassed)
193193
.and_then(|obj| get_jit_value(vm, &obj))?;
194194
jit_args.set(arg_idx, default)?;

vm/src/dictdatatype.rs

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -738,36 +738,6 @@ impl DictKey for Py<PyStr> {
738738
}
739739
}
740740

741-
impl DictKey for PyStrRef {
742-
type Owned = Self;
743-
#[inline]
744-
fn _to_owned(&self, _vm: &VirtualMachine) -> Self::Owned {
745-
self.clone()
746-
}
747-
748-
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
749-
Ok(self.hash(vm))
750-
}
751-
752-
fn key_is(&self, other: &PyObject) -> bool {
753-
self.is(other)
754-
}
755-
756-
fn key_eq(&self, vm: &VirtualMachine, other_key: &PyObject) -> PyResult<bool> {
757-
if self.is(other_key) {
758-
Ok(true)
759-
} else if let Some(pystr) = str_exact(other_key, vm) {
760-
Ok(pystr.as_str() == self.as_str())
761-
} else {
762-
vm.bool_eq(self.as_object(), other_key)
763-
}
764-
}
765-
766-
fn key_as_isize(&self, vm: &VirtualMachine) -> PyResult<isize> {
767-
self.as_object().key_as_isize(vm)
768-
}
769-
}
770-
771741
impl DictKey for PyRefExact<PyStr> {
772742
type Owned = Self;
773743
#[inline]
@@ -793,39 +763,6 @@ impl DictKey for PyRefExact<PyStr> {
793763

794764
/// Implement trait for the str type, so that we can use strings
795765
/// to index dictionaries.
796-
impl DictKey for &str {
797-
type Owned = String;
798-
#[inline]
799-
fn _to_owned(&self, _vm: &VirtualMachine) -> Self::Owned {
800-
(**self).to_owned()
801-
}
802-
803-
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
804-
// follow a similar route as the hashing of PyStrRef
805-
Ok(vm.state.hash_secret.hash_str(*self))
806-
}
807-
808-
fn key_is(&self, _other: &PyObject) -> bool {
809-
// No matter who the other pyobject is, we are never the same thing, since
810-
// we are a str, not a pyobject.
811-
false
812-
}
813-
814-
fn key_eq(&self, vm: &VirtualMachine, other_key: &PyObject) -> PyResult<bool> {
815-
if let Some(pystr) = str_exact(other_key, vm) {
816-
Ok(pystr.as_str() == *self)
817-
} else {
818-
// Fall back to PyObjectRef implementation.
819-
let s = vm.ctx.new_str(*self);
820-
s.key_eq(vm, other_key)
821-
}
822-
}
823-
824-
fn key_as_isize(&self, vm: &VirtualMachine) -> PyResult<isize> {
825-
Err(vm.new_type_error("'str' object cannot be interpreted as an integer".to_owned()))
826-
}
827-
}
828-
829766
impl DictKey for str {
830767
type Owned = String;
831768
#[inline]
@@ -964,7 +901,7 @@ mod tests {
964901
assert_eq!(true, dict.contains(&vm, &*key1).unwrap());
965902
assert_eq!(true, dict.contains(&vm, "x").unwrap());
966903

967-
let val = dict.get(&vm, &"x").unwrap().unwrap();
904+
let val = dict.get(&vm, "x").unwrap().unwrap();
968905
vm.bool_eq(&val, &value2)
969906
.expect("retrieved value must be equal to inserted value.");
970907
})

vm/src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ impl ExecutingFrame<'_> {
572572
}
573573
bytecode::Instruction::DeleteGlobal(idx) => {
574574
let name = &self.code.names[*idx as usize];
575-
match self.globals.del_item(&*name, vm) {
575+
match self.globals.del_item(&**name, vm) {
576576
Ok(()) => {}
577577
Err(e) if e.fast_isinstance(&vm.ctx.exceptions.key_error) => {
578578
return Err(

vm/src/intern.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
builtins::{PyStr, PyTypeRef},
33
common::lock::PyRwLock,
4-
PyRef, PyRefExact,
4+
Py, PyRef, PyRefExact,
55
};
66
use std::ops::Deref;
77

@@ -88,12 +88,14 @@ mod sealed {
8888
}
8989

9090
/// A sealed marker trait for `DictKey` types that always become an exact instance of `str`
91-
pub trait Internable: sealed::SealedInternable + crate::dictdatatype::DictKey {
91+
pub trait Internable: sealed::SealedInternable + AsRef<Self::Key> {
92+
type Key: crate::dictdatatype::DictKey + ?Sized;
9293
fn as_str(&self) -> &str;
9394
fn into_pyref(self, str_type: PyTypeRef) -> PyRefExact<PyStr>;
9495
}
9596

9697
impl Internable for String {
98+
type Key = str;
9799
fn as_str(&self) -> &str {
98100
String::as_str(self)
99101
}
@@ -104,6 +106,7 @@ impl Internable for String {
104106
}
105107

106108
impl Internable for &str {
109+
type Key = str;
107110
fn as_str(&self) -> &str {
108111
self
109112
}
@@ -113,6 +116,7 @@ impl Internable for &str {
113116
}
114117

115118
impl Internable for PyRefExact<PyStr> {
119+
type Key = Py<PyStr>;
116120
fn as_str(&self) -> &str {
117121
self.deref().as_str()
118122
}

vm/src/scope.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use crate::{
2-
builtins::{pystr::IntoPyStrRef, PyDictRef, PyStrRef},
3-
convert::ToPyObject,
4-
function::ArgMapping,
5-
VirtualMachine,
6-
};
1+
use crate::{builtins::PyDictRef, function::ArgMapping, VirtualMachine};
72
use std::fmt;
83

94
#[derive(Clone)]
@@ -140,14 +135,14 @@ impl Scope {
140135
// }
141136
}
142137

143-
mod sealed {
144-
pub trait Sealed {}
145-
impl Sealed for &str {}
146-
impl Sealed for super::PyStrRef {}
147-
}
148-
pub trait PyName:
149-
sealed::Sealed + crate::dictdatatype::DictKey + Clone + ToPyObject + IntoPyStrRef
150-
{
151-
}
152-
impl PyName for &str {}
153-
impl PyName for PyStrRef {}
138+
// mod sealed {
139+
// pub trait Sealed {}
140+
// impl Sealed for &str {}
141+
// impl Sealed for super::PyStrRef {}
142+
// }
143+
// pub trait PyName:
144+
// sealed::Sealed + crate::dictdatatype::DictKey + Clone + ToPyObject + IntoPyStrRef
145+
// {
146+
// }
147+
// impl PyName for str {}
148+
// impl PyName for Py<PyStr> {}

vm/src/stdlib/thread.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ pub(crate) mod _thread {
371371
} else {
372372
let dict = zelf.ldict(vm);
373373
if let Some(value) = value {
374-
dict.set_item(&attr, value, vm)?;
374+
dict.set_item(&*attr, value, vm)?;
375375
} else {
376-
dict.del_item(&attr, vm)?;
376+
dict.del_item(&*attr, vm)?;
377377
}
378378
Ok(())
379379
}

0 commit comments

Comments
 (0)