Convert a function type to a function pointer type

I am implementing a "typed" version of the java native interface. For that, the library uses an automatically generated string signature from function type. But I am facing an ergonomic issue that is due to the fact that I do not know if there is a way to turn a "specific" function type into a function pointer type:

trait X{}

impl<R> X for fn()->R {
impl<R,A> X for fn(A)->R {
// ...implement for n parameter until n is sufficiently large

fn f(){}

fn test<T:X>(_:T){}

fn main() {
    test(f); // Compiler Error: `X is not implemented for "fn() {f}"
    let a: fn() = f; //"fn() {f}" coerced to "fn()"
    test(a); // Compiler fine

The line test(f) does not compile because the type of f is not a function pointer but a type that denotes the function named f (a type that only has one instance:f). I do not want to use the explicit "function" to "function pointer" coercion as is done by let a: fn() = f. How can I do that? Is there a standard library function to_function_pointer(f)?

test(a as fn());

I don't want that the user specifies the function type! I don't want something as `32i32 as i32 + 42i32 as i32' I probably have to write an issue or a PR more documented!

1 Like

See #58078.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.