-
Notifications
You must be signed in to change notification settings - Fork 58
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
Why isn't Box<T>
FFI-safe for sized T
?
#334
Comments
This might be related to #198 -- if you declare a function as |
By this reasoning,
I don't understand your hypothetical. Why would a Rust declaration for a C-implemented function constrain C callers of that function? It seems sufficient for the purposes of a Rust spec to say what happens (and what behavior by this function will cause UB) when it's called from Rust. Who cares what happens when it's called from C? (The other thing I don't understand is how literally to take "effectively becomes |
You end up having multiple different declarations of the same function (some with and some without For this to affect calls from C, xLTO needs to be enabled. Without xLTO it still affects the case where Rust code imports the same function multiple times with different signatures. If any signature makes aliasing guarantees, behavior is as if all signatures did that, and hence all callers need to be careful. Indeed this also affects To be clear, I am not entirely sure if this is the reason for the FFI warning. I just wanted to mention this since this is a reason for being careful with using Rust reference types in FFI signatures.
That box is definitely much safer in various ways, since it opts-out from all the |
Ralf, thanks for explaining this. I was able to amaze my friends with this knowledge. To sum up:
Thanks again for your time! |
Hi, apologies in advance if I'm asking this in the wrong place or year. ;)
My real use case: Define a big "everything-but-the-kitchen-sink"-style struct in Rust and pass it by pointer between Rust and C, using cbindgen to make the fields accessible from C, as part of adding Rust code to an existing C project. One of the fields is an
Option<Box<OtherStruct>>
. Rust warns thatBox<T>
is not FFI-safe.Current workaround: Our own custom, FFI-safe box type (based on
NonNull<T>
).The documentation for
Box
says:How do we feel about that sentence? If we accept that, is there a good reason for
Box<T>
not to be FFI-safe whenT: Sized
?The text was updated successfully, but these errors were encountered: