mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Refactor DictKey - remove PyStrRef
This commit is contained in:
1 parent
8a2cd2ba08
commit
c6b33aab02
7 files changed
+24
-88
No files matched your search
@@ -242,7 +242,7 @@ impl PyFunction {
|
||||
.filter(|(slot, _)| slot.is_none())
|
||||
{
|
||||
if let Some(defaults) = &get_defaults!().1 {
|
||||
if let Some(default) = defaults.get_item_opt(&*kwarg, vm)? {
|
||||
if let Some(default) = defaults.get_item_opt(&**kwarg, vm)? {
|
||||
*slot = Some(default);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -188,7 +188,7 @@ pub(crate) fn get_jit_args<'a>(
|
||||
let arg_idx = i + func.code.arg_count;
|
||||
if !jit_args.is_set(arg_idx) {
|
||||
let default = kw_only_defaults
|
||||
.get_item(&*name, vm)
|
||||
.get_item(&**name, vm)
|
||||
.map_err(|_| ArgsError::NotAllArgsPassed)
|
||||
.and_then(|obj| get_jit_value(vm, &obj))?;
|
||||
jit_args.set(arg_idx, default)?;
|
||||
|
||||
+1
-64
@@ -738,36 +738,6 @@ impl DictKey for Py<PyStr> {
|
||||
}
|
||||
}
|
||||
|
||||
impl DictKey for PyStrRef {
|
||||
type Owned = Self;
|
||||
#[inline]
|
||||
fn _to_owned(&self, _vm: &VirtualMachine) -> Self::Owned {
|
||||
self.clone()
|
||||
}
|
||||
|
||||
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
|
||||
Ok(self.hash(vm))
|
||||
}
|
||||
|
||||
fn key_is(&self, other: &PyObject) -> bool {
|
||||
self.is(other)
|
||||
}
|
||||
|
||||
fn key_eq(&self, vm: &VirtualMachine, other_key: &PyObject) -> PyResult<bool> {
|
||||
if self.is(other_key) {
|
||||
Ok(true)
|
||||
} else if let Some(pystr) = str_exact(other_key, vm) {
|
||||
Ok(pystr.as_str() == self.as_str())
|
||||
} else {
|
||||
vm.bool_eq(self.as_object(), other_key)
|
||||
}
|
||||
}
|
||||
|
||||
fn key_as_isize(&self, vm: &VirtualMachine) -> PyResult<isize> {
|
||||
self.as_object().key_as_isize(vm)
|
||||
}
|
||||
}
|
||||
|
||||
impl DictKey for PyRefExact<PyStr> {
|
||||
type Owned = Self;
|
||||
#[inline]
|
||||
@@ -793,39 +763,6 @@ impl DictKey for PyRefExact<PyStr> {
|
||||
|
||||
/// Implement trait for the str type, so that we can use strings
|
||||
/// to index dictionaries.
|
||||
impl DictKey for &str {
|
||||
type Owned = String;
|
||||
#[inline]
|
||||
fn _to_owned(&self, _vm: &VirtualMachine) -> Self::Owned {
|
||||
(**self).to_owned()
|
||||
}
|
||||
|
||||
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
|
||||
// follow a similar route as the hashing of PyStrRef
|
||||
Ok(vm.state.hash_secret.hash_str(*self))
|
||||
}
|
||||
|
||||
fn key_is(&self, _other: &PyObject) -> bool {
|
||||
// No matter who the other pyobject is, we are never the same thing, since
|
||||
// we are a str, not a pyobject.
|
||||
false
|
||||
}
|
||||
|
||||
fn key_eq(&self, vm: &VirtualMachine, other_key: &PyObject) -> PyResult<bool> {
|
||||
if let Some(pystr) = str_exact(other_key, vm) {
|
||||
Ok(pystr.as_str() == *self)
|
||||
} else {
|
||||
// Fall back to PyObjectRef implementation.
|
||||
let s = vm.ctx.new_str(*self);
|
||||
s.key_eq(vm, other_key)
|
||||
}
|
||||
}
|
||||
|
||||
fn key_as_isize(&self, vm: &VirtualMachine) -> PyResult<isize> {
|
||||
Err(vm.new_type_error("'str' object cannot be interpreted as an integer".to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
impl DictKey for str {
|
||||
type Owned = String;
|
||||
#[inline]
|
||||
@@ -964,7 +901,7 @@ mod tests {
|
||||
assert_eq!(true, dict.contains(&vm, &*key1).unwrap());
|
||||
assert_eq!(true, dict.contains(&vm, "x").unwrap());
|
||||
|
||||
let val = dict.get(&vm, &"x").unwrap().unwrap();
|
||||
let val = dict.get(&vm, "x").unwrap().unwrap();
|
||||
vm.bool_eq(&val, &value2)
|
||||
.expect("retrieved value must be equal to inserted value.");
|
||||
})
|
||||
|
||||
+1
-1
@@ -572,7 +572,7 @@ impl ExecutingFrame<'_> {
|
||||
}
|
||||
bytecode::Instruction::DeleteGlobal(idx) => {
|
||||
let name = &self.code.names[*idx as usize];
|
||||
match self.globals.del_item(&*name, vm) {
|
||||
match self.globals.del_item(&**name, vm) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.fast_isinstance(&vm.ctx.exceptions.key_error) => {
|
||||
return Err(
|
||||
|
||||
+6
-2
@@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
builtins::{PyStr, PyTypeRef},
|
||||
common::lock::PyRwLock,
|
||||
PyRef, PyRefExact,
|
||||
Py, PyRef, PyRefExact,
|
||||
};
|
||||
use std::ops::Deref;
|
||||
|
||||
@@ -88,12 +88,14 @@ mod sealed {
|
||||
}
|
||||
|
||||
/// A sealed marker trait for `DictKey` types that always become an exact instance of `str`
|
||||
pub trait Internable: sealed::SealedInternable + crate::dictdatatype::DictKey {
|
||||
pub trait Internable: sealed::SealedInternable + AsRef<Self::Key> {
|
||||
type Key: crate::dictdatatype::DictKey + ?Sized;
|
||||
fn as_str(&self) -> &str;
|
||||
fn into_pyref(self, str_type: PyTypeRef) -> PyRefExact<PyStr>;
|
||||
}
|
||||
|
||||
impl Internable for String {
|
||||
type Key = str;
|
||||
fn as_str(&self) -> &str {
|
||||
String::as_str(self)
|
||||
}
|
||||
@@ -104,6 +106,7 @@ impl Internable for String {
|
||||
}
|
||||
|
||||
impl Internable for &str {
|
||||
type Key = str;
|
||||
fn as_str(&self) -> &str {
|
||||
self
|
||||
}
|
||||
@@ -113,6 +116,7 @@ impl Internable for &str {
|
||||
}
|
||||
|
||||
impl Internable for PyRefExact<PyStr> {
|
||||
type Key = Py<PyStr>;
|
||||
fn as_str(&self) -> &str {
|
||||
self.deref().as_str()
|
||||
}
|
||||
|
||||
+12
-17
@@ -1,9 +1,4 @@
|
||||
use crate::{
|
||||
builtins::{pystr::IntoPyStrRef, PyDictRef, PyStrRef},
|
||||
convert::ToPyObject,
|
||||
function::ArgMapping,
|
||||
VirtualMachine,
|
||||
};
|
||||
use crate::{builtins::PyDictRef, function::ArgMapping, VirtualMachine};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -140,14 +135,14 @@ impl Scope {
|
||||
// }
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
pub trait Sealed {}
|
||||
impl Sealed for &str {}
|
||||
impl Sealed for super::PyStrRef {}
|
||||
}
|
||||
pub trait PyName:
|
||||
sealed::Sealed + crate::dictdatatype::DictKey + Clone + ToPyObject + IntoPyStrRef
|
||||
{
|
||||
}
|
||||
impl PyName for &str {}
|
||||
impl PyName for PyStrRef {}
|
||||
// mod sealed {
|
||||
// pub trait Sealed {}
|
||||
// impl Sealed for &str {}
|
||||
// impl Sealed for super::PyStrRef {}
|
||||
// }
|
||||
// pub trait PyName:
|
||||
// sealed::Sealed + crate::dictdatatype::DictKey + Clone + ToPyObject + IntoPyStrRef
|
||||
// {
|
||||
// }
|
||||
// impl PyName for str {}
|
||||
// impl PyName for Py<PyStr> {}
|
||||
@@ -371,9 +371,9 @@ pub(crate) mod _thread {
|
||||
} else {
|
||||
let dict = zelf.ldict(vm);
|
||||
if let Some(value) = value {
|
||||
dict.set_item(&attr, value, vm)?;
|
||||
dict.set_item(&*attr, value, vm)?;
|
||||
} else {
|
||||
dict.del_item(&attr, vm)?;
|
||||
dict.del_item(&*attr, vm)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user