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(cbindings): cbindings rust simple libwaku integration example #2089

Merged
merged 6 commits into from
Feb 26, 2024

Conversation

Ivansete-status
Copy link
Collaborator

@Ivansete-status Ivansete-status commented Sep 28, 2023

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 defining macros! 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

@github-actions
Copy link

github-actions bot commented Sep 28, 2023

You can find the image built from this PR at

quay.io/wakuorg/nwaku-pr:2089

Built from fd7bcec

@Ivansete-status Ivansete-status self-assigned this Sep 28, 2023
Copy link
Contributor

@SionoiS SionoiS left a 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.

Comment on lines +52 to +49
let buffer_utf8 =
String::from_utf8(slice::from_raw_parts(buffer as *mut u8, buffer_len)
.to_vec())
.expect("valid utf8");
Copy link
Contributor

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.

);

// Extracting the current waku version
let version: Arc<RefCell<String>> = Arc::new(RefCell::new(String::from("")));
Copy link
Contributor

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.

Copy link
Collaborator Author

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

);

// Extracting the default pubsub topic
let default_pubsub_topic: Arc<RefCell<String>> = Arc::new(RefCell::new(String::from("")));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

*version.borrow_mut() = data.to_string();
};
let cb = get_trampoline(&closure);
waku_set_user_data(&mut ctx, &closure as *const _ as *const c_void);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks beautiful indeed!

@Ivansete-status Ivansete-status changed the title chore(cbindings): WIP-paused. cbindings rust chore(cbindings): cbindings rust simple libwaku integration example Feb 25, 2024
@Ivansete-status Ivansete-status marked this pull request as ready for review February 26, 2024 12:14
@Ivansete-status Ivansete-status merged commit a499300 into master Feb 26, 2024
8 checks passed
@Ivansete-status Ivansete-status deleted the cbindings-rust branch February 26, 2024 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants