Skip to content

Documentation unclear with Fn trait on extern "C" function pointers #69250

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

Open
MarkMcCaskey opened this issue Feb 18, 2020 · 2 comments
Open
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-FFI Area: Foreign function interface (FFI) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team

Comments

@MarkMcCaskey
Copy link
Contributor

MarkMcCaskey commented Feb 18, 2020

The relevant docs are here and here in the repo:

In addition, function pointers of any signature, ABI, or safety are Copy, and all safe function pointers implement Fn, FnMut, and FnOnce. This works because these traits are specially known to the compiler.

The code:

extern "C" fn abc(x: u32) {
    println!("Hello, {}", x);
}

fn do_it<F: Fn(u32)>(f: F, n: u32) {
    f(n)
}

fn main() {
    do_it(abc, 5);
}

Does not compile even though abc can be called without unsafe, which the docs seem to suggest is sufficient for this to work.

Rust playground demonstrating the issue

I'd be happy to update the docs, but unfortunately I think I don't understand why this doesn't work well enough to do so.

@jonas-schievink jonas-schievink added A-FFI Area: Foreign function interface (FFI) C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools labels Feb 18, 2020
@jonas-schievink jonas-schievink added the T-lang Relevant to the language team label Mar 6, 2020
@josephlr
Copy link
Contributor

See #40710

@hkBst
Copy link
Member

hkBst commented Feb 6, 2025

#40710 now works, but this case still doesn't, so there is some difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-FFI Area: Foreign function interface (FFI) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team
Projects
None yet
Development

No branches or pull requests

4 participants