Reduce &String usage

This commit is contained in:
Jeong YunWon committed 2020-01-15 11:51:29 +09:00
1 parent 582e5dfca9
commit e5b91c25a8
3 files changed
+18 -31

No files matched your search

+1 -1
View File
@@ -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![] }))
}
+5 -18
View File
@@ -369,29 +369,16 @@ impl DictKey for &str {
}
impl DictKey for &String {
fn do_hash(self, _vm: &VirtualMachine) -> PyResult<HashValue> {
// 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<HashValue> {
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<bool> {
if let Some(py_str_value) = other_key.payload::<PyString>() {
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)
}
}
+12 -12
View File
@@ -690,12 +690,6 @@ where
}
}
impl<T> IntoPyObject for PyRef<T> {
fn into_pyobject(self, _vm: &VirtualMachine) -> PyResult {
Ok(self.obj)
}
}
impl<'a, T: PyValue> From<&'a PyRef<T>> for &'a PyObjectRef {
fn from(obj: &'a PyRef<T>) -> 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<T>(&self, other: &T) -> bool
@@ -1017,6 +1005,18 @@ pub trait IntoPyObject {
fn into_pyobject(self, vm: &VirtualMachine) -> PyResult;
}
impl<T> IntoPyObject for PyRef<T> {
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)