Open mineichen opened 2 years ago
So you're basically implementing closures manually so that they're FFI-safe? I think the only way to do this would be with a proc macro, right? Specially because we'd need variadic params in Fn's generics. But it might be a bit complicated, you'd have to know what variables are captured and etc. We could also use a hack like for tuples.
Because "extern C fn" can be instrumented with generics within the from
implementation, we don't need any macros. Just some pointer casts. You can find a working example here
I don't know if we have to implement variadic functions. For multiple parameters we could implement:
impl<T, TParam1, TParam2, TResult> From<T> for RBoxFnOnce<Tuple2<TParam1, TParam2>, TResult> where T: FnOnce(TParam1, TParam2) -> TResult;
@mineichen FWIW I did this as well, and though it's a bit cumbersome I had the same approach for multiple parameters (using tuples) and couldn't think of anything much better.
I'm currently missing a stable_abi alternative for Fn, FnMut and FnOnce. I'm planning to implement those in the following style:
Usage looks like:
I'm not very experienced, but IMO this shouldn't have undefined behavior. If you are interested to integrate such functionality into stable_abi, I'm happy to fork your repo and push everything upstream eventually. Otherwise, I'll just create my own repo. Please let me know about your preferences.