Design a language feature to solve Field Projections (original) (raw)

Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust Project Goals

Design a language feature to solve Field Projections

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.

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 typeT. 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:

It also has several pointer-like types that could support a natural pointer-to-field operation. For example:

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.

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.

Current proposals

In addition to Field Projections RFC v2 already mentioned above, there is a newer proposalthat improves upon it.

For historical context, there also is the Field Projections RFC v1.

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

Ownership and team asks

Frequently asked questions

Why not push for RFC decision?

The design is still too early to expect a final decision on the RFC.