I have created one ffi library and I want to use it in my main project which is a web service using actix web.
Basically the ffi library struct which i want to use in my project is as following
pub struct FfiWrapper {
task_ptr: *const FfiTask,
}
impl FfiWrapper {
pub fn new() -> Result<Self, ()> {
unsafe {
let task_ptr = bindgen_bindings::CreateTask();
if task_ptr.is_null() {
Err(())
} else {
Ok(Self { task_ptr })
}
}
}
pub fn start_task(&self) -> i32 {
let res = unsafe { bindgen_bindings::StartTask(self.task_ptr) };
res
}
}
impl Drop for FfiWrapper {
fn drop(&mut self) {
unsafe {
bindgen_bindings::DeleteTask(self.task_ptr);
}
}
}
The above library code is written by me and can be modified if required
where bindgen_bindings
is binding created by bindgen for the c library i am using
In my main actix web project I want to pass the FfiWrapper so that I can use it when new request hits one of my endpoint ffi_endpoint
like so
let ffi_wrapper = Data::new(Mutex::new(tts_engine));
let server = HttpServer::new(|| App::new().service(ffi_endpoint).app_data(ffi_wrapper))
.listen(listener)?
.run();
But I am getting following error
`*const bindgen_sys::bindgen_bindings::FfiTask_T` cannot be sent between threads safely
within `FfiWrapper`, the trait `Send` is not implemented for `*const bindgen_sys::bindgen_bindings::FfiTask_T`, which is required by `{closure@src/startup.rsSend`
required for `Mutex<EngineWrapper>` to implement `Sync`
required for `Arc<Mutex<EngineWrapper>>` to implement `Send`
My bindgen bindings which were created
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct FfiTask_T {
_unused: [u8; 0],
}
pub type FfiTask = FfiTask_T;
extern "C" {
pub fn CreateTask() -> *const FfiTask;
}
extern "C" {
pub fn DeleteTask(ptask: *const FfiTask);
}
My C library header file function definitions
struct FfiTask_T;
typedef struct FfiTask_T FfiTask;
const FfiTask* CreateTask();
int StartTask(const FfiTask* pTask);
void DeleteTask(const FfiTask *ptask);
I am new to rust and ffi in general. Can someone please help me with above query.
Thanks