Skip to contentSkip to conversation

rust: pin-init: document self-referential feature

PATCH Superseded Gary Guo 4 participants 4 messages Nov 24 – Dec 1

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gary Guo

author
#0 lore raw

PATCH 1/1 rust: pin-init: document self-referential feature

From: Gary Guo <[email protected]>

Add a section to the documentation of #[pin_data] macro about the safe self-referential creation feature.

---
 rust/pin-init/src/lib.rs | 176 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 171 insertions(+), 5 deletions(-)
rust/pin-init/src/lib.rs +171 -5
index 9ffa76434310..c0e87586b072 100644
@@ -307,6 +307,9 @@
 /// Tuple structs are supported as well. Their fields have no names, so the generated projection
 /// is a tuple struct too and its fields are accessed by index.
 ///
+/// Self-referential structs, where a field borrows another field of the same struct, are also
+/// supported. See the [section on self-referential structs](#self-referential-structs) below.
+///
 /// If your `struct` implements `Drop`, then you need to add `PinnedDrop` as arguments to this
 /// macro, and change your `Drop` implementation to `PinnedDrop` annotated with
 /// `#[`[`macro@pinned_drop`]`]`, since dropping pinned values requires extra care.
@@ -376,6 +379,170 @@
 ///     }
 /// }
 /// ```
+///
+/// # Self-Referential Structs
+///
+/// This macro also supports structs where a field borrows another field of the same struct. To do
+/// that, use the name of the borrowed field as a lifetime (a *field lifetime*) in the type of the
+/// borrowing field:
+///
+/// ```
+/// # use pin_init::*;
+/// #[pin_data]
+/// struct SelfRef {
+///     part: &'msg str,
+///     msg: String,
+/// }
+/// ```
+///
+/// Here the field `part` captures the field lifetime `'msg` and thereby borrows the field `msg`.
+/// A field may only borrow from fields declared after it: fields are dropped in declaration
+/// order, so the borrowing field is then always dropped before the field it borrows. Structs
+/// with borrowed fields or captured lifetimes do not implement `Unpin`.
+///
+/// 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:
+///
+/// ```
+/// # use pin_init::*;
+/// # #[pin_data]
+/// # struct SelfRef {
+/// #     part: &'msg str,
+/// #     msg: String,
+/// # }
+/// stack_pin_init!(let foo = pin_init!(SelfRef {
+///     msg: "hello world".to_owned(),
+///     part: &msg[..5],
+/// }));
+/// assert_eq!(*foo.part(), "hello");
+/// ```
+///
+/// Note that `&mut`/`Pin<&mut T>` bindings are also generated for non-borrowed field, but access to
+/// them are limited to within the initializer.
+///
+/// ## Mutable Borrows
+///
+/// A field is only shared-borrowed by default. To also allow `&mut` borrows, mark the borrowed
+/// field with `#[borrowed(mut)]`:
+///
+/// ```
+/// # use pin_init::*;
+/// #[pin_data]
+/// struct MutSelfRef {
+///     part: &'mut_msg mut str,
+///     #[borrowed(mut)]
+///     mut_msg: String,
+/// }
+///
+/// stack_pin_init!(let foo = pin_init!(MutSelfRef {
+///     mut_msg: "hello world".to_owned(),
+///     part: &mut mut_msg[..5],
+/// }));
+/// foo.as_mut().with_project(|proj| proj.part.make_ascii_uppercase());
+/// assert_eq!(*foo.part(), "HELLO");
+/// ```
+///
+/// Shared borrows are inferred automatically whenever a field lifetime is used, but they can also
+/// be requested explicitly with `#[borrowed]`.
+///
+/// ## Variance
+///
+/// A captured field lifetime is *covariant* by default: just like a normal reference, the field
+/// type allows the captured lifetime to be shortened. If the field type does not allow that (for
+/// example because the borrow is stored behind a `Mutex`), the capture has to be marked as
+/// *invariant* using the `#[uses]` attribute:
+///
+/// ```
+/// # use pin_init::*;
+/// # use std::sync::Mutex;
+/// #[pin_data]
+/// struct InvariantSelfRef {
+///     #[uses('owner: invariant)]
+///     part: Mutex<&'owner str>,
+///     owner: String,
+/// }
+/// ```
+///
+/// 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)]`.
+///
+/// ## Existential Lifetimes
+///
+/// A field lifetime ties a borrow to a specific field. Sometimes a field should instead borrow
+/// from something outside the struct that merely outlives one of the struct's lifetime
+/// parameters. Such lifetimes are declared in the where clause of the struct using `exists`:
+///
+/// ```
+/// # use pin_init::*;
+/// # use std::marker::PhantomData;
+/// # use std::sync::Mutex;
+/// struct CoherentBox<'a>(PhantomData<&'a ()>);
+///
+/// #[pin_data]
+/// struct DeviceBox<'a>
+/// where
+///     exists<'x>: 'a,
+/// {
+///     #[uses('x: invariant)]
+///     m: Mutex<CoherentBox<'x>>,
+///     p: PhantomData<&'a ()>,
+/// }
+///
+/// // The struct is covariant in `'a`, so it can be shortened:
+/// fn shorten<'long: 'short, 'short>(f: DeviceBox<'long>) -> DeviceBox<'short> {
+///     f
+/// }
+/// # let _ = shorten;
+/// ```
+///
+/// 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`).
+///
+/// ## Projections and Accessors
+///
+/// Since field lifetimes are not parameters of the struct, borrows of the struct cannot carry
+/// them. This macro therefore generates several methods for accessing the fields:
+///
+/// - `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).
+///
+/// Every field (except those borrowed mutably) that captures field lifetimes also gets an accessor
+/// method, because they cannot be directly accessed. `fn field(&self)` is generated if all of its
+/// captures are covariant, otherwise `fn with_field(&self, f)` is generated where `f` is called
+/// with the field at its captured lifetimes.
+///
+/// ```
+/// # use pin_init::*;
+/// # #[pin_data]
+/// # struct SelfRef {
+/// #     part: &'msg str,
+/// #     msg: String,
+/// # }
+/// # stack_pin_init!(let foo = pin_init!(SelfRef {
+/// #     msg: "hello world".to_owned(),
+/// #     part: &msg[..5],
+/// # }));
+/// foo.as_mut().with_project(|proj| {
+///     *proj.part = &proj.msg[6..];
+/// });
+/// assert_eq!(*foo.part(), "world");
+/// foo.as_ref().with_project_ref(|proj| {
+///     assert_eq!(*proj.part, "world");
+/// });
+/// ```
 pub use ::pin_init_internal::pin_data;
 
 /// Used to implement `PinnedDrop` safely.
@@ -664,11 +831,10 @@ macro_rules! stack_try_pin_init {
 ///
 /// Arbitrary Rust expressions can be used to set the value of a variable.
 ///
-/// The fields are initialized in the order that they appear in the initializer. So it is possible
-/// to read already initialized fields using raw pointers.
-///
-/// IMPORTANT: You are not allowed to create references to fields of the struct inside of the
-/// initializer.
+/// 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-functions
 ///

---
base-commit: 5eb5b404e6e7631a986333f00e7b3b18aafcfaa8
change-id: 20261124-dev-pin-init-doc-c488879a1c79

Best regards,

Signature · 1 line
Gary Guo <[email protected]>
reviewer
#1 lore raw

rust/pin-init/src/lib.rs line 405

Quoted 3 lines · Gary Guo
rust/pin-init/src/lib.rs +3 -0
+/// 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>"

rust/pin-init/src/lib.rs line 422

Quoted 2 lines
rust/pin-init/src/lib.rs +2 -0
+/// 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"

rust/pin-init/src/lib.rs line 470

Quoted 4 lines
rust/pin-init/src/lib.rs +4 -0
+/// 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.

rust/pin-init/src/lib.rs line 503

Quoted 3 lines
rust/pin-init/src/lib.rs +3 -0
+/// 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).

rust/pin-init/src/lib.rs line 520

Quoted 11 lines
rust/pin-init/src/lib.rs +11 -0
+/// - `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).

rust/pin-init/src/lib.rs macro_rules! stack_try_pin_init { · line 837

Quoted 4 lines
rust/pin-init/src/lib.rs +4 -0
+/// 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

#2 lore raw
In reply to Gary Guo
Quoted 4 lines · Gary Guo

From: Gary Guo <[email protected]>

Add a section to the documentation of #[pin_data] macro about the safe self-referential creation feature.

Thx for writing this, that's very useful. I read through it. Just some comments below:

rust/pin-init/src/lib.rs line 401

Quoted 4 lines
rust/pin-init/src/lib.rs +4 -0
+/// Here the field `part` captures the field lifetime `'msg` and thereby borrows the field `msg`.
+/// A field may only borrow from fields declared after it: fields are dropped in declaration
+/// order, so the borrowing field is then always dropped before the field it borrows. Structs
+/// with borrowed fields or captured lifetimes do not implement `Unpin`.

Unpin is something the user would have to implement for his struct, isn't it? So are you saying here that the macros do not generate Unpin? Why's that mentioned, do you expect anyone to need / expect to have Unpin?

rust/pin-init/src/lib.rs line 426

Quoted 3 lines
rust/pin-init/src/lib.rs +3 -0
+/// ## Mutable Borrows
+///
+/// A field is only shared-borrowed by default. To also allow `&mut` borrows, mark the borrowed

I think the English-bros say "To also allow *for* …"

Quoted 7 lines
rust/pin-init/src/lib.rs +7 -0
+/// field with `#[borrowed(mut)]`:
+///
+/// ```
+/// # use pin_init::*;
+/// #[pin_data]
+/// struct MutSelfRef {
+///     part: &'mut_msg mut str,

rust/pin-init/src/lib.rs line 443

Quoted 10 lines
rust/pin-init/src/lib.rs +10 -0
+///     #[borrowed(mut)]
+///     mut_msg: String,
+/// }
+///
+/// stack_pin_init!(let foo = pin_init!(MutSelfRef {
+///     mut_msg: "hello world".to_owned(),
+///     part: &mut mut_msg[..5],
+/// }));
+/// foo.as_mut().with_project(|proj| proj.part.make_ascii_uppercase());
+/// assert_eq!(*foo.part(), "HELLO");

Is #[borrowed(mut)] basically a better alternative to UnsafeCell?

rust/pin-init/src/lib.rs

Quoted 6 lines
rust/pin-init/src/lib.rs +5 -0
+/// ## Existential Lifetimes
+///
+/// A field lifetime ties a borrow to a specific field. Sometimes a field should instead borrow
+/// from something outside the struct that merely outlives one of the struct's lifetime
+/// parameters.

"the struct's lifetime parameters" == "the struct's members' lifetime parameters" ?

It doesn't get fully clear to me why you mention "sometimes should instead borrow from sth outside the struct". You seem to be referring to "normal" structs that borrow something with a normal lifetime. So it reads as if you fear that users might use self-referencial structs when really their struct should just borrow something from the outside.. why do you mention that, when would the user borrow something that the struct already has which shouldn't be in there?

rust/pin-init/src/lib.rs line 507

Quoted 3 lines
rust/pin-init/src/lib.rs +3 -0
+/// ## Projections and Accessors
+///
+/// Since field lifetimes are not parameters of the struct, borrows of the struct cannot carry

s/parameters/members ?

rust/pin-init/src/lib.rs line 513

Quoted 6 lines
rust/pin-init/src/lib.rs +6 -0
+/// them. This macro therefore generates several methods for accessing the fields:
+///
+/// - `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.

Means all self-referential fields are invisible? I would maybe mention that project() should be the preferred accessor for users.

rust/pin-init/src/lib.rs line 516

Quoted 3 lines
rust/pin-init/src/lib.rs +3 -0
+/// - `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`.

Thus, the user only wants these two if he needs to access self- referential fields?

rust/pin-init/src/lib.rs

Quoted 8 lines
rust/pin-init/src/lib.rs +7 -0
+///
+/// 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).
+///
+/// Every field (except those borrowed mutably) that captures field lifetimes also gets an accessor
+/// method,

Are you refering to my_struct.member(), or to
my_struct.with_member(|member| {… ) ?

Quoted 12 lines

because they cannot be directly accessed. fn field(&self) is generated if all of its

rust/pin-init/src/lib.rs +11 -0
+/// captures are covariant, otherwise `fn with_field(&self, f)` is generated where `f` is called
+/// with the field at its captured lifetimes.
+///
+/// ```
+/// # use pin_init::*;
+/// # #[pin_data]
+/// # struct SelfRef {
+/// #     part: &'msg str,
+/// #     msg: String,
+/// # }
+/// # stack_pin_init!(let foo = pin_init!(SelfRef {

rust/pin-init/src/lib.rs line 544

Quoted 10 lines
rust/pin-init/src/lib.rs +10 -0
+/// #     msg: "hello world".to_owned(),
+/// #     part: &msg[..5],
+/// # }));
+/// foo.as_mut().with_project(|proj| {
+///     *proj.part = &proj.msg[6..];
+/// });
+/// assert_eq!(*foo.part(), "world");
+/// foo.as_ref().with_project_ref(|proj| {
+///     assert_eq!(*proj.part, "world");
+/// });

So if you need one field only you would do:

foo.as_ref().with_part(|part| {
pr_info("{}", part);
});

?

maintainer
#3 lore raw
Quoted 9 lines · Philipp Stanner
Quoted 4 lines · Gary Guo

From: Gary Guo <[email protected]>

Add a section to the documentation of #[pin_data] macro about the safe self-referential creation feature.

Thx for writing this, that's very useful. I read through it. Just some comments below:

Quoted 9 lines

rust/pin-init/src/lib.rs line 401

Quoted 4 lines
rust/pin-init/src/lib.rs +4 -0
+/// Here the field `part` captures the field lifetime `'msg` and thereby borrows the field `msg`.
+/// A field may only borrow from fields declared after it: fields are dropped in declaration
+/// order, so the borrowing field is then always dropped before the field it borrows. Structs
+/// with borrowed fields or captured lifetimes do not implement `Unpin`.

Unpin is something the user would have to implement for his struct, isn't it? So are you saying here that the macros do not generate Unpin? Why's that mentioned, do you expect anyone to need / expect to have Unpin?

If you use pin_data, an Unpin implementation is controlled by pin_data. I reckon this is also under-documented, but most users won't need to know anyway (unless they manually implement Unpin and hit a duplicate implementation errror). Typically, if all fields are Unpin, then the struct will be Unpin despite use of #[pin_data].

For self-referential struct, pin_data generates a never-satisfied implementation and thus effectively never implement Unpin but also block user from implementing it. This is true even if all fields are Unpin.

E.g.

    struct Foo {
        y: &'x u32,
        x: u32,
    }

all fields are Unpin but Foo: !Unpin.

I'd probably not expand it out because most users won't need to care.

Quoted 18 lines

rust/pin-init/src/lib.rs line 426

Quoted 4 lines
rust/pin-init/src/lib.rs +3 -0
+/// ## Mutable Borrows
+///
+/// A field is only shared-borrowed by default. To also allow `&mut` borrows, mark the borrowed

I think the English-bros say "To also allow *for* …"

Quoted 11 lines
rust/pin-init/src/lib.rs +11 -0
+/// field with `#[borrowed(mut)]`:
+///
+/// ```
+/// # use pin_init::*;
+/// #[pin_data]
+/// struct MutSelfRef {
+///     part: &'mut_msg mut str,
+///     #[borrowed(mut)]
+///     mut_msg: String,
+/// }
+///
Quoted 8 lines

rust/pin-init/src/lib.rs line 443

Quoted 6 lines
rust/pin-init/src/lib.rs +6 -0
+/// stack_pin_init!(let foo = pin_init!(MutSelfRef {
+///     mut_msg: "hello world".to_owned(),
+///     part: &mut mut_msg[..5],
+/// }));
+/// foo.as_mut().with_project(|proj| proj.part.make_ascii_uppercase());
+/// assert_eq!(*foo.part(), "HELLO");

Is #[borrowed(mut)] basically a better alternative to UnsafeCell?

For some use cases perhaps, although #[borrowed(mut)] would make the field disappear from all &self and Pin<&mut Self> so you can only access it via the mutable reference that is created during initialization. If it fits your use cases then it's a good replacement, but no generally true.

Quoted 10 lines

rust/pin-init/src/lib.rs

Quoted 7 lines
rust/pin-init/src/lib.rs +5 -0
+/// ## Existential Lifetimes
+///
+/// A field lifetime ties a borrow to a specific field. Sometimes a field should instead borrow
+/// from something outside the struct that merely outlives one of the struct's lifetime
+/// parameters.

"the struct's lifetime parameters" == "the struct's members' lifetime parameters" ?

The current wording is correct. Struct have generic parameters, and it can either be lifetime parameter, type parameter or const parameter. Members don't have parameters.

Quoted 9 lines

It doesn't get fully clear to me why you mention "sometimes should instead borrow from sth outside the struct". You seem to be referring to "normal" structs that borrow something with a normal lifetime. So it reads as if you fear that users might use self-referencial structs when really their struct should just borrow something from the outside.. why do you mention that, when would the user borrow something that the struct already has which shouldn't be in there?

No this feature is somewhat independent of self-referential borrow. You can have a struct that just borrow from outside the struct only, but use the exists feature:

    struct Foo<'a> where exists<'inv>: 'a {
        foo: Mutex<&'inv ()>,
    }

This is similar to

    struct Foo<'a> {
        foo: Mutex<&'a ()>,
    }

However the existential lifetime causes the struct to be covariant over 'a and not invariant. This becomes part of self-referential series because they're both erased lifetimes and the implementation are remarkably similar (and shares almost all the code).

Quoted 8 lines

rust/pin-init/src/lib.rs line 507

Quoted 5 lines
rust/pin-init/src/lib.rs +3 -0
+/// ## Projections and Accessors
+///
+/// Since field lifetimes are not parameters of the struct, borrows of the struct cannot carry

s/parameters/members ?

Quoted 9 lines

rust/pin-init/src/lib.rs line 513

Quoted 6 lines
rust/pin-init/src/lib.rs +6 -0
+/// them. This macro therefore generates several methods for accessing the fields:
+///
+/// - `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.

Means all self-referential fields are invisible? I would maybe mention that project() should be the preferred accessor for users.

Depend on what you mean by preferred? If project() works then yes it is the best. But many cases you do need to use with_project()/with_project_ref() or the accessor methods so there's no single recommendation possible.

Quoted 7 lines

rust/pin-init/src/lib.rs line 516

Quoted 4 lines
rust/pin-init/src/lib.rs +3 -0
+/// - `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`.

Thus, the user only wants these two if he needs to access self- referential fields?

If you never access self-referential fields then you just need project() (or directly access the field), which is why these methods didn't exist before the pin-init self-ref series.

Quoted 13 lines

rust/pin-init/src/lib.rs

Quoted 10 lines
rust/pin-init/src/lib.rs +7 -0
+///
+/// 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).
+///
+/// Every field (except those borrowed mutably) that captures field lifetimes also gets an accessor
+/// method,

Are you refering to my_struct.member(), or to
my_struct.with_member(|member| {… ) ?

It's literally the next sentence?

Quoted 26 lines
Quoted 13 lines

because they cannot be directly accessed. fn field(&self) is generated if all of its

rust/pin-init/src/lib.rs +11 -0
+/// captures are covariant, otherwise `fn with_field(&self, f)` is generated where `f` is called
+/// with the field at its captured lifetimes.
+///
+/// ```
+/// # use pin_init::*;
+/// # #[pin_data]
+/// # struct SelfRef {
+/// #     part: &'msg str,
+/// #     msg: String,
+/// # }
+/// # stack_pin_init!(let foo = pin_init!(SelfRef {

rust/pin-init/src/lib.rs line 544

Quoted 10 lines
rust/pin-init/src/lib.rs +10 -0
+/// #     msg: "hello world".to_owned(),
+/// #     part: &msg[..5],
+/// # }));
+/// foo.as_mut().with_project(|proj| {
+///     *proj.part = &proj.msg[6..];
+/// });
+/// assert_eq!(*foo.part(), "world");
+/// foo.as_ref().with_project_ref(|proj| {
+///     assert_eq!(*proj.part, "world");
+/// });

So if you need one field only you would do:

Quoted 3 lines

foo.as_ref().with_part(|part| {
pr_info("{}", part);
});

Ah I didn't include an example of invariant capture in the example above. I should spell it out (and perhaps add comment in the doc code block too).

Best,
Gary