-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Expand implicit conversion of functions to function pointers #58078
Comments
Can someone tell if this change would require a RFC? |
I still like some feedback on if this is wanted or not. If I could get some guidance I'm willing to write a pr for it. |
There's one more point to add. Let's rename // doesn't work
add1.call_fn(5);
// works, but at what cost
(add1 as fn(_) -> _).call_fn(5); I have the same problem, which I'm struggling with. Current coercion rules make APIs based on functions as first-class citizens barely usable. PR would be probably very welcome. Unfortunately I can't say how hard will it be to actually implement if possible at all. I think that core problem is that Rust makes only 1-step coercions for non-deref coercions. Fn items can coerce to fn pointers, fn pointers can coerce to trait implementors, but fn items can't make 2 steps and coerce to trait implementor directly. If that's really the case, #18602 should solve it. |
I think it's more that trait resolution doesn't fall back to consider implementations for types you could coerce to (no function items implement your trait.) It's not specific to function items and function pointers; compare here, where no arrays implement the trait. For the function item / function pointer example, this change -impl MyFn for fn(usize) -> usize {
+impl<F> MyFn for F where F: Fn(usize) -> usize { Implements the trait for function items as well, allowing the un-casted version to compile. |
This doesn't work once the arguments or return types are also generic due to "unused generic" errors. Otherwise it would be my preferred implementation as well. |
(I'm not sure if this requires a RFC, if so please let me know.)
Allow implicit conversion of functions to function pointers when trait is implemented on a function pointer. The code below shows the problem I'm having.
Calling
accepts_fn
works fine today, however callingaccepts_my_fn
doesn't work, even though theMyFn
trait is implementation for function pointers. To make it work a explicit conversion (as fn(_) -> _
) is required. This seems a bit surprising that in one instance a function in implicitly converted and another instance it is not.(playground)
The text was updated successfully, but these errors were encountered: