-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 coercing non-capturing closures to function pointers. #32449
Comments
I like this proposal, but it might fare better for discussion on either https://github.com/rust-lang/rfcs/issues or https://internals.rust-lang.org/. Issues on this repo are more for directly actionable tasks, like bugfixes and implementation/stabilization of features which have already gone through the RFC process. |
One thing that Rust has which C++ doesn't is that functions can be defined anywhere: const A: [fn(); 4] = [
{fn tmp() { unsafe { b = 4 } } tmp},
{fn tmp() { unsafe { b = 3 } } tmp},
{fn tmp() { unsafe { b = 2 } } tmp},
{fn tmp() { unsafe { b = 1 } } tmp},
]; so perhaps nicer syntax for this? |
We do already have the closure syntax, so adding more syntax for anonymous functions is probably not a good idea as that'd just generate more confusion. We already use inference to determine which |
I've moved this over to rust-lang/rfcs#1555. |
In C++, you are able to pass a non-capturing lambda as a function pointer, as, for example, a function argument:
It would be incredibly convenient for rust to be able to do the same:
A use case would be a static (const) array of function pointers, where dynamic dispatch is not necessary.
Example in current rust (this is a compilable example):
This obviously relies upon dynamic dispatch to a level that rust is unable to optimize away. Ideally it would be possible to use a table of
fn()
s instead of&'static Fn()
and rust would be able to coerce the closures into function pointers because nothing needs to be captured.Another, possibly more widely-applicable scenario is simply being able to pass a closure into a C function that takes a callback.
The text was updated successfully, but these errors were encountered: