Skip to content

Rework initialization and accessor generation#143

Open
nbdd0121 wants to merge 6 commits into
mainfrom
dev/accessor-rework
Open

Rework initialization and accessor generation#143
nbdd0121 wants to merge 6 commits into
mainfrom
dev/accessor-rework

Conversation

@nbdd0121
Copy link
Copy Markdown
Member

Depends on #132

Use different guard types to determine the type of let bindings generated, and also move much of the logic of the generated code to __internal.rs. Each commit explains the rationale on its own.

@nbdd0121 nbdd0121 force-pushed the dev/accessor-rework branch from 7407d8b to a91a85e Compare April 28, 2026 15:44
@nbdd0121 nbdd0121 mentioned this pull request May 4, 2026
@nbdd0121 nbdd0121 force-pushed the dev/accessor-rework branch from a91a85e to 6056cad Compare May 11, 2026 11:48
nbdd0121 added 6 commits May 11, 2026 22:27
Currently, the `pin_init` library has an `Invariant` type alias, and it is
instantiated using `PhantomData`. Generated code from `pin_data` on the
other hand cannot access the crate-local type alias, so it generates
`PhantomData<fn(T) -> T>` directly. This is all very inconsistent, despite
the exact same use case of ensuring invariance.

Add `PhantomInvariant` and `PhantomInvariantLifetime` and switch all users
that need to express the concept of invariance to use these. They're
polyfills of unstable types in the same names in the Rust standard library.

Signed-off-by: Gary Guo <gary@garyguo.net>
`InitializerKind::Code` is a special case where it does not initialize a
field, and thus generate no guard and accessors. Handle it earlier and make
the rest of the code more linear.

Signed-off-by: Gary Guo <gary@garyguo.net>
Instead of projecting the created reference, simply create drop guards with
different marker types and have the `let_binding()` method of guards of
different marker produce different type instead.

This allows more flexible lifetime as this is now controlled by the guard.
This will be needed when implementing self-referential fields.

Signed-off-by: Gary Guo <gary@garyguo.net>
The `InitData` and `PinData` traits do not need to exist, the inference
helpers could be inherent methods instead.

There is no risk for calling the wrong methods even when user defines it,
as inherent methods take priority over trait methods.

With this change, it unlocks the possibility of attaching additional bounds
to the method per type, which is not possible for trait methods.

Signed-off-by: Gary Guo <gary@garyguo.net>
By projecting slots, the `pin_init!` and `init!` code path can be more
unified. This also reduces the amount of macro-generated code and shifts
them to the shared infrastructure.

Signed-off-by: Gary Guo <gary@garyguo.net>
Instead of projecting using pointer to a field project the full slot. This
further shifts the code generation from the initializer site to the struct
definition site, which means less code is generated overall.

It also makes the safety comment easier to justify, as now the projection
is done by the `#[pin_data]` macro which has full visibility of pinnedness
of fields.

The field alignment could also be checked on the `#[pin_data]` side;
however, since `init!()` macro works for other type of structs, we cannot
remove the alignment check from `init!`/`pin_init!` side anyway, so I opted
to still keep the alignment check in init.rs.

Signed-off-by: Gary Guo <gary@garyguo.net>
@nbdd0121 nbdd0121 force-pushed the dev/accessor-rework branch from 6056cad to 5f615ef Compare May 14, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant