Skip to content

Commit 3d9812e

Browse files
committed
Remove remaining borrow_value() uses
1 parent 46e94b5 commit 3d9812e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+254
-285
lines changed

src/lib.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! mod mymod {
1919
//! use rustpython_vm::builtins::PyStrRef;
2020
//TODO: use rustpython_vm::prelude::*;
21-
//! use rustpython_vm::common::borrow::BorrowValue;
2221
//!
2322
//! #[pyfunction]
2423
//! fn do_thing(x: i32) -> i32 {
@@ -28,7 +27,7 @@
2827
//! #[pyfunction]
2928
//! fn other_thing(s: PyStrRef) -> (String, usize) {
3029
//! let new_string = format!("hello from rust, {}!", s);
31-
//! let prev_len = s.borrow_value().len();
30+
//! let prev_len = s.as_str().len();
3231
//! (new_string, prev_len)
3332
//! }
3433
//! }
@@ -47,8 +46,7 @@ extern crate log;
4746
use clap::{App, AppSettings, Arg, ArgMatches};
4847
use rustpython_vm::{
4948
builtins::PyInt, compile, exceptions::print_exception, match_class, scope::Scope, sysmodule,
50-
BorrowValue, InitParameter, Interpreter, ItemProtocol, PyResult, PySettings, TypeProtocol,
51-
VirtualMachine,
49+
InitParameter, Interpreter, ItemProtocol, PyResult, PySettings, TypeProtocol, VirtualMachine,
5250
};
5351

5452
use std::convert::TryInto;
@@ -117,12 +115,12 @@ where
117115
Ok(()) => 0,
118116
Err(err) if err.isinstance(&vm.ctx.exceptions.system_exit) => {
119117
let args = err.args();
120-
match args.borrow_value() {
118+
match args.as_slice() {
121119
[] => 0,
122120
[arg] => match_class!(match arg {
123121
ref i @ PyInt => {
124122
use num_traits::cast::ToPrimitive;
125-
i.borrow_value().to_i32().unwrap_or(0)
123+
i.as_bigint().to_i32().unwrap_or(0)
126124
}
127125
arg => {
128126
if vm.is_none(arg) {
@@ -555,12 +553,7 @@ __import__("io").TextIOWrapper(
555553
.downcast()
556554
.expect("TextIOWrapper.read() should return str");
557555
eprintln!("running get-pip.py...");
558-
_run_string(
559-
vm,
560-
scope,
561-
getpip_code.borrow_value(),
562-
"get-pip.py".to_owned(),
563-
)?;
556+
_run_string(vm, scope, getpip_code.as_str(), "get-pip.py".to_owned())?;
564557
} else if let Some(filename) = matches.value_of("script") {
565558
run_script(&vm, scope.clone(), filename)?;
566559
if matches.is_present("inspect") {

src/shell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustpython_vm::{
66
compile::{self, CompileError, CompileErrorType},
77
exceptions::{print_exception, PyBaseExceptionRef},
88
scope::Scope,
9-
BorrowValue, PyResult, TypeProtocol, VirtualMachine,
9+
PyResult, TypeProtocol, VirtualMachine,
1010
};
1111

1212
enum ShellExecResult {
@@ -59,7 +59,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
5959
.get_attribute(vm.sys_module.clone(), prompt_name)
6060
.and_then(|prompt| vm.to_str(&prompt));
6161
let prompt = match prompt {
62-
Ok(ref s) => s.borrow_value(),
62+
Ok(ref s) => s.as_str(),
6363
Err(_) => "",
6464
};
6565
let result = match repl.readline(prompt) {

src/shell/helper.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustpython_vm::builtins::{PyDictRef, PyStrRef};
22
use rustpython_vm::VirtualMachine;
3-
use rustpython_vm::{BorrowValue, PyIterable, PyResult, TryFromObject};
3+
use rustpython_vm::{PyIterable, PyResult, TryFromObject};
44

55
pub struct ShellHelper<'vm> {
66
vm: &'vm VirtualMachine,
@@ -104,7 +104,7 @@ impl<'vm> ShellHelper<'vm> {
104104
.filter(|res| {
105105
res.as_ref()
106106
.ok()
107-
.map_or(true, |s| s.borrow_value().starts_with(word_start))
107+
.map_or(true, |s| s.as_str().starts_with(word_start))
108108
})
109109
.collect::<Result<Vec<_>, _>>()
110110
.ok()?;
@@ -117,7 +117,7 @@ impl<'vm> ShellHelper<'vm> {
117117
let no_underscore = all_completions
118118
.iter()
119119
.cloned()
120-
.filter(|s| !s.borrow_value().starts_with('_'))
120+
.filter(|s| !s.as_str().starts_with('_'))
121121
.collect::<Vec<_>>();
122122

123123
// if there are only completions that start with a '_', give them all of the
@@ -130,13 +130,13 @@ impl<'vm> ShellHelper<'vm> {
130130
};
131131

132132
// sort the completions alphabetically
133-
completions.sort_by(|a, b| std::cmp::Ord::cmp(a.borrow_value(), b.borrow_value()));
133+
completions.sort_by(|a, b| std::cmp::Ord::cmp(a.as_str(), b.as_str()));
134134

135135
Some((
136136
startpos,
137137
completions
138138
.into_iter()
139-
.map(|s| s.borrow_value().to_owned())
139+
.map(|s| s.as_str().to_owned())
140140
.collect(),
141141
))
142142
}

vm/src/builtins/dict.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use crate::iterator;
1212
use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhashable};
1313
use crate::vm::{ReprGuard, VirtualMachine};
1414
use crate::{
15-
BorrowValue, IdProtocol, IntoPyObject, ItemProtocol, PyArithmaticValue::*, PyAttributes,
16-
PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
17-
TryFromObject, TypeProtocol,
15+
IdProtocol, IntoPyObject, ItemProtocol, PyArithmaticValue::*, PyAttributes, PyClassImpl,
16+
PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
17+
TypeProtocol,
1818
};
1919

2020
pub type DictContentType = dictdatatype::Dict;
@@ -205,11 +205,7 @@ impl PyDict {
205205
for (key, value) in zelf {
206206
let key_repr = vm.to_repr(&key)?;
207207
let value_repr = vm.to_repr(&value)?;
208-
str_parts.push(format!(
209-
"{}: {}",
210-
key_repr.borrow_value(),
211-
value_repr.borrow_value()
212-
));
208+
str_parts.push(format!("{}: {}", key_repr, value_repr));
213209
}
214210

215211
format!("{{{}}}", str_parts.join(", "))
@@ -622,7 +618,7 @@ macro_rules! dict_iterator {
622618
let mut str_parts = vec![];
623619
for (key, value) in zelf.dict.clone() {
624620
let s = vm.to_repr(&($result_fn)(vm, key, value))?;
625-
str_parts.push(s.borrow_value().to_owned());
621+
str_parts.push(s.as_str().to_owned());
626622
}
627623
format!("{}([{}])", $class_name, str_parts.join(", "))
628624
} else {

vm/src/builtins/function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::slots::{Callable, Comparable, PyComparisonOp, SlotDescriptor, SlotGet
1919
use crate::IntoPyObject;
2020
use crate::VirtualMachine;
2121
use crate::{
22-
BorrowValue, IdProtocol, ItemProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
23-
PyRef, PyResult, PyValue, TypeProtocol,
22+
IdProtocol, ItemProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef,
23+
PyResult, PyValue, TypeProtocol,
2424
};
2525
use itertools::Itertools;
2626
#[cfg(feature = "jit")]
@@ -277,7 +277,7 @@ impl PyFunction {
277277
code.clone(),
278278
Scope::new(Some(locals), self.globals.clone()),
279279
vm.builtins.dict().unwrap(),
280-
self.closure.as_ref().map_or(&[], |c| c.borrow_value()),
280+
self.closure.as_ref().map_or(&[], |c| c.as_slice()),
281281
vm,
282282
)
283283
.into_ref(vm);

vm/src/builtins/function/jitfunc.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::exceptions::PyBaseExceptionRef;
66
use crate::function::FuncArgs;
77
use crate::VirtualMachine;
88
use crate::{
9-
BorrowValue, IdProtocol, IntoPyObject, ItemProtocol, PyObjectRef, PyResult, TryFromObject,
10-
TypeProtocol,
9+
IdProtocol, IntoPyObject, ItemProtocol, PyObjectRef, PyResult, TryFromObject, TypeProtocol,
1110
};
1211
use num_traits::ToPrimitive;
1312
use rustpython_jit::{AbiValue, Args, CompiledCode, JitArgumentError, JitType};
@@ -95,11 +94,11 @@ pub fn get_jit_arg_types(func: &PyFunctionRef, vm: &VirtualMachine) -> PyResult<
9594
let mut arg_types = Vec::new();
9695

9796
for arg in arg_names.args {
98-
arg_types.push(get_jit_arg_type(&dict, arg.borrow_value(), vm)?);
97+
arg_types.push(get_jit_arg_type(&dict, arg.as_str(), vm)?);
9998
}
10099

101100
for arg in arg_names.kwonlyargs {
102-
arg_types.push(get_jit_arg_type(&dict, arg.borrow_value(), vm)?);
101+
arg_types.push(get_jit_arg_type(&dict, arg.as_str(), vm)?);
103102
}
104103

105104
Ok(arg_types)
@@ -152,7 +151,7 @@ pub(crate) fn get_jit_args<'a>(
152151
// Handle keyword arguments
153152
for (name, value) in &func_args.kwargs {
154153
let arg_pos =
155-
|args: &[PyStrRef], name: &str| args.iter().position(|arg| arg.borrow_value() == name);
154+
|args: &[PyStrRef], name: &str| args.iter().position(|arg| arg.as_str() == name);
156155
if let Some(arg_idx) = arg_pos(arg_names.args, name) {
157156
if jit_args.is_set(arg_idx) {
158157
return Err(ArgsError::ArgPassedMultipleTimes);
@@ -173,7 +172,7 @@ pub(crate) fn get_jit_args<'a>(
173172

174173
// fill in positional defaults
175174
if let Some(defaults) = defaults {
176-
let defaults = defaults.borrow_value();
175+
let defaults = defaults.as_slice();
177176
for (i, default) in defaults.iter().enumerate() {
178177
let arg_idx = i + func.code.arg_count - defaults.len();
179178
if !jit_args.is_set(arg_idx) {

vm/src/builtins/int.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ use crate::slots::{Comparable, Hashable, PyComparisonOp};
1919
use crate::VirtualMachine;
2020
use crate::{bytesinner::PyBytesInner, byteslike::try_bytes_like};
2121
use crate::{
22-
BorrowValue, IdProtocol, IntoPyObject, IntoPyResult, PyArithmaticValue, PyClassImpl,
23-
PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
24-
TypeProtocol,
22+
IdProtocol, IntoPyObject, IntoPyResult, PyArithmaticValue, PyClassImpl, PyComparisonValue,
23+
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
2524
};
2625
use rustpython_common::hash;
2726

@@ -53,7 +52,7 @@ impl fmt::Display for PyInt {
5352

5453
pub type PyIntRef = PyRef<PyInt>;
5554

56-
impl<'a> BorrowValue<'a> for PyInt {
55+
impl<'a> rustpython_common::borrow::BorrowValue<'a> for PyInt {
5756
type Borrowed = &'a BigInt;
5857

5958
fn borrow_value(&'a self) -> Self::Borrowed {

vm/src/builtins/list.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhas
1717
use crate::utils::Either;
1818
use crate::vm::{ReprGuard, VirtualMachine};
1919
use crate::{
20-
BorrowValue, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef,
21-
PyResult, PyValue, TryFromObject, TypeProtocol,
20+
PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
21+
TryFromObject, TypeProtocol,
2222
};
2323

2424
/// Built-in mutable sequence.
@@ -58,7 +58,7 @@ impl PyValue for PyList {
5858
}
5959
}
6060

61-
impl<'a> BorrowValue<'a> for PyList {
61+
impl<'a> rustpython_common::borrow::BorrowValue<'a> for PyList {
6262
type Borrowed = PyMappedRwLockReadGuard<'a, [PyObjectRef]>;
6363

6464
fn borrow_value(&'a self) -> Self::Borrowed {
@@ -110,8 +110,8 @@ impl PyList {
110110
#[pymethod(name = "__add__")]
111111
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
112112
if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
113-
let mut elements = self.borrow_value().to_vec();
114-
elements.extend(other.borrow_value().iter().cloned());
113+
let mut elements = self.borrow_list().to_vec();
114+
elements.extend(other.borrow_list().iter().cloned());
115115
Ok(vm.ctx.new_list(elements))
116116
} else {
117117
Err(vm.new_type_error(format!(
@@ -135,7 +135,7 @@ impl PyList {
135135

136136
#[pymethod(name = "__bool__")]
137137
fn bool(&self) -> bool {
138-
!self.borrow_value().is_empty()
138+
!self.borrow_list().is_empty()
139139
}
140140

141141
#[pymethod]
@@ -145,12 +145,12 @@ impl PyList {
145145

146146
#[pymethod]
147147
fn copy(&self, vm: &VirtualMachine) -> PyObjectRef {
148-
vm.ctx.new_list(self.borrow_value().to_vec())
148+
vm.ctx.new_list(self.borrow_list().to_vec())
149149
}
150150

151151
#[pymethod(name = "__len__")]
152152
fn len(&self) -> usize {
153-
self.borrow_value().len()
153+
self.borrow_list().len()
154154
}
155155

156156
#[pymethod(name = "__sizeof__")]
@@ -165,7 +165,7 @@ impl PyList {
165165

166166
#[pymethod(name = "__reversed__")]
167167
fn reversed(zelf: PyRef<Self>) -> PyListReverseIterator {
168-
let final_position = zelf.borrow_value().len();
168+
let final_position = zelf.borrow_list().len();
169169
PyListReverseIterator {
170170
position: AtomicCell::new(final_position as isize),
171171
list: zelf,
@@ -174,7 +174,7 @@ impl PyList {
174174

175175
#[pymethod(name = "__getitem__")]
176176
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
177-
let result = match zelf.borrow_value().get_item(vm, needle, "list")? {
177+
let result = match zelf.borrow_list().get_item(vm, needle, "list")? {
178178
Either::A(obj) => obj,
179179
Either::B(vec) => vm.ctx.new_list(vec),
180180
};
@@ -310,8 +310,7 @@ impl PyList {
310310
return Ok(index);
311311
}
312312
}
313-
let needle_str = vm.to_str(&needle)?;
314-
Err(vm.new_value_error(format!("'{}' is not in list", needle_str.borrow_value())))
313+
Err(vm.new_value_error(format!("'{}' is not in list", vm.to_str(&needle)?)))
315314
}
316315

317316
#[pymethod]
@@ -344,8 +343,7 @@ impl PyList {
344343
// defer delete out of borrow
345344
Ok(self.borrow_list_mut().remove(index))
346345
} else {
347-
let needle_str = vm.to_str(&needle)?;
348-
Err(vm.new_value_error(format!("'{}' is not in list", needle_str.borrow_value())))
346+
Err(vm.new_value_error(format!("'{}' is not in list", vm.to_str(&needle)?)))
349347
}
350348
.map(drop)
351349
}

vm/src/builtins/object.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use crate::slots::PyComparisonOp;
1010
use crate::utils::Either;
1111
use crate::vm::VirtualMachine;
1212
use crate::{
13-
BorrowValue, IdProtocol, ItemProtocol, PyArithmaticValue, PyAttributes, PyClassImpl,
14-
PyComparisonValue, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject,
15-
TypeProtocol,
13+
IdProtocol, ItemProtocol, PyArithmaticValue, PyAttributes, PyClassImpl, PyComparisonValue,
14+
PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1615
};
1716

1817
/// The most base type
@@ -305,7 +304,7 @@ pub(crate) fn setattr(
305304
Err(vm.new_attribute_error(format!(
306305
"'{}' object has no attribute '{}'",
307306
obj.class().name,
308-
attr_name.borrow_value()
307+
attr_name,
309308
)))
310309
}
311310
}

0 commit comments

Comments
 (0)