-
Notifications
You must be signed in to change notification settings - Fork 65
Propose goal: Field Projections #329
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
BennoLossin
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
BennoLossin:field-projection-2025h2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,98 @@ | ||
# Design a language feature to solve Field Projections | ||
|
||
| Metadata | | | ||
|:-----------------|----------------------------------------------------------------------------------| | ||
| Point of contact | @BennoLossin | | ||
| Teams | <!-- TEAMS WITH ASKS --> | | ||
| Task owners | <!-- TASK OWNERS --> | | ||
| Status | Proposed, Invited | | ||
| Tracking issue | | | ||
| Zulip channel | N/A | | ||
|
||
## Summary | ||
|
||
Figure out the best design for field projections. Update the existing [Field Projections RFC] or | ||
author a new one and implement it for use in nightly via a lang experiment. | ||
|
||
[Field Projections RFC]: https://github.com/rust-lang/rfcs/pull/3735 | ||
|
||
## Motivation | ||
|
||
Rust makes extensive use of smart pointers (`Box<T>`, `Rc<T>`, `Arc<T>`), modified references (`&mut | ||
MaybeUninit<T>`, `Pin<&mut T>`) and custom pointer types (`NonNull<T>`). | ||
|
||
Some of these types implement the `Deref[Mut]` trait(s) allowing one to access fields of the type | ||
`T`. But not all of them can implement it due to various reasons. However, they often *can* support | ||
operations that "index" into the fields of the type `T`. For example `&mut MaybeUninit<Struct>` | ||
conceptually has fields of type `&mut MaybeUninit<Field>`. | ||
|
||
### The status quo | ||
|
||
Rust has a lot of container types that make it difficult to directly interact with fields of structs | ||
that they wrap. For example: | ||
- `MaybeUninit<T>`, | ||
- `UnsafeCell<T>`, | ||
- `Cell<T>` | ||
|
||
It also has several pointer-like types that could support a natural pointer-to-field operation. For | ||
example: | ||
- `NonNull<T>`, | ||
- `*const T` / `*mut T`, | ||
- `cell::Ref<'_, T>` / `cell::RefMut<'_, T>` | ||
|
||
Additionally, there is `Pin<&mut T>`, which already has a well-established name for this operation: | ||
pin-projections. The ecosystem provides several crates to add this operation to the struct itself. | ||
|
||
#### Custom types | ||
|
||
A plethora of types making use of field projections are found in the context of Rust for Linux. | ||
Therefore they might -- with high probability -- come up in other embedded projects too. | ||
|
||
- `VolatilePtr<'a, T>` like `*mut T`, but with a lifetime & all accesses to the pointer are | ||
volatile. | ||
- `Ptr<'a, T>` like a `&'a T` but without certain rust guarantees (most likely `&'a UnsafePinned<T>` | ||
under the hood). | ||
- `RcuMutex<T>` a safe abstraction for RCU (a special synchronization primitive in the kernel) | ||
working together with a `Mutex<T>` to synchronize accesses to data (this requires complex | ||
projections, only allowing certain fields to be projected). | ||
- `SeqLockRef<'_, T>` | ||
- `AtomicPtr<T>` where `T` is a small enough type composed of integers. | ||
- `UserPtr<T>` a pointer into userspace | ||
|
||
Additionally, Rust for Linux could take advantage of field information present in the current | ||
proposal. Essentially answering the question "does this type have a field of type X at offset Y?" | ||
via traits. | ||
|
||
Note that the projections listed above are also very important to Rust for Linux. Virtually all | ||
types are pinned in the kernel, so `Pin<&mut T>` comes up a lot in drivers. We're also handling raw | ||
pointers very often where we could use `NonNull<T>` instead if they had better field access. | ||
|
||
### The next 6 months | ||
|
||
Have design meetings with the relevant parties & update the existing or write a new RFC. | ||
|
||
### The "shiny future" we are working towards | ||
|
||
Have field projections available in stable Rust. | ||
|
||
## Design axioms | ||
|
||
- **Effortless Syntax.** Using field projections in a non-generic context should look very similar | ||
to normal field accesses. | ||
- **Broad Solution.** Field projections should be very general and solve complex projection problems | ||
such as pin-projections and `RcuMutex<T>`. | ||
|
||
## Ownership and team asks | ||
|
||
| Task | Owner(s) or team(s) | Notes | | ||
|----------------------|-------------------------------------|---------------------------------------------------------------------| | ||
| Design meeting | ![Team][] [lang] | Possibly more than one required as well as discussions on zulip. | | ||
| Lang-team experiment | @dingxiangfei2009, @BennoLossin, ![Team][] [lang] | | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure exactly what @nikomatsakis envisioned here when implementing the checks, but this will fail CI tooling as it will think you two are also teams. Likely this check should be improved on our side ^^. |
||
| Author RFC | @BennoLossin | | | ||
| Lang-team champion | ![Team][] [lang] | *Champion Needed* | | ||
| Secondary RFC review | ![Team][] [types] | might be a good idea? | | ||
| RFC decision | ![Team][] [lang] | | | ||
|
||
|
||
## Frequently asked questions | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since invited goals don't have persons who signed up to do the work yet, and it seems that that person will be you: