-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: result_ffi_guarantees #3391
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4b5a891
rfc text
Lokathor f647e37
restrict the ZST rules, provide further explanation.
Lokathor e729531
require that there NOT be the non_exhaustive attribute on the ZST
Lokathor 2c258cc
clarify that the condition list is an "all" list.
Lokathor e8ecdc4
restrict the guarantee to when the ZST has no fields at all.
Lokathor 40b65f3
missing comma
Lokathor 7320c01
Add RFC number and tracking issue
tmandry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# RFC: result_ffi_guarantees | ||
|
||
- Feature Name: `result_ffi_guarantees` | ||
- Start Date: 2023-02-15 | ||
- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000) | ||
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) | ||
|
||
# Summary | ||
[summary]: #summary | ||
|
||
This RFC gives specific layout and ABI guarantees when wrapping "non-zero" data types from `core` in `Option` or `Result`. This allows those data types to be used directly in FFI, in place of the primitive form of the data (eg: `Result<(), NonZeroI32>` instead of `i32`). | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
Rust often needs to interact with foreign code. However, foreign function type signatures don't normally support any of Rust's rich type system. Particular function inputs and outputs will simply use 0 (or null) as a sentinel value and the programmer has to remember when that's happening. | ||
|
||
Though it's common for "raw bindings" crates to also have "high level wrapper" crates that go with them (eg: `windows-sys`/`windows`, or `sdl2-sys`/`sdl2`, etc), someone still has to write those wrapper crates which use the foreign functions directly. Allowing Rust programmers to use more detailed types with foreign functions makes their work easier. | ||
|
||
# Guide-level explanation | ||
[guide-level-explanation]: #guide-level-explanation | ||
|
||
I'm not sure how to write a "guide" portion of this that's any simpler than the "reference" portion, which is already quite short. | ||
|
||
# Reference-level explanation | ||
[reference-level-explanation]: #reference-level-explanation | ||
|
||
When either of these two `core` types: | ||
|
||
* `Option<T>` | ||
* `Result<T, E>` where either `T` or `E` are a zero-sized type with alignment 1 (a "1-ZST"). | ||
|
||
Is combined with a non-zero or non-null type (see the chart), the combination has the same layout (size and alignment) and the same ABI as the primitive form of the data. | ||
|
||
| Example combined Type | Primitive Type | | ||
|:-|:-| | ||
| `Result<NonNull<T>, ()>` | `*mut T` | | ||
| `Result<&T, ()>` | `&T` | | ||
| `Result<&mut T, ()>` | `&mut T` | | ||
| `Result<fn(), ()>` | `fn()` | | ||
| `Result<NonZeroI8, ()>` | `i8` | | ||
| `Result<NonZeroI16, ()>` | `i16` | | ||
| `Result<NonZeroI32, ()>` | `i32` | | ||
| `Result<NonZeroI64, ()>` | `i64` | | ||
| `Result<NonZeroI128, ()>` | `i128` | | ||
| `Result<NonZeroIsize, ()>` | `isize` | | ||
| `Result<NonZeroU8, ()>` | `u8` | | ||
| `Result<NonZeroU16, ()>` | `u16` | | ||
| `Result<NonZeroU32, ()>` | `u32` | | ||
| `Result<NonZeroU64, ()>` | `u64` | | ||
| `Result<NonZeroU128, ()>` | `u128` | | ||
| `Result<NonZeroUsize, ()>` | `usize` | | ||
|
||
* While `fn()` is listed just once in the above table, this rule applies to all `fn` types (regardless of ABI, arguments, and return type). | ||
|
||
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
* The compiler has less flexibility with respect to discriminant computation and pattern matching optimizations when a type is niche-optimized. | ||
|
||
# Rationale and alternatives | ||
[rationale-and-alternatives]: #rationale-and-alternatives | ||
|
||
It's always possible to *not* strengthen the guarantees of the language. | ||
|
||
# Prior art | ||
[prior-art]: #prior-art | ||
|
||
The compiler already suports `Option` being combined with specific non-zero types, this RFC mostly expands the list of guaranteed support. | ||
|
||
# Unresolved questions | ||
[unresolved-questions]: #unresolved-questions | ||
|
||
None at this time. | ||
|
||
# Future possibilities | ||
[future-possibilities]: #future-possibilities | ||
|
||
* This could be expanded to include [ControlFlow](https://doc.rust-lang.org/nightly/core/ops/enum.ControlFlow.html) and [Poll](https://doc.rust-lang.org/nightly/core/task/enum.Poll.html). | ||
* This could be extended to *all* similar enums in the future. However, without a way to opt-in to the special layout and ABI guarantees (eg: a trait or attribute) it becomes yet another semver hazard for library authors. The RFC is deliberately limited in scope to avoid bikesheding. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.