From abe9f82695d8842875c443d6adbee80a2c0371e4 Mon Sep 17 00:00:00 2001 From: Vincent Thiberville Date: Sat, 27 Apr 2024 19:43:39 +0200 Subject: [PATCH] feat: replace use of Arc> with Arc<...> A Box inside an Arc is not needed, the Arc already puts the value onto the heap. --- boreal-cli/src/main.rs | 12 +++++------- boreal/src/module/console.rs | 9 ++++++--- boreal/src/module/mod.rs | 5 ++--- boreal/tests/it/modules.rs | 4 ++-- boreal/tests/it/utils.rs | 4 ++-- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/boreal-cli/src/main.rs b/boreal-cli/src/main.rs index d5b40855..bcf30aab 100644 --- a/boreal-cli/src/main.rs +++ b/boreal-cli/src/main.rs @@ -262,13 +262,11 @@ fn main() -> ExitCode { let no_console_logs = args.get_flag("no_console_logs"); // Even if the console logs are disabled, add the module so that rules that use it // can still compile properly. - let _r = compiler.add_module(boreal::module::Console::with_callback(Box::new( - move |log| { - if !no_console_logs { - println!("{log}"); - } - }, - ))); + let _r = compiler.add_module(boreal::module::Console::with_callback(move |log| { + if !no_console_logs { + println!("{log}"); + } + })); compiler.set_params( boreal::compiler::CompilerParams::default() diff --git a/boreal/src/module/console.rs b/boreal/src/module/console.rs index 6e761514..55477df3 100644 --- a/boreal/src/module/console.rs +++ b/boreal/src/module/console.rs @@ -5,7 +5,7 @@ use super::{EvalContext, Module, ModuleData, ModuleDataMap, StaticValue, Type, V /// `console` module. pub struct Console { - callback: Arc>, + callback: Arc, } /// Type of callback called when a message is logged. @@ -53,7 +53,7 @@ impl Module for Console { } pub struct Data { - callback: Arc>, + callback: Arc, } impl ModuleData for Console { @@ -66,7 +66,10 @@ impl Console { /// The callback will be called when expressions using this module /// are used. #[must_use] - pub fn with_callback(callback: Box) -> Self { + pub fn with_callback(callback: T) -> Self + where + T: Fn(String) + Send + Sync + 'static, + { Self { callback: Arc::new(callback), } diff --git a/boreal/src/module/mod.rs b/boreal/src/module/mod.rs index 2ba4fb8e..1f7775f3 100644 --- a/boreal/src/module/mod.rs +++ b/boreal/src/module/mod.rs @@ -395,9 +395,8 @@ pub enum Value { /// Value::Integer(x), // Number of matches of string $foo /// ]); /// ``` - // TODO: find a way to simplify this #[allow(clippy::type_complexity)] - Arc) -> Option + Send + Sync>>, + Arc) -> Option + Send + Sync>, ), /// An undefined value. @@ -524,7 +523,7 @@ impl Value { where F: Fn(&mut EvalContext, Vec) -> Option + Send + Sync + 'static, { - Self::Function(Arc::new(Box::new(f))) + Self::Function(Arc::new(f)) } } diff --git a/boreal/tests/it/modules.rs b/boreal/tests/it/modules.rs index 1ae3a59e..8d98e826 100644 --- a/boreal/tests/it/modules.rs +++ b/boreal/tests/it/modules.rs @@ -449,9 +449,9 @@ fn test_module_console() { let mut compiler = Compiler::new(); let res = compiler .compiler - .add_module(boreal::module::Console::with_callback(Box::new(|log| { + .add_module(boreal::module::Console::with_callback(|log| { LOGS.lock().unwrap().push(log); - }))); + })); assert!(res); compiler.add_rules( diff --git a/boreal/tests/it/utils.rs b/boreal/tests/it/utils.rs index f6a9a1b6..b4f2eb91 100644 --- a/boreal/tests/it/utils.rs +++ b/boreal/tests/it/utils.rs @@ -940,7 +940,7 @@ fn static_value_to_value(value: StaticValue) -> ModuleValue { .map(|(k, v)| (k, static_value_to_value(v))) .collect(), ), - StaticValue::Function { fun, .. } => ModuleValue::Function(Arc::new(Box::new(fun))), + StaticValue::Function { fun, .. } => ModuleValue::Function(Arc::new(fun)), } } @@ -976,7 +976,7 @@ fn convert_yara_obj_to_module_value(obj: yara::YrObject) -> ModuleValue { }) .collect(), ), - YrObjectValue::Function => ModuleValue::Function(Arc::new(Box::new(|_, _| None))), + YrObjectValue::Function => ModuleValue::Function(Arc::new(|_, _| None)), YrObjectValue::Undefined => ModuleValue::Undefined, } }