Skip to content

Commit

Permalink
chore(translator): reduce useless clone
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad committed Mar 30, 2024
1 parent 4a814b1 commit 465876f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
7 changes: 5 additions & 2 deletions engine/translator/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ pub fn translate(c: &mut Criterion) {
#[cfg(feature = "rhai")]
{
let engine = Engine::new();
let script = engine
let script_a = engine
.compile("fn translate(input) { [input, \"\", input, false] }")
.unwrap();
translator.register("give_back".to_owned(), script);
let script_b = engine.compile("fn translate(input) { [input, \"\", input.len().to_string(), false] }").unwrap();

translator.register("give_back".to_owned(), script_a);
translator.register("to_length".to_owned(), script_b);
}

// Generates candidates for testing.
Expand Down
23 changes: 13 additions & 10 deletions engine/translator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,24 @@ impl Translator {
#[cfg(feature = "rhai")]
let predicates =
predicates.chain(self.translators.iter().filter_map(|(_name, translator)| {
let data = engine
let mut data = engine
.call_fn::<Array>(&mut scope, translator, "translate", (input.to_owned(),))
.unwrap_or_default();

(data.len() == 4).then(|| {
let code = data[0].clone().into_string().unwrap();
let remaining_code = data[1].clone().into_string().unwrap();
let texts = data[2]
.clone()
.into_array()
.unwrap_or(vec![data[2].clone()])
.iter()
.map(|e| e.clone().into_string().unwrap())
let code = data.remove(0).into_string().unwrap();
let remaining_code = data.remove(0).into_string().unwrap();
let texts = data.remove(0);
let texts = if texts.is_array() {
texts.into_array().unwrap()
} else {
vec![texts]
};
let texts = texts
.into_iter()
.map(|e| e.into_string().unwrap())
.collect();
let translated = data[3].clone().as_bool().unwrap();
let translated = data.remove(0).as_bool().unwrap();

(1.0, (code, remaining_code, texts, translated))
})
Expand Down

0 comments on commit 465876f

Please sign in to comment.