From c0541153889556bc3c0bee8dff4dc8ddb61d4520 Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Mon, 7 Jul 2025 21:16:51 +0200 Subject: [PATCH 1/6] add field projections goal --- src/2025h2/field-projections.md | 98 +++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/2025h2/field-projections.md diff --git a/src/2025h2/field-projections.md b/src/2025h2/field-projections.md new file mode 100644 index 00000000..5717c654 --- /dev/null +++ b/src/2025h2/field-projections.md @@ -0,0 +1,98 @@ +# Design a language feature to solve Field Projections + +| Metadata | | +|:-----------------|----------------------------------------------------------------------------------| +| Point of contact | @BennoLossin | +| Teams | | +| 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`, `Rc`, `Arc`), modified references (`&mut +MaybeUninit`, `Pin<&mut T>`) and custom pointer types (`NonNull`). + +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` +conceptually has fields of type `&mut MaybeUninit`. + +### 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`, +- `UnsafeCell`, +- `Cell` + +It also has several pointer-like types that could support a natural pointer-to-field operation. For +example: +- `NonNull`, +- `*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` + under the hood). +- `RcuMutex` a safe abstraction for RCU (a special synchronization primitive in the kernel) + working together with a `Mutex` to synchronize accesses to data (this requires complex + projections, only allowing certain fields to be projected). +- `SeqLockRef<'_, T>` +- `AtomicPtr` where `T` is a small enough type composed of integers. +- `UserPtr` 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` 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`. + +## 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] | | +| 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 + From 4a6db0ee26a503bd9843a7bbb0fd4be840f71d55 Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Fri, 18 Jul 2025 23:50:58 +0200 Subject: [PATCH 2/6] fix status --- src/2025h2/field-projections.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/2025h2/field-projections.md b/src/2025h2/field-projections.md index 5717c654..89c64611 100644 --- a/src/2025h2/field-projections.md +++ b/src/2025h2/field-projections.md @@ -5,7 +5,7 @@ | Point of contact | @BennoLossin | | Teams | | | Task owners | | -| Status | Proposed, Invited | +| Status | Proposed | | Tracking issue | | | Zulip channel | N/A | From 679269c719ce123708b036bc247d35af80622a9b Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Sat, 19 Jul 2025 23:38:28 +0200 Subject: [PATCH 3/6] move lang team experiment developers & rfc authors to notes --- src/2025h2/field-projections.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/2025h2/field-projections.md b/src/2025h2/field-projections.md index 89c64611..7effd9ea 100644 --- a/src/2025h2/field-projections.md +++ b/src/2025h2/field-projections.md @@ -87,8 +87,8 @@ Have field projections available in stable Rust. | 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] | | -| Author RFC | @BennoLossin | | +| Lang-team experiment | ![Team][] [lang] | @dingxiangfei2009, @BennoLossin | +| Author RFC | ![Team][] [lang] | @BennoLossin | | Lang-team champion | ![Team][] [lang] | *Champion Needed* | | Secondary RFC review | ![Team][] [types] | might be a good idea? | | RFC decision | ![Team][] [lang] | | From b44cdc87ba0468f58bf40ad481f9582c3b1b3c2f Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Sun, 20 Jul 2025 00:01:54 +0200 Subject: [PATCH 4/6] add current proposals & historical context --- src/2025h2/field-projections.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/2025h2/field-projections.md b/src/2025h2/field-projections.md index 7effd9ea..8f522223 100644 --- a/src/2025h2/field-projections.md +++ b/src/2025h2/field-projections.md @@ -67,6 +67,16 @@ Note that the projections listed above are also very important to Rust for Linux 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` instead if they had better field access. +#### Current proposals + +In addition to [Field Projections RFC v2] already mentioned above, there is a [newer +proposal](https://hackmd.io/@BennoLossin/HkMBy6Hzlx) that improves upon it. + +For historical context, there also is the [Field Projections RFC v1]. + +[Field Projections RFC v1]: https://github.com/rust-lang/rfcs/pull/3318 +[Field Projections RFC v2]: https://github.com/rust-lang/rfcs/pull/3735 + ### The next 6 months Have design meetings with the relevant parties & update the existing or write a new RFC. From 2f6a5d44fa1ca8ee921789fb7e24ac7487bf464b Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Sun, 20 Jul 2025 00:39:31 +0200 Subject: [PATCH 5/6] fix author rfc --- src/2025h2/field-projections.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/2025h2/field-projections.md b/src/2025h2/field-projections.md index 8f522223..de126b9e 100644 --- a/src/2025h2/field-projections.md +++ b/src/2025h2/field-projections.md @@ -98,7 +98,7 @@ Have field projections available in stable Rust. |----------------------|-------------------------------------|---------------------------------------------------------------------| | Design meeting | ![Team][] [lang] | Possibly more than one required as well as discussions on zulip. | | Lang-team experiment | ![Team][] [lang] | @dingxiangfei2009, @BennoLossin | -| Author RFC | ![Team][] [lang] | @BennoLossin | +| Author RFC | @BennoLossin | | | Lang-team champion | ![Team][] [lang] | *Champion Needed* | | Secondary RFC review | ![Team][] [types] | might be a good idea? | | RFC decision | ![Team][] [lang] | | From 9f3b9b3298ce823da7784095d99794b1832526da Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Sun, 20 Jul 2025 00:42:07 +0200 Subject: [PATCH 6/6] fix secondary review --- src/2025h2/field-projections.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/2025h2/field-projections.md b/src/2025h2/field-projections.md index de126b9e..48c1171e 100644 --- a/src/2025h2/field-projections.md +++ b/src/2025h2/field-projections.md @@ -100,7 +100,7 @@ Have field projections available in stable Rust. | Lang-team experiment | ![Team][] [lang] | @dingxiangfei2009, @BennoLossin | | Author RFC | @BennoLossin | | | Lang-team champion | ![Team][] [lang] | *Champion Needed* | -| Secondary RFC review | ![Team][] [types] | might be a good idea? | +| RFC secondary review | ![Team][] [types] | might be a good idea? | | RFC decision | ![Team][] [lang] | |