-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow "ABI agnostic" generics in FFI imports. #2770
Comments
This seems like a good idea. |
I would love to have this sort of thing outside the context of FFI as well- a way to write generic code that's guaranteed not to be monomorphized because it's just passing around pointers. |
@rpjohnst That's a separate problem, rust-lang/rust#46477 (which should probably be renamed). |
(Saw this for the first time when I got an email notification due to it being transferred to the RFCs repo, so here's a reply to a year-old comment...)
I somewhat disagree. An optimization pass that discovers functions that happen to be "parameter independent" would be nice to have, but I'd also like a language feature to enforce independence at the source level. This would be useful as a way to control compile time and code size. And it would also allow such parameters to be object safe. |
@comex Fair, although that sounds a bit more like the |
For example, a C library might have the following API:
However, the
c_void
pointee makes it harder to use (having to cast to and from*mut c_void
) and more error-prone (having no real type safety other than "it's a raw pointer and unsafe to deref").We could, instead, allow this definition:
This is valid because we can fully compute the call ABI for
foo_each_bar::<T>
without knowingT
(and this is somethingrustc
has been able to do independently of LLVM for a while now).If, e.g.
<T>
is replaced with<T: ?Sized>
, the definition would be disallowed, since the layout of*mut T
would then depend on the choice ofT
, as opposed to always being a pointer scalar.If that last example works, we can also combine it with Rust references:
Note how the callback no longer needs to be unsafe (since it doesn't take raw pointers)!
An adapter for a closure being used as the callback can be as simple as:
(or even just
|f, bar| f(bar)
if we start coercing closures to non-Rust-ABIfn
pointers)cc @rust-lang/compiler
The text was updated successfully, but these errors were encountered: