Re: [PATCH] rust: pin-init: document self-referential feature
From: Alice Ryhl
Date: Thu Nov 26 2026 - 03:14:02 EST
On Tue, Nov 24, 2026 at 10:16:23PM +0000, Gary Guo wrote:
> +/// When initializing such a struct with [`pin_init!`], every named initialized field have a
> +/// binding generated for it (as `&T`/`Pin<&T>` for fields share-borrowed, or `&mut`/`Pin<&mut T>`
> +/// for fields mutably-borrowed), so a field can borrow from the fields initialized before it:
A couple of minor typos here:
- "have a" -> "has a"
- "share-borrowed" -> "shared-borrowed" (to match "Mutable Borrows" below)
- "`&mut`/`Pin<&mut T>`" -> "`&mut T`/`Pin<&mut T>`"
> +/// Note that `&mut`/`Pin<&mut T>` bindings are also generated for non-borrowed field, but access to
> +/// them are limited to within the initializer.
Same here:
- "`&mut`/`Pin<&mut T>`" -> "`&mut T`/`Pin<&mut T>`"
- "non-borrowed field" -> "non-borrowed fields"
- "access to them are" -> "access to them is"
> +/// The macro verifies that captures marked covariant really are covariant. `#[uses]` accepts a
> +/// comma-separated list of lifetimes, each with an optional variance (the default being
> +/// `covariant`). The special lifetime `'_` stands for all field lifetimes mentioned in the type
> +/// of the field, e.g. `#[uses('_: invariant)]`.
I think `'_` also applies to existential lifetimes mentioned in the
field type, not just field lifetimes.
> +/// An existential lifetime is not a parameter of the struct, so just like a field lifetime it is
> +/// hidden from the type of the struct. Unlike a field lifetime, it is not tied to a field; the
> +/// only bounds placed on it are those written in the where clause (here `'x: 'a`).
I think it'd be useful to explicitly say:
are those written in the where clause (here `'x: 'a`, i.e. 'x is
shorter than 'a).
> +/// - `project(self: Pin<&mut Self>)` — the pin projection: `#[pin]` fields are projected as
> +/// `Pin<&mut T>`, all other fields as `&mut T`. Fields that capture a field lifetime covariantly
> +/// are only projected as (possibly pinned) shared references; fields with invariant field
> +/// lifetime capture are omitted entirely.
> +/// - `with_project(self: Pin<&mut Self>, f)` — calls `f` with a projection in which every
> +/// visible field has its actual field lifetime, so even invariant captures are visible.
> +/// - `with_project_ref(self: Pin<&Self>, f)` — the shared version of `with_project`.
> +///
> +/// For all projections, shared borrow fields are only accessible with shared references, and
> +/// mutably borrowed fields are omitted (they are only accessible through the fields that borrow
> +/// them).
"shared borrow fields" -> "shared-borrowed fields" (to match "mutably
borrowed fields" and avoid sounding like fields that hold a borrow).
> +/// The fields are initialized in the order that they appear in the initializer. A let binding
> +/// is generated so already initialized fields can be read. For [self-referential
> +/// structs](pin_data#self-referential-structs), you may also use such generated bindings to
> +/// initialize fields that borrow from them.
`init!` just below still has `- no references to fields are allowed to
be created inside of the initializer.` even though `init!` generates the
same `let` bindings for named fields.
Alice