-
Notifications
You must be signed in to change notification settings - Fork 57
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(cbindings): cbindings rust simple libwaku integration example #2089
Conversation
You can find the image built from this PR at
Built from fd7bcec |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but could be simplified a bit.
let buffer_utf8 = | ||
String::from_utf8(slice::from_raw_parts(buffer as *mut u8, buffer_len) | ||
.to_vec()) | ||
.expect("valid utf8"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do the allocation later in the closure. Using str::from_utf8
here instead.
examples/rust/src/main.rs
Outdated
); | ||
|
||
// Extracting the current waku version | ||
let version: Arc<RefCell<String>> = Arc::new(RefCell::new(String::from(""))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to ref count here since you don't clone. Also you could use a OnceCell
since you won't be modifying more than once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much! This has been applied in 36df31b
examples/rust/src/main.rs
Outdated
); | ||
|
||
// Extracting the default pubsub topic | ||
let default_pubsub_topic: Arc<RefCell<String>> = Arc::new(RefCell::new(String::from(""))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
examples/rust/src/main.rs
Outdated
*version.borrow_mut() = data.to_string(); | ||
}; | ||
let cb = get_trampoline(&closure); | ||
waku_set_user_data(&mut ctx, &closure as *const _ as *const c_void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be possible to get rid of this. See experiment here: https://github.com/richard-ramos/test_rust_ffi/blob/af098508acfae181df508eee524d75f70af68010/rustlib/src/lib.rs#L29-L38
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks beautiful indeed!
653a712
to
9a2cfdb
Compare
Description
The purpose of this PR is to show how the
libwaku
can be used from Rust.So far, it performs the following points:
waku_new
(create waku node.)waku_version
(extract default pubsub topic to the Rust space.)waku_default_pubsub_topic
(extract default pubsub topic to the Rust space.)The "trampoline" technique is being used in order to properly pass Rust closures as callbacks to the
libwaku
. Therefore, we don't have the same concise code that we can achieve in cpp but it is indeed feasible. The code can become more concise by definingmacros!
and the idea for the next step would be to properly expose a safe API to the user and leave the unsafe code managed internally.Notice that this PR also includes changes in
libwaku
that make it avoid the use of global variables, as stated in the next recommendation: #1865 (comment). This should definitely go in a separate PR.cc - @jm-clius @fryorcraken @alrevuelta @vpavlin @NagyZoltanPeter @gabrielmer