Skip to content
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

Add some missing FFI safe types #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/en/07_ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ The following types are considered C-compatible:
- `repr(C)`-annotated `struct`,
- `repr(C)` or `repr(Int)`-annotated `enum` with at least one variant and only
fieldless variants (where `Int` is an integral primitive type),
- pointers.
- pointers,
- an `Option<T>` where `T` is either
- `core::ptr::NonNull<U>` and `U` is a `Sized` C-compatible type, then it is
Comment on lines +133 to +135
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The part with the pointers (such as *mut U) forget to express the U is a Sized ... invariant that Option<ptr::NonNull<U>> do.

I'd weaken the C-compatible bound on U, but explicit the Sizedrequirement:

Suggested change
- pointers,
- an `Option<T>` where `T` is either
- `core::ptr::NonNull<U>` and `U` is a `Sized` C-compatible type, then it is
- pointers to some type `Pointee`, where:
- `Pointee : Sized`,
- `Pointee` is either a C-compatible type too (and only then can the pointer be dereferenced by the C side), or it is not, in which case it must be treated as a pointer to an opaque object (_c.f._, the dedicated section \[FIXME:add link\]).
- an `Option<T>` where `T` is either
- `core::ptr::NonNull<Pointee>` where `Pointee` meets the constraints mentioned for the pointer case.

compatible to a `*const T` and `*mut T` pointer;
- `core::num::NonZero*`, then is compatible to the corresponding integral
primitive type;
- a `repr(transparent)`-annotated `struct` with only one field, where that
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're allowed to have more than one field if one is a align-1 ZST. This is common for PhantomData, for example.

Copy link
Author

@HeroicKatora HeroicKatora Jun 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're even allowed to have an arbitrary amount of align-1 ZSTs, however this seemed very similar to the ZST restrictions below. The list style itself is not exactly exhaustive in either case. I have not yet seen any such use in ffi in the wild whereas I did find use for all other cases that I've listed.

field has a C-compatible type.

The following types are not C-compatible:

Expand Down