-
Notifications
You must be signed in to change notification settings - Fork 707
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
feature: wrap every C function on unsafe blocks #2774
Comments
Could you clarify what your actual output is and what you expect? Are you saying that |
Like
No sure if there, or somewhere else, since that looks more like a declaration. But yes, something like that. |
What problem are you trying to solve? That unsafe block doesn’t do anything. |
To avoid having to manually add the unsafe block every time calling one of the binded C functions. As things are, when calling those functions, if not wrapping them up with unsafe, rust complains because rust considers C funstions unsafe. Besides ending up with a very populated code with unsafe, it could be avoided introducing the unsafe wrappers when creating the bindings. |
Unsafe blocks don't work like that, I think you mean just making the generated functions not Unsafe doesn't mean unsound though, so usually your abstraction layer provides a thin wrapper to make it safe. It is always good practice to explain why an pub fn function_name(a: i32, b: i32) -> i32 {
// SAFETY: FFI call with no preconditions
unsafe { c_function_name(a, b) }
} |
Well, yes. I was looking for something like that, because when one is binding hundreds of C functions, none of them really unsafe, it's quite a lot of writing unsafe every time one of such functions is called. Automating such wrapping sounds much more better than having to is on every call for C functions... |
If they are truly safe to call (which is rare, but it does happen sometimes), then I agree it could be nice to automate. However, doing it unconditionally is typically wrong, i.e. the symbols should be manually picked, e.g. from a list/regex passed in a flag or perhaps supporting a Would it be possible to see some of the functions you are dealing with? |
Sorry but they are literally unsafe as you need to hold safety invariants that you don't have to care about when calling non foreign functions.
This is a feature in my opinion. If you call a foreign function you have to be extra careful anyway, writing that
I'm personally opposed to add any functionality that would allow removing |
Please let me know if a "discussion" would be a better fit...
Every C function requires to be wrapped by an unsafe block, otherwise rust complains about it because it considers C unasfe.
What I see getting generate for an arbitrary C function is:
When the generator like:
The thing with the generated rust bindings, is that one always has to wrap each binding with an unsafe block, since C is considered unsafe...
Note I also tried:
And that doesn't help, one still needs to wrap bindings with unsafe blocks. The doc for unsafe_ops sort of suggest it only wraps functions considered unsafe on the C context, not any C function.
Does it make sense? Should all C functions be wrapped by default with unsafe blocks, and if wanted, turn that behavior off?
Thanks !
The text was updated successfully, but these errors were encountered: