corrode
corrode copied to clipboard
implicitly construct/dereference function pointers
In C, a function call looks like this:
int putchar(int);
putchar('A');
Indirecting through a function pointer looks like this:
int (*f)(int) = &putchar;
(*f)('B');
But it can also look like this:
int (*g)(int) = putchar;
g('C');
Notably, C automatically dereferences a function pointer if it's called, and it automatically takes the address of a function if it's used where a function pointer is expected.
In Rust, function items have a type such as fn(i32) -> i32
, which is a kind of pointer. So first we need to translate C types like int (*)(int)
to that Rust type, not to *const fn(i32) -> i32
or something.
Then we need two special cases in expression evaluation:
- Taking the address of an expression that has function type should be a no-op.
- Dereferencing an expression that has function type should be a no-op.
Here's an example Rust program which calls the C standard library's qsort
function, passing it a callback which is implemented in Rust: https://is.gd/QLJxHh