-
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
Support coercing non-capturing closures to extern function pointers #44291
Comments
You don't. It basically only works for callback functions that take a data pointer (which many do). Then you can do something like this (highly unsafe): #![feature(fn_traits, unboxed_closures)]
fn closure_to_fn_and_data<A, F: Fn<A> + 'static>(
f: F,
) -> (
unsafe extern "C" fn(*const Box<Fn<A, Output = F::Output>>, A) -> F::Output,
*const Box<Fn<A, Output = F::Output>>,
) {
unsafe extern "C" fn callback<A, F: Fn<A> + 'static>(
closure: *const Box<Fn<A, Output = F::Output>>,
args: A,
) -> F::Output {
std::ops::Fn::call(&**closure, args)
}
(
callback::<A, F>,
Box::into_raw(Box::new(Box::new(f) as Box<Fn<A, Output = F::Output>>)) as _,
)
} |
@joshtriplett could you factor out this question from this issue? I'd like to comment on that as well but I dont want to pollute this discussion |
What happens if the same closure is coerced to both fn joshtriplett(hi: bool) {
let closure = || ();
if hi {
let _rust_fn: fn() = closure;
} else {
let _c_fn: extern fn() = closure;
}
} |
@dtolnay I'd expect a thin shim, and you'd get a duplicated closure body only if LLVM inlines it. |
Triage: I don't believe there's been any change here. I'd expect this to work: const foo: [extern fn(&mut u32); 1] = [
|var: &mut u32| {},
]; But it does not:
|
The PR has been closed due to inactivity so it is free if anyone wants to pick it up |
This is a followup to #39817 . Non-capturing closures now coerce to function pointers, but not to
extern fn
pointers, as provided to C functions expecting a callback. Adding support for this would make it much simpler to call a C function and provide an appropriate callback inline.(I'm also curious what it would take to make a capturing closure work as an extern function pointer, but that's a separate issue.)
The text was updated successfully, but these errors were encountered: