Skip to content

Commit 5ecff1b

Browse files
committed
Remove unnecessary to_string conversions
1 parent d508d13 commit 5ecff1b

File tree

14 files changed

+36
-51
lines changed

14 files changed

+36
-51
lines changed

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn main() {
6060
// Figure out if a script was passed:
6161
match matches.value_of("script") {
6262
None => run_shell(&mut vm),
63-
Some(filename) => run_script(&mut vm, &filename.to_string()),
63+
Some(filename) => run_script(&mut vm, filename),
6464
}
6565
};
6666

@@ -69,7 +69,7 @@ fn main() {
6969
}
7070

7171
fn _run_string(vm: &mut VirtualMachine, source: &str, source_path: Option<String>) -> PyResult {
72-
let code_obj = compile::compile(vm, &source.to_string(), compile::Mode::Exec, source_path)?;
72+
let code_obj = compile::compile(vm, source, compile::Mode::Exec, source_path)?;
7373
// trace!("Code object: {:?}", code_obj.borrow());
7474
let builtins = vm.get_builtin_scope();
7575
let vars = vm.context().new_scope(Some(builtins)); // Keep track of local variables
@@ -114,7 +114,7 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
114114
}
115115

116116
fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool {
117-
match compile::compile(vm, &source.to_string(), compile::Mode::Single, None) {
117+
match compile::compile(vm, source, compile::Mode::Single, None) {
118118
Ok(code) => {
119119
match vm.run_code_obj(code, scope) {
120120
Ok(_value) => {

vm/src/builtins.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ fn builtin_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
405405

406406
fn builtin_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
407407
arg_check!(vm, args, required = [(obj, None)]);
408-
let len_method_name = "__len__".to_string();
409-
match vm.get_method(obj.clone(), &len_method_name) {
408+
let len_method_name = "__len__";
409+
match vm.get_method(obj.clone(), len_method_name) {
410410
Ok(value) => vm.invoke(value, PyFuncArgs::default()),
411411
Err(..) => Err(vm.new_type_error(format!(
412412
"object of type '{}' has no method {:?}",
@@ -616,8 +616,8 @@ fn builtin_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
616616
required = [(x, None), (y, None)],
617617
optional = [(mod_value, Some(vm.ctx.int_type()))]
618618
);
619-
let pow_method_name = "__pow__".to_string();
620-
let result = match vm.get_method(x.clone(), &pow_method_name) {
619+
let pow_method_name = "__pow__";
620+
let result = match vm.get_method(x.clone(), pow_method_name) {
621621
Ok(attrib) => vm.invoke(attrib, PyFuncArgs::new(vec![y.clone()], vec![])),
622622
Err(..) => Err(vm.new_type_error("unsupported operand type(s) for pow".to_string())),
623623
};
@@ -626,11 +626,8 @@ fn builtin_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
626626
//order to improve performance
627627
match mod_value {
628628
Some(mod_value) => {
629-
let mod_method_name = "__mod__".to_string();
630-
match vm.get_method(
631-
result.expect("result not defined").clone(),
632-
&mod_method_name,
633-
) {
629+
let mod_method_name = "__mod__";
630+
match vm.get_method(result.expect("result not defined").clone(), mod_method_name) {
634631
Ok(value) => vm.invoke(value, PyFuncArgs::new(vec![mod_value.clone()], vec![])),
635632
Err(..) => {
636633
Err(vm.new_type_error("unsupported operand type(s) for mod".to_string()))
@@ -731,8 +728,8 @@ fn builtin_zip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
731728
// builtin___import__
732729

733730
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
734-
let mod_name = "__builtins__".to_string();
735-
let py_mod = ctx.new_module(&mod_name, ctx.new_scope(None));
731+
let mod_name = "__builtins__";
732+
let py_mod = ctx.new_module(mod_name, ctx.new_scope(None));
736733

737734
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
738735
ctx.set_attr(&py_mod, "__name__", ctx.new_str(String::from("__main__")));

vm/src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ impl Frame {
671671
None => PathBuf::from("."),
672672
};
673673

674-
let obj = import(vm, current_path, &module.to_string(), symbol)?;
674+
let obj = import(vm, current_path, module, symbol)?;
675675

676676
// Push module on stack:
677677
self.push_value(obj);

vm/src/obj/objstr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn str_capitalize(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
352352
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
353353
let value = get_value(&s);
354354
let (first_part, lower_str) = value.split_at(1);
355-
let capitalized = format!("{}{}", first_part.to_uppercase().to_string(), lower_str);
355+
let capitalized = format!("{}{}", first_part.to_uppercase(), lower_str);
356356
Ok(vm.ctx.new_str(capitalized))
357357
}
358358

vm/src/stdlib/ast.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,24 +610,20 @@ fn ast_parse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
610610
}
611611

612612
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
613-
let ast_mod = ctx.new_module(&"ast".to_string(), ctx.new_scope(None));
613+
let ast_mod = ctx.new_module("ast", ctx.new_scope(None));
614614
ctx.set_attr(&ast_mod, "parse", ctx.new_rustfunc(ast_parse));
615615
ctx.set_attr(
616616
&ast_mod,
617617
"Module",
618-
ctx.new_class(&"_ast.Module".to_string(), ctx.object()),
618+
ctx.new_class("_ast.Module", ctx.object()),
619619
);
620620

621621
// TODO: maybe we can use some clever macro to generate this?
622622
ctx.set_attr(
623623
&ast_mod,
624624
"FunctionDef",
625-
ctx.new_class(&"_ast.FunctionDef".to_string(), ctx.object()),
626-
);
627-
ctx.set_attr(
628-
&ast_mod,
629-
"Call",
630-
ctx.new_class(&"_ast.Call".to_string(), ctx.object()),
625+
ctx.new_class("_ast.FunctionDef", ctx.object()),
631626
);
627+
ctx.set_attr(&ast_mod, "Call", ctx.new_class("_ast.Call", ctx.object()));
632628
ast_mod
633629
}

vm/src/stdlib/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
232232
}
233233

234234
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
235-
let json_mod = ctx.new_module(&"json".to_string(), ctx.new_scope(None));
235+
let json_mod = ctx.new_module("json", ctx.new_scope(None));
236236
ctx.set_attr(&json_mod, "dumps", ctx.new_rustfunc(dumps));
237237
ctx.set_attr(&json_mod, "loads", ctx.new_rustfunc(loads));
238238
// TODO: Make this a proper type with a constructor

vm/src/stdlib/keyword.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn keyword_iskeyword(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1818
}
1919

2020
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
21-
let py_mod = ctx.new_module(&"keyword".to_string(), ctx.new_scope(None));
21+
let py_mod = ctx.new_module("keyword", ctx.new_scope(None));
2222
ctx.set_attr(&py_mod, "iskeyword", ctx.new_rustfunc(keyword_iskeyword));
2323
let keyword_kwlist = ctx.new_list(
2424
lexer::get_keywords()

vm/src/stdlib/re.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ fn re_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5050
}
5151

5252
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
53-
let py_mod = ctx.new_module(&"re".to_string(), ctx.new_scope(None));
54-
let match_type = ctx.new_class(&"Match".to_string(), ctx.object());
53+
let py_mod = ctx.new_module("re", ctx.new_scope(None));
54+
let match_type = ctx.new_class("Match", ctx.object());
5555
ctx.set_attr(&py_mod, "Match", match_type);
5656
ctx.set_attr(&py_mod, "match", ctx.new_rustfunc(re_match));
5757
ctx.set_attr(&py_mod, "search", ctx.new_rustfunc(re_search));

vm/src/stdlib/string.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,15 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
2121
*/
2222

2323
// Constants:
24-
ctx.set_attr(&py_mod, "ascii_letters", ctx.new_str(ascii_letters.clone()));
25-
ctx.set_attr(
26-
&py_mod,
27-
"ascii_lowercase",
28-
ctx.new_str(ascii_lowercase.clone()),
29-
);
30-
ctx.set_attr(
31-
&py_mod,
32-
"ascii_uppercase",
33-
ctx.new_str(ascii_uppercase.clone()),
34-
);
35-
ctx.set_attr(&py_mod, "digits", ctx.new_str(digits.clone()));
36-
ctx.set_attr(&py_mod, "hexdigits", ctx.new_str(hexdigits.clone()));
37-
ctx.set_attr(&py_mod, "octdigits", ctx.new_str(octdigits.clone()));
38-
// ctx.set_attr(&py_mod, "printable", ctx.new_str(printable.clone()));
39-
ctx.set_attr(&py_mod, "punctuation", ctx.new_str(punctuation.clone()));
40-
// ctx.set_attr(&py_mod, "whitespace", ctx.new_str(whitespace.clone()));
24+
ctx.set_attr(&py_mod, "ascii_letters", ctx.new_str(ascii_letters));
25+
ctx.set_attr(&py_mod, "ascii_lowercase", ctx.new_str(ascii_lowercase));
26+
ctx.set_attr(&py_mod, "ascii_uppercase", ctx.new_str(ascii_uppercase));
27+
ctx.set_attr(&py_mod, "digits", ctx.new_str(digits));
28+
ctx.set_attr(&py_mod, "hexdigits", ctx.new_str(hexdigits));
29+
ctx.set_attr(&py_mod, "octdigits", ctx.new_str(octdigits));
30+
// ctx.set_attr(&py_mod, "printable", ctx.new_str(printable));
31+
ctx.set_attr(&py_mod, "punctuation", ctx.new_str(punctuation));
32+
// ctx.set_attr(&py_mod, "whitespace", ctx.new_str(whitespace));
4133

4234
py_mod
4335
}

vm/src/stdlib/time_module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn time_time(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3131
}
3232

3333
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
34-
let py_mod = ctx.new_module(&"time".to_string(), ctx.new_scope(None));
34+
let py_mod = ctx.new_module("time", ctx.new_scope(None));
3535
ctx.set_attr(&py_mod, "sleep", ctx.new_rustfunc(time_sleep));
3636
ctx.set_attr(&py_mod, "time", ctx.new_rustfunc(time_time));
3737
py_mod

0 commit comments

Comments
 (0)