Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: extend rust example with waku_start #3224

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@

use std::os::raw::{c_char, c_int, c_void};
use std::{slice, thread, time};
use std::cell::OnceCell;
use std::ffi::CString;
use std::os::raw::{c_char, c_int, c_void};
use std::{slice, thread, time};

pub type WakuCallback =
unsafe extern "C" fn(
c_int,
*const c_char,
usize,
*const c_void,
);
pub type WakuCallback = unsafe extern "C" fn(c_int, *const c_char, usize, *const c_void);

extern "C" {
pub fn waku_new(
Expand All @@ -19,11 +12,9 @@ extern "C" {
user_data: *const c_void,
) -> *mut c_void;

pub fn waku_version(
ctx: *const c_void,
cb: WakuCallback,
user_data: *const c_void,
) -> c_int;
pub fn waku_version(ctx: *const c_void, cb: WakuCallback, user_data: *const c_void) -> c_int;

pub fn waku_start(ctx: *const c_void, cb: WakuCallback, user_data: *const c_void) -> c_int;

pub fn waku_default_pubsub_topic(
ctx: *mut c_void,
Expand All @@ -43,9 +34,8 @@ pub unsafe extern "C" fn trampoline<C>(
let closure = &mut *(data as *mut C);

let buffer_utf8 =
String::from_utf8(slice::from_raw_parts(buffer as *mut u8, buffer_len)
.to_vec())
.expect("valid utf8");
String::from_utf8(slice::from_raw_parts(buffer as *mut u8, buffer_len).to_vec())
.expect("valid utf8");

closure(return_val, &buffer_utf8);
}
Expand All @@ -70,7 +60,7 @@ fn main() {
unsafe {
// Create the waku node
let closure = |ret: i32, data: &str| {
println!("Ret {ret}. Error creating waku node {data}");
println!("Ret {ret}. waku_new closure called {data}");
};
let cb = get_trampoline(&closure);
let config_json_str = CString::new(config_json).unwrap();
Expand Down Expand Up @@ -99,14 +89,20 @@ fn main() {
let _ = default_pubsub_topic.set(data.to_string());
};
let cb = get_trampoline(&closure);
let _ret = waku_default_pubsub_topic(
ctx,
cb,
&closure as *const _ as *const c_void,
);
let _ret = waku_default_pubsub_topic(ctx, cb, &closure as *const _ as *const c_void);

println!("Version: {}", version.get_or_init(|| unreachable!()));
println!("Default pubsubTopic: {}", default_pubsub_topic.get_or_init(|| unreachable!()));
println!(
"Default pubsubTopic: {}",
default_pubsub_topic.get_or_init(|| unreachable!())
);

// Start the Waku node
let closure = |ret: i32, data: &str| {
println!("Ret {ret}. waku_start closure called {data}");
};
let cb = get_trampoline(&closure);
let _ret = waku_start(ctx, cb, &closure as *const _ as *const c_void);
}

loop {
Expand Down