From 947949601f2336f1cdb5bb369632c8b342c2e6cd Mon Sep 17 00:00:00 2001 From: Moritz Gmeiner Date: Tue, 3 Sep 2024 16:56:22 +0200 Subject: [PATCH] replaced some Strings with SmolStr --- interpreter/src/class.rs | 4 ++-- interpreter/src/error.rs | 2 +- interpreter/src/function.rs | 11 +++++------ interpreter/src/interpreter.rs | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/interpreter/src/class.rs b/interpreter/src/class.rs index 59eddf0..dcdba20 100644 --- a/interpreter/src/class.rs +++ b/interpreter/src/class.rs @@ -29,7 +29,7 @@ impl LoxClass { // if class has an init method: insert an implicit "return this;" at its end if let Some(Value::Function(init)) = methods.get("init") { - let name = init.name().to_owned(); + let name = init.name().clone(); let closure = init.closure().clone(); let param_names = init.param_names().to_vec(); let mut body = init.body().clone(); @@ -74,7 +74,7 @@ impl LoxClass { self.superclass.clone() } - pub fn name(&self) -> &str { + pub fn name(&self) -> &SmolStr { &self.name } diff --git a/interpreter/src/error.rs b/interpreter/src/error.rs index 9467d23..3e944ed 100644 --- a/interpreter/src/error.rs +++ b/interpreter/src/error.rs @@ -28,7 +28,7 @@ pub enum RuntimeError { NotCallable { callee: Value }, #[error("{name}() takes {arity} args, but {given} were given.")] WrongArity { - name: String, + name: SmolStr, arity: usize, given: usize, }, diff --git a/interpreter/src/function.rs b/interpreter/src/function.rs index b45d1c2..227d9c9 100644 --- a/interpreter/src/function.rs +++ b/interpreter/src/function.rs @@ -4,11 +4,10 @@ use std::rc::Rc; use rlox2_frontend::parser::Stmt; use smol_str::SmolStr; -use crate::environment::{ClosureScope, HeapedValue}; - use super::environment::Environment; use super::interpreter::EvalResult; use super::{Runtime, Value}; +use crate::environment::{ClosureScope, HeapedValue}; #[derive(Debug, Clone)] pub struct LoxFunction { @@ -35,7 +34,7 @@ impl LoxFunction { Rc::new(fun) } - pub fn name(&self) -> &str { + pub fn name(&self) -> &SmolStr { &self.name } @@ -85,13 +84,13 @@ pub type ExternFunClosure = fn(Vec, &mut Environment) -> EvalResult, arity: usize, closure: ExternFunClosure) -> Self { + pub fn new(name: impl Into, arity: usize, closure: ExternFunClosure) -> Self { let name = name.into(); LoxExternFunction { @@ -101,7 +100,7 @@ impl LoxExternFunction { } } - pub fn name(&self) -> &str { + pub fn name(&self) -> &SmolStr { &self.name } diff --git a/interpreter/src/interpreter.rs b/interpreter/src/interpreter.rs index d52737e..b8695f3 100644 --- a/interpreter/src/interpreter.rs +++ b/interpreter/src/interpreter.rs @@ -215,7 +215,7 @@ impl Eval for Expr { let method = method_expr.eval(env)?; if let Value::Function(ref fun) = method { - let name = fun.name().into(); + let name = fun.name().clone(); methods.insert(name, method); } } @@ -322,7 +322,7 @@ pub fn call_fun(fun: Rc, env: &mut Environment) -> EvalResult