diff --git a/engine/translator/benches/bench.rs b/engine/translator/benches/bench.rs index a4d8bae..d9ca99a 100644 --- a/engine/translator/benches/bench.rs +++ b/engine/translator/benches/bench.rs @@ -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. diff --git a/engine/translator/src/lib.rs b/engine/translator/src/lib.rs index d706900..c5acdfc 100644 --- a/engine/translator/src/lib.rs +++ b/engine/translator/src/lib.rs @@ -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::(&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)) })