Skip to content

Commit 23910f7

Browse files
committed
Accept slices to function calls
Instead of allocated types, per [0]. [0] https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#ptr_arg
1 parent 6388891 commit 23910f7

File tree

9 files changed

+27
-27
lines changed

9 files changed

+27
-27
lines changed

parser/src/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn parse(filename: &Path) -> Result<ast::Program, String> {
4040
}
4141
}
4242

43-
pub fn parse_program(source: &String) -> Result<ast::Program, String> {
43+
pub fn parse_program(source: &str) -> Result<ast::Program, String> {
4444
let lxr = lexer::Lexer::new(&source);
4545
match python::ProgramParser::new().parse(lxr) {
4646
Err(lalrpop_util::ParseError::UnrecognizedToken {
@@ -52,15 +52,15 @@ pub fn parse_program(source: &String) -> Result<ast::Program, String> {
5252
}
5353
}
5454

55-
pub fn parse_statement(source: &String) -> Result<ast::LocatedStatement, String> {
55+
pub fn parse_statement(source: &str) -> Result<ast::LocatedStatement, String> {
5656
let lxr = lexer::Lexer::new(&source);
5757
match python::StatementParser::new().parse(lxr) {
5858
Err(why) => Err(String::from(format!("{:?}", why))),
5959
Ok(p) => Ok(p),
6060
}
6161
}
6262

63-
pub fn parse_expression(source: &String) -> Result<ast::Expression, String> {
63+
pub fn parse_expression(source: &str) -> Result<ast::Expression, String> {
6464
let lxr = lexer::Lexer::new(&source);
6565
match python::ExpressionParser::new().parse(lxr) {
6666
Err(why) => Err(String::from(format!("{:?}", why))),

vm/src/compile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct Compiler {
1818

1919
pub fn compile(
2020
vm: &mut VirtualMachine,
21-
source: &String,
21+
source: &str,
2222
mode: Mode,
2323
source_path: Option<String>,
2424
) -> Result<PyObjectRef, String> {
@@ -115,7 +115,7 @@ impl Compiler {
115115
self.emit(Instruction::ReturnValue);
116116
}
117117

118-
fn compile_statements(&mut self, statements: &Vec<ast::LocatedStatement>) {
118+
fn compile_statements(&mut self, statements: &[ast::LocatedStatement]) {
119119
for statement in statements {
120120
self.compile_statement(statement)
121121
}

vm/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::compile;
44
use super::pyobject::{PyObjectRef, PyResult};
55
use super::vm::VirtualMachine;
66

7-
pub fn eval(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> PyResult {
7+
pub fn eval(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> PyResult {
88
match compile::compile(vm, source, compile::Mode::Eval, None) {
99
Ok(bytecode) => {
1010
debug!("Code object: {:?}", bytecode);

vm/src/import.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::compile;
1313
use super::pyobject::{DictProtocol, PyObjectKind, PyResult};
1414
use super::vm::VirtualMachine;
1515

16-
fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
16+
fn import_uncached_module(vm: &mut VirtualMachine, module: &str) -> PyResult {
1717
// Check for Rust-native modules
1818
if let Some(module) = vm.stdlib_inits.get(module) {
1919
return Ok(module(&vm.ctx).clone());
@@ -53,7 +53,7 @@ fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult
5353
Ok(vm.ctx.new_module(module, scope))
5454
}
5555

56-
fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult {
56+
fn import_module(vm: &mut VirtualMachine, module_name: &str) -> PyResult {
5757
// First, see if we already loaded the module:
5858
let sys_modules = vm.sys_module.get_item("modules").unwrap();
5959
if let Some(module) = sys_modules.get_item(module_name) {
@@ -64,7 +64,7 @@ fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult {
6464
Ok(module)
6565
}
6666

67-
pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<String>) -> PyResult {
67+
pub fn import(vm: &mut VirtualMachine, module_name: &str, symbol: &Option<String>) -> PyResult {
6868
let module = import_module(vm, module_name)?;
6969
// If we're importing a symbol, look it up and use it, otherwise construct a module and return
7070
// that
@@ -75,7 +75,7 @@ pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<Str
7575
Ok(obj)
7676
}
7777

78-
fn find_source(vm: &VirtualMachine, name: &String) -> io::Result<PathBuf> {
78+
fn find_source(vm: &VirtualMachine, name: &str) -> io::Result<PathBuf> {
7979
let sys_path = vm.sys_module.get_item("path").unwrap();
8080
let mut paths: Vec<PathBuf> = match sys_path.borrow().kind {
8181
PyObjectKind::List { ref elements } => elements

vm/src/obj/objsequence.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ impl PySliceableSequence for Vec<PyObjectRef> {
6363
pub fn get_item(
6464
vm: &mut VirtualMachine,
6565
sequence: &PyObjectRef,
66-
elements: &Vec<PyObjectRef>,
66+
elements: &[PyObjectRef],
6767
subscript: PyObjectRef,
6868
) -> PyResult {
6969
match &(subscript.borrow()).kind {
7070
PyObjectKind::Integer { value } => {
71-
let pos_index = elements.get_pos(*value);
71+
let pos_index = elements.to_vec().get_pos(*value);
7272
if pos_index < elements.len() {
7373
let obj = elements[pos_index].clone();
7474
Ok(obj)
@@ -84,10 +84,10 @@ pub fn get_item(
8484
} => Ok(PyObject::new(
8585
match &(sequence.borrow()).kind {
8686
PyObjectKind::Tuple { elements: _ } => PyObjectKind::Tuple {
87-
elements: elements.get_slice_items(&subscript),
87+
elements: elements.to_vec().get_slice_items(&subscript),
8888
},
8989
PyObjectKind::List { elements: _ } => PyObjectKind::List {
90-
elements: elements.get_slice_items(&subscript),
90+
elements: elements.to_vec().get_slice_items(&subscript),
9191
},
9292
ref kind => panic!("sequence get_item called for non-sequence: {:?}", kind),
9393
},

vm/src/obj/objstr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,18 @@ impl PySliceableSequence for String {
147147
}
148148
}
149149

150-
pub fn subscript(vm: &mut VirtualMachine, value: &String, b: PyObjectRef) -> PyResult {
150+
pub fn subscript(vm: &mut VirtualMachine, value: &str, b: PyObjectRef) -> PyResult {
151151
// let value = a
152152
match &(*b.borrow()).kind {
153153
&PyObjectKind::Integer { value: ref pos } => {
154-
let idx = value.get_pos(*pos);
154+
let idx = value.to_string().get_pos(*pos);
155155
Ok(vm.new_str(value[idx..idx + 1].to_string()))
156156
}
157157
&PyObjectKind::Slice {
158158
start: _,
159159
stop: _,
160160
step: _,
161-
} => Ok(vm.new_str(value.get_slice_items(&b))),
161+
} => Ok(vm.new_str(value.to_string().get_slice_items(&b).to_string())),
162162
_ => panic!(
163163
"TypeError: indexing type {:?} with index {:?} is not supported (yet?)",
164164
value, b

vm/src/pyobject.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl PyContext {
273273
)
274274
}
275275

276-
pub fn new_class(&self, name: &String, base: PyObjectRef) -> PyObjectRef {
276+
pub fn new_class(&self, name: &str, base: PyObjectRef) -> PyObjectRef {
277277
objtype::new(self.type_type(), name, vec![base], self.new_dict()).unwrap()
278278
}
279279

@@ -289,10 +289,10 @@ impl PyContext {
289289
}.into_ref()
290290
}
291291

292-
pub fn new_module(&self, name: &String, scope: PyObjectRef) -> PyObjectRef {
292+
pub fn new_module(&self, name: &str, scope: PyObjectRef) -> PyObjectRef {
293293
PyObject::new(
294294
PyObjectKind::Module {
295-
name: name.clone(),
295+
name: name.to_string(),
296296
dict: scope.clone(),
297297
},
298298
self.module_type.clone(),

vm/src/stdlib/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn create_node(ctx: &PyContext, _name: &str) -> PyObjectRef {
5151
node
5252
}
5353

54-
fn statements_to_ast(ctx: &PyContext, statements: &Vec<ast::LocatedStatement>) -> PyObjectRef {
54+
fn statements_to_ast(ctx: &PyContext, statements: &[ast::LocatedStatement]) -> PyObjectRef {
5555
let mut py_statements = vec![];
5656
for statement in statements {
5757
py_statements.push(statement_to_ast(ctx, statement));

vm/src/vm.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ impl VirtualMachine {
219219
self.current_frame().last_value()
220220
}
221221

222-
fn store_name(&mut self, name: &String) -> Option<PyResult> {
222+
fn store_name(&mut self, name: &str) -> Option<PyResult> {
223223
let obj = self.pop_value();
224224
self.current_frame_mut().locals.set_item(name, obj);
225225
None
226226
}
227227

228-
fn load_name(&mut self, name: &String) -> Option<PyResult> {
228+
fn load_name(&mut self, name: &str) -> Option<PyResult> {
229229
// Lookup name in scope and put it onto the stack!
230230
let mut scope = self.current_frame().locals.clone();
231231
loop {
@@ -628,8 +628,8 @@ impl VirtualMachine {
628628
}
629629
}
630630

631-
fn import(&mut self, module: &String, symbol: &Option<String>) -> Option<PyResult> {
632-
let obj = match import(self, module, symbol) {
631+
fn import(&mut self, module: &str, symbol: &Option<String>) -> Option<PyResult> {
632+
let obj = match import(self, &module.to_string(), symbol) {
633633
Ok(value) => value,
634634
Err(value) => return Some(Err(value)),
635635
};
@@ -643,7 +643,7 @@ impl VirtualMachine {
643643
objtype::get_attribute(self, obj.clone(), attr_name)
644644
}
645645

646-
fn load_attr(&mut self, attr_name: &String) -> Option<PyResult> {
646+
fn load_attr(&mut self, attr_name: &str) -> Option<PyResult> {
647647
let parent = self.pop_value();
648648
match self.get_attribute(parent, attr_name) {
649649
Ok(obj) => {
@@ -654,7 +654,7 @@ impl VirtualMachine {
654654
}
655655
}
656656

657-
fn store_attr(&mut self, attr_name: &String) -> Option<PyResult> {
657+
fn store_attr(&mut self, attr_name: &str) -> Option<PyResult> {
658658
let parent = self.pop_value();
659659
let value = self.pop_value();
660660
parent.set_attr(attr_name, value);

0 commit comments

Comments
 (0)