fix: event callback signature

This commit is contained in:
Richard Ramos 2024-02-13 18:03:34 -04:00
parent 5687e2585c
commit 1f9283a849
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
5 changed files with 15 additions and 15 deletions

View File

@ -5,6 +5,10 @@ mod general;
mod node;
mod utils;
// Required so functions inside libwaku can call RLN functions even if we
// use it within the bindings functions
#[allow(clippy::single_component_path_imports)]
#[allow(unused)]
use rln;
pub use node::{

View File

@ -5,11 +5,8 @@
//! When an event is emitted, this callback will be triggered receiving a [`Signal`]
// std
use std::ffi::{c_char, c_int, c_void, CStr};
use std::ops::Deref;
use std::sync::Mutex;
use std::ffi::c_void;
// crates
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
// internal
use crate::general::{WakuMessage, WakuPubSubTopic};
@ -70,22 +67,20 @@ impl WakuMessageEvent {
}
/// Wrapper callback, it transformst the `*const c_char` into a [`Signal`]
/// and executes the [`CALLBACK`] funtion with it
fn callback() -> WakuCallBack {
fn callback<F: FnMut(Signal) + Send + Sync>(mut f: F) -> WakuCallBack {
let cb = |v: &str| {
print!("{}", v);
let data: Signal = serde_json::from_str(v).expect("Parsing signal to succeed");
f(data);
};
let mut closure = cb;
get_trampoline(&closure)
get_trampoline(&cb)
}
/// Register callback to act as event handler and receive application signals,
/// which are used to react to asynchronous events in Waku
pub fn waku_set_event_callback(ctx: *mut c_void) {
pub fn waku_set_event_callback<F: FnMut(Signal) + Send + Sync>(ctx: *mut c_void, f: F) {
// <F: FnMut(Signal) + Send + Sync + 'static> , , f: F
unsafe { waku_sys::waku_set_event_callback(ctx, callback(), std::ptr::null_mut()) };
unsafe { waku_sys::waku_set_event_callback(ctx, callback(f), std::ptr::null_mut()) };
}
#[cfg(test)]

View File

@ -2,7 +2,6 @@
// std
use std::ffi::CString;
use std::ptr;
// crates
use libc::c_void;
// internal
@ -36,7 +35,7 @@ pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<*mut c_void> {
// TODO: create error handler function, format of err message is
// {"message":"The actual message","eventType":"error"}
if error != "" {
if !error.is_empty() {
return Err(error);
}

View File

@ -70,8 +70,8 @@ impl WakuNodeHandle {
relay::waku_relay_unsubscribe(self.ctx, pubsub_topic)
}
pub fn set_event_callback(&self) {
events::waku_set_event_callback(self.ctx)
pub fn set_event_callback<F: FnMut(Signal) + Send + Sync>(&self, f: F) {
events::waku_set_event_callback(self.ctx, f)
}
}

View File

@ -11,6 +11,7 @@ use crate::utils::{get_trampoline, handle_json_response, handle_no_response, han
/// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/)
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding)
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn waku_create_content_topic(
ctx: *mut c_void,
application_name: &str,
@ -55,6 +56,7 @@ pub fn waku_create_content_topic(
}
/// Default pubsub topic used for exchanging waku messages defined in [RFC 10](https://rfc.vac.dev/spec/10/)
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn waku_default_pubsub_topic(ctx: *mut c_void) -> WakuPubSubTopic {
let mut result: String = Default::default();
let result_cb = |v: &str| result = v.to_string();