Skip to content

Commit

Permalink
feat: add UnwindSafe traits to module private datas
Browse files Browse the repository at this point in the history
  • Loading branch information
vthib committed Sep 22, 2024
1 parent 56111d7 commit 4350230
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
5 changes: 3 additions & 2 deletions boreal/src/module/console.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::Write;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::{collections::HashMap, sync::Arc};

use super::{EvalContext, Module, ModuleData, ModuleDataMap, StaticValue, Type, Value};
Expand All @@ -9,7 +10,7 @@ pub struct Console {
}

/// Type of callback called when a message is logged.
pub type LogCallback = dyn Fn(String) + Send + Sync;
pub type LogCallback = dyn Fn(String) + Send + Sync + UnwindSafe + RefUnwindSafe;

impl Module for Console {
fn get_name(&self) -> &'static str {
Expand Down Expand Up @@ -69,7 +70,7 @@ impl Console {
#[must_use]
pub fn with_callback<T>(callback: T) -> Self
where
T: Fn(String) + Send + Sync + 'static,
T: Fn(String) + Send + Sync + UnwindSafe + RefUnwindSafe + 'static,
{
Self {
callback: Arc::new(callback),
Expand Down
22 changes: 14 additions & 8 deletions boreal/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl std::fmt::Debug for ModuleUserData {

/// Object holding the data of each module. See [`ModuleData`].
pub struct ModuleDataMap<'scanner> {
private_data: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
private_data: HashMap<TypeId, Box<dyn Any + Send + Sync + UnwindSafe + RefUnwindSafe>>,
user_data: &'scanner ModuleUserData,
}

Expand Down Expand Up @@ -390,7 +390,7 @@ pub trait ModuleData: Module {
/// Private Data to associate with the module.
///
/// This is the data that the module can store and retrieve during a scan.
type PrivateData: Any + Send + Sync;
type PrivateData: Any + Send + Sync + UnwindSafe + RefUnwindSafe;

/// Data that the user can provide to the module.
///
Expand All @@ -417,17 +417,23 @@ impl<'scanner> ModuleDataMap<'scanner> {
/// Retrieve the private data of a module.
#[must_use]
pub fn get<T: Module + ModuleData + 'static>(&self) -> Option<&T::PrivateData> {
self.private_data
.get(&TypeId::of::<T>())
.and_then(|v| v.downcast_ref())
self.private_data.get(&TypeId::of::<T>()).and_then(|v| {
// For some reason, having "dyn Any + Send + Sync + UnwindSafe + RefUnwindSafe"
// causes the rust compiler to fail to resolve `v.downcast_ref()`, so we need to
// explicit where this method comes from.
<dyn Any>::downcast_ref(&**v)
})
}

/// Retrieve a mutable borrow on the private data of a module.
#[must_use]
pub fn get_mut<T: Module + ModuleData + 'static>(&mut self) -> Option<&mut T::PrivateData> {
self.private_data
.get_mut(&TypeId::of::<T>())
.and_then(|v| v.downcast_mut())
self.private_data.get_mut(&TypeId::of::<T>()).and_then(|v| {
// For some reason, having "dyn Any + Send + Sync + UnwindSafe + RefUnwindSafe"
// causes the rust compiler to fail to resolve `v.downcast_mut()`, so we need to
// explicit where this method comes from.
<dyn Any>::downcast_mut(&mut **v)
})
}

/// Retrieve the user data of a module.
Expand Down

0 comments on commit 4350230

Please sign in to comment.