From e5b91c25a86f5e54e72edc8b49ca85e71fec46e7 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 11 Jan 2020 02:22:24 +0900 Subject: [PATCH] Reduce &String usage --- parser/src/parser.rs | 2 +- vm/src/dictdatatype.rs | 23 +++++------------------ vm/src/pyobject.rs | 24 ++++++++++++------------ 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 7a2ddd248..0f889c5af 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -145,7 +145,7 @@ mod tests { #[test] fn test_parse_empty() { - let parse_ast = parse_program(&String::from("")); + let parse_ast = parse_program(""); assert_eq!(parse_ast, Ok(ast::Program { statements: vec![] })) } diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 2d0b93582..279a41f97 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -369,29 +369,16 @@ impl DictKey for &str { } impl DictKey for &String { - fn do_hash(self, _vm: &VirtualMachine) -> PyResult { - // follow a similar route as the hashing of PyStringRef - let raw_hash = pyhash::hash_value(self).to_bigint().unwrap(); - let raw_hash = pyhash::hash_bigint(&raw_hash); - let mut hasher = DefaultHasher::new(); - raw_hash.hash(&mut hasher); - Ok(hasher.finish() as HashValue) + fn do_hash(self, vm: &VirtualMachine) -> PyResult { + self.as_str().do_hash(vm) } - fn do_is(self, _other: &PyObjectRef) -> bool { - // No matter who the other pyobject is, we are never the same thing, since - // we are a str, not a pyobject. - false + fn do_is(self, other: &PyObjectRef) -> bool { + self.as_str().do_is(other) } fn do_eq(self, vm: &VirtualMachine, other_key: &PyObjectRef) -> PyResult { - if let Some(py_str_value) = other_key.payload::() { - Ok(py_str_value.as_str() == self) - } else { - // Fall back to PyString implementation. - let s = vm.new_str(self.to_string()); - s.do_eq(vm, other_key) - } + self.as_str().do_eq(vm, other_key) } } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 9f38e4328..a02d162c3 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -690,12 +690,6 @@ where } } -impl IntoPyObject for PyRef { - fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult { - Ok(self.obj) - } -} - impl<'a, T: PyValue> From<&'a PyRef> for &'a PyObjectRef { fn from(obj: &'a PyRef) -> Self { obj.as_object() @@ -745,12 +739,6 @@ impl TryFromObject for PyCallable { } } -impl IntoPyObject for PyCallable { - fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult { - Ok(self.into_object()) - } -} - pub trait IdProtocol { fn get_id(&self) -> usize; fn is(&self, other: &T) -> bool @@ -1017,6 +1005,18 @@ pub trait IntoPyObject { fn into_pyobject(self, vm: &VirtualMachine) -> PyResult; } +impl IntoPyObject for PyRef { + fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult { + Ok(self.obj) + } +} + +impl IntoPyObject for PyCallable { + fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult { + Ok(self.into_object()) + } +} + impl IntoPyObject for PyObjectRef { fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult { Ok(self)