From 38738948f71525d595c95ccbfe777397535dbece Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Sun, 25 Feb 2024 23:05:26 +0100 Subject: [PATCH] examples/rust/src/main.rs: adapt code to latest libwaku state --- examples/rust/src/main.rs | 61 +++++++++++++++------------------------ 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/examples/rust/src/main.rs b/examples/rust/src/main.rs index 92689c3826..b80199949f 100644 --- a/examples/rust/src/main.rs +++ b/examples/rust/src/main.rs @@ -7,45 +7,39 @@ use std::ffi::CString; pub type WakuCallback = unsafe extern "C" fn( + c_int, *const c_char, usize, *const c_void, ); extern "C" { - pub fn waku_init( - cb: WakuCallback, - user_data: *const c_void, - ) -> *mut c_void; - pub fn waku_new( - ctx: *mut *mut c_void, config_json: *const u8, cb: WakuCallback, - ) -> c_int; + user_data: *const c_void, + ) -> *mut c_void; pub fn waku_version( - ctx: *mut *mut c_void, + ctx: *const c_void, cb: WakuCallback, + user_data: *const c_void, ) -> c_int; pub fn waku_default_pubsub_topic( - ctx: *mut *mut c_void, + ctx: *mut c_void, cb: WakuCallback, - ) -> *mut c_void; - - pub fn waku_set_user_data( - ctx: *mut *mut c_void, user_data: *const c_void, - ); + ) -> *mut c_void; } pub unsafe extern "C" fn trampoline( + return_val: c_int, buffer: *const c_char, buffer_len: usize, data: *const c_void, ) where - C: FnMut(&str), + C: FnMut(i32, &str), { let closure = &mut *(data as *mut C); @@ -54,12 +48,12 @@ pub unsafe extern "C" fn trampoline( .to_vec()) .expect("valid utf8"); - closure(&buffer_utf8); + closure(return_val, &buffer_utf8); } pub fn get_trampoline(_closure: &C) -> WakuCallback where - C: FnMut(&str), + C: FnMut(i32, &str), { trampoline:: } @@ -74,52 +68,45 @@ fn main() { }"; unsafe { - // Initialize the waku library - let closure = |data: &str| { - println!("Error initializing the waku library: {data} \n\n"); - }; - let cb = get_trampoline(&closure); - let mut ctx = waku_init(cb, &closure as *const _ as *const c_void); - // Create the waku node - let closure = |data: &str| { - println!("Error creating waku node {data} \n\n"); + let closure = |ret: i32, data: &str| { + println!("Ret {ret}. Error creating waku node {data}"); }; let cb = get_trampoline(&closure); let config_json_str = CString::new(config_json).unwrap(); - waku_set_user_data(&mut ctx, &closure as *const _ as *const c_void); - let _ret = waku_new( - &mut ctx, + let ctx = waku_new( config_json_str.as_ptr() as *const u8, cb, + &closure as *const _ as *const c_void, ); // Extracting the current waku version let version: Arc> = Arc::new(RefCell::new(String::from(""))); - let closure = |data: &str| { + let closure = |ret: i32, data: &str| { + println!("version_closure. Ret: {ret}. Data: {data}"); *version.borrow_mut() = data.to_string(); }; let cb = get_trampoline(&closure); - waku_set_user_data(&mut ctx, &closure as *const _ as *const c_void); let _ret = waku_version( - &mut ctx, + &ctx as *const _ as *const c_void, cb, + &closure as *const _ as *const c_void, ); // Extracting the default pubsub topic let default_pubsub_topic: Arc> = Arc::new(RefCell::new(String::from(""))); - let closure = |data: &str| { + let closure = |_ret: i32, data: &str| { *default_pubsub_topic.borrow_mut() = data.to_string(); }; let cb = get_trampoline(&closure); - waku_set_user_data(&mut ctx, &closure as *const _ as *const c_void); let _ret = waku_default_pubsub_topic( - &mut ctx, + ctx, cb, + &closure as *const _ as *const c_void, ); - println!("Version {}", version.borrow()); - println!("Default pubsubTopic {}", default_pubsub_topic.borrow()); + println!("Version: {}", version.borrow()); + println!("Default pubsubTopic: {}", default_pubsub_topic.borrow()); } loop {