Skip to content

Clarify FFI-safety of Option<extern "C" fn> #59

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

Closed
ramosbugs opened this issue Mar 10, 2018 · 3 comments
Closed

Clarify FFI-safety of Option<extern "C" fn> #59

ramosbugs opened this issue Mar 10, 2018 · 3 comments

Comments

@ramosbugs
Copy link
Contributor

Relatively new to Rust, and reading through the Nomicon to make sure I build a new FFI module safely. I noticed one omission in https://github.com/rust-lang-nursery/nomicon/blob/master/src/other-reprs.md.

#13 added language to call out that Option<&T> is FFI-safe as long as *const T is FFI-safe. Based on rust-lang/rust#40913, it seems that the preferred way of expressing nullable function pointers (returned via FFI) is using Option<fn> (or I guess Option<extern "C" fn> since function pointers set by C code are presumably also C-ABI).

If my understanding is correct, shall we add this to the Nomicon? I'm happy to send a PR if that's desired.

Also, are Option<&T> and Option<extern "C" fn> an exhaustive list of repr(C) FFI-safe enums with fields, or are there other exceptions to document as well?

@ramosbugs
Copy link
Contributor Author

Actually, it looks like this is covered by the nullable pointer optimization section, which specifically mentions function pointers (along with &T, &mut T, and boxes). I wonder if it's worth including a forward pointer to the more comprehensive section in the bullet added in #13 (cc: @geofft).

@Gankra
Copy link
Contributor

Gankra commented Mar 12, 2018

Being more clear shouldn't hurt here.

@Gankra
Copy link
Contributor

Gankra commented Mar 12, 2018

If possible the clarified language on other-reprs should link to the nullable pointer optimization. I'm thinking the wording should be something like:

If T is an FFI-safe non-nullable pointer type, Option<T> is guaranteed to have the same layout and ABI as T and is therefore also FFI-safe. As of this writing, this covers &, &mut, and function pointers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants