Skip to content

Commit

Permalink
feat: replace use of Arc<Box<...>> with Arc<...>
Browse files Browse the repository at this point in the history
A Box inside an Arc is not needed, the Arc already puts the value onto
the heap.
  • Loading branch information
vthib committed Apr 27, 2024
1 parent 776221c commit abe9f82
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
12 changes: 5 additions & 7 deletions boreal-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions boreal/src/module/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{EvalContext, Module, ModuleData, ModuleDataMap, StaticValue, Type, V

/// `console` module.
pub struct Console {
callback: Arc<Box<LogCallback>>,
callback: Arc<LogCallback>,
}

/// Type of callback called when a message is logged.
Expand Down Expand Up @@ -53,7 +53,7 @@ impl Module for Console {
}

pub struct Data {
callback: Arc<Box<LogCallback>>,
callback: Arc<LogCallback>,
}

impl ModuleData for Console {
Expand All @@ -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<LogCallback>) -> Self {
pub fn with_callback<T>(callback: T) -> Self
where
T: Fn(String) + Send + Sync + 'static,
{
Self {
callback: Arc::new(callback),
}
Expand Down
5 changes: 2 additions & 3 deletions boreal/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Fn(&mut EvalContext, Vec<Value>) -> Option<Value> + Send + Sync>>,
Arc<dyn Fn(&mut EvalContext, Vec<Value>) -> Option<Value> + Send + Sync>,
),

/// An undefined value.
Expand Down Expand Up @@ -524,7 +523,7 @@ impl Value {
where
F: Fn(&mut EvalContext, Vec<Value>) -> Option<Value> + Send + Sync + 'static,
{
Self::Function(Arc::new(Box::new(f)))
Self::Function(Arc::new(f))
}
}

Expand Down
4 changes: 2 additions & 2 deletions boreal/tests/it/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions boreal/tests/it/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}

Expand Down Expand Up @@ -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,
}
}
Expand Down

0 comments on commit abe9f82

Please sign in to comment.