rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Add callback to declare functions/statics safe

Open arctic-alpaca opened this issue 1 year ago • 2 comments

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.

arctic-alpaca avatar Dec 18 '24 16:12 arctic-alpaca

Hoping to use this soon. Is there anything that's holding progress up on merging this?

loftyinclination avatar Dec 07 '25 17:12 loftyinclination

But also, it feels not the most useful feature, because as soon as you have a pointer input around you can't claim that the function is safe.

I think that this could be quite useful when marking functions whose purpose is to allocate structs helpful -- any reduction in the number of possible unsafe bindings is helpful when dealing with large code bases.

Having said that, I agree with your point about pointers -- currently, given the information accessibly through ItemInfo, it isn't possible to determine whether or not a function takes any pointers. I imagine that changing that would be outside of the scope of this work though.

loftyinclination avatar Dec 08 '25 13:12 loftyinclination