Skip to content

Commit ebe0302

Browse files
authored
Propose goal: Field Projections (#329)
* add field projections goal * fix status * move lang team experiment developers & rfc authors to notes * add current proposals & historical context * fix author rfc * fix secondary review
1 parent d030e99 commit ebe0302

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

src/2025h2/field-projections.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Design a language feature to solve Field Projections
2+
3+
| Metadata | |
4+
|:-----------------|----------------------------------------------------------------------------------|
5+
| Point of contact | @BennoLossin |
6+
| Teams | <!-- TEAMS WITH ASKS --> |
7+
| Task owners | <!-- TASK OWNERS --> |
8+
| Status | Proposed |
9+
| Tracking issue | |
10+
| Zulip channel | N/A |
11+
12+
## Summary
13+
14+
Figure out the best design for field projections. Update the existing [Field Projections RFC] or
15+
author a new one and implement it for use in nightly via a lang experiment.
16+
17+
[Field Projections RFC]: https://github.com/rust-lang/rfcs/pull/3735
18+
19+
## Motivation
20+
21+
Rust makes extensive use of smart pointers (`Box<T>`, `Rc<T>`, `Arc<T>`), modified references (`&mut
22+
MaybeUninit<T>`, `Pin<&mut T>`) and custom pointer types (`NonNull<T>`).
23+
24+
Some of these types implement the `Deref[Mut]` trait(s) allowing one to access fields of the type
25+
`T`. But not all of them can implement it due to various reasons. However, they often *can* support
26+
operations that "index" into the fields of the type `T`. For example `&mut MaybeUninit<Struct>`
27+
conceptually has fields of type `&mut MaybeUninit<Field>`.
28+
29+
### The status quo
30+
31+
Rust has a lot of container types that make it difficult to directly interact with fields of structs
32+
that they wrap. For example:
33+
- `MaybeUninit<T>`,
34+
- `UnsafeCell<T>`,
35+
- `Cell<T>`
36+
37+
It also has several pointer-like types that could support a natural pointer-to-field operation. For
38+
example:
39+
- `NonNull<T>`,
40+
- `*const T` / `*mut T`,
41+
- `cell::Ref<'_, T>` / `cell::RefMut<'_, T>`
42+
43+
Additionally, there is `Pin<&mut T>`, which already has a well-established name for this operation:
44+
pin-projections. The ecosystem provides several crates to add this operation to the struct itself.
45+
46+
#### Custom types
47+
48+
A plethora of types making use of field projections are found in the context of Rust for Linux.
49+
Therefore they might -- with high probability -- come up in other embedded projects too.
50+
51+
- `VolatilePtr<'a, T>` like `*mut T`, but with a lifetime & all accesses to the pointer are
52+
volatile.
53+
- `Ptr<'a, T>` like a `&'a T` but without certain rust guarantees (most likely `&'a UnsafePinned<T>`
54+
under the hood).
55+
- `RcuMutex<T>` a safe abstraction for RCU (a special synchronization primitive in the kernel)
56+
working together with a `Mutex<T>` to synchronize accesses to data (this requires complex
57+
projections, only allowing certain fields to be projected).
58+
- `SeqLockRef<'_, T>`
59+
- `AtomicPtr<T>` where `T` is a small enough type composed of integers.
60+
- `UserPtr<T>` a pointer into userspace
61+
62+
Additionally, Rust for Linux could take advantage of field information present in the current
63+
proposal. Essentially answering the question "does this type have a field of type X at offset Y?"
64+
via traits.
65+
66+
Note that the projections listed above are also very important to Rust for Linux. Virtually all
67+
types are pinned in the kernel, so `Pin<&mut T>` comes up a lot in drivers. We're also handling raw
68+
pointers very often where we could use `NonNull<T>` instead if they had better field access.
69+
70+
#### Current proposals
71+
72+
In addition to [Field Projections RFC v2] already mentioned above, there is a [newer
73+
proposal](https://hackmd.io/@BennoLossin/HkMBy6Hzlx) that improves upon it.
74+
75+
For historical context, there also is the [Field Projections RFC v1].
76+
77+
[Field Projections RFC v1]: https://github.com/rust-lang/rfcs/pull/3318
78+
[Field Projections RFC v2]: https://github.com/rust-lang/rfcs/pull/3735
79+
80+
### The next 6 months
81+
82+
Have design meetings with the relevant parties & update the existing or write a new RFC.
83+
84+
### The "shiny future" we are working towards
85+
86+
Have field projections available in stable Rust.
87+
88+
## Design axioms
89+
90+
- **Effortless Syntax.** Using field projections in a non-generic context should look very similar
91+
to normal field accesses.
92+
- **Broad Solution.** Field projections should be very general and solve complex projection problems
93+
such as pin-projections and `RcuMutex<T>`.
94+
95+
## Ownership and team asks
96+
97+
| Task | Owner(s) or team(s) | Notes |
98+
|----------------------|-------------------------------------|---------------------------------------------------------------------|
99+
| Design meeting | ![Team][] [lang] | Possibly more than one required as well as discussions on zulip. |
100+
| Lang-team experiment | ![Team][] [lang] | @dingxiangfei2009, @BennoLossin |
101+
| Author RFC | @BennoLossin | |
102+
| Lang-team champion | ![Team][] [lang] | *Champion Needed* |
103+
| RFC secondary review | ![Team][] [types] | might be a good idea? |
104+
| RFC decision | ![Team][] [lang] | |
105+
106+
107+
## Frequently asked questions
108+

0 commit comments

Comments
 (0)