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

Add callback to declare functions/statics safe #3058

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

arctic-alpaca
Copy link
Contributor

Since Rust 1.82, extern items can be marked as safe. This PR adds the ability to mark functions and statics in the generated bindings as safe via ParseCallbacks.
To fit into Rust's typical safety comment conventions, the callback returns a string that will be prepended to the generated item as safety comment.
Becauseprettyplease does not support non-doc comments, I added an additional test using Formatter::Rustfmt.

Comment on lines +471 to +477
/* Safety : "safe to access" */
pub safe static my_safe_var: [::std::os::raw::c_int; 3usize];
}
unsafe extern "C" {
/* Safety : "safe to call" */
pub safe fn my_safe_func() -> ::std::os::raw::c_int;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these comments meant to describe only the reason why they are marked as safe, i.e. rather than the full signature being correct? Related: rust-lang/rust-clippy#13560.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The specific comments are just there to test things are working, they could be replaced by dummy reasoning for test.

The comments can be used to express any kind of safety reasoning. Generally I'd expect people to only reason about why a function is safe to call or a const is safe to access and to rely on bindgen to generate correct bindings.

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.

2 participants