Commit 37604019 authored by Danilo Krummrich's avatar Danilo Krummrich
Browse files

Merge tag 'pin-init-v6.18' of https://github.com/Rust-for-Linux/linux into drm-rust-next



pin-init changes for v6.18

Changed:

- `#[pin_data]` now generates a `*Projection` struct similar to the
  `pin-project` crate.

- Add initializer code blocks to `[try_][pin_]init!` macros: make
  initializer macros accept any number of `_: {/* arbitrary code */},` &
  make them run the code at that point.

- Make the `[try_][pin_]init!` macros expose initialized fields via a
  `let` binding as `&mut T` or `Pin<&mut T>` for later fields.

Upstream dev news:

- Released v0.0.10 before the changes included in this tag.

- Inform users of the impending rename from `pinned-init` to `pin-init`
  (in the kernel the rename already happened).

- More CI improvements.

Signed-off-by: default avatarDanilo Krummrich <dakr@kernel.org>

From: Benno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/r/20250912174148.373530-1-lossin@kernel.org
parents cf4fd52e 42415d16
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -134,11 +134,9 @@ pub fn new<'a, E>(
        T: 'a,
        Error: From<E>,
    {
        let callback = Self::devres_callback;

        try_pin_init!(&this in Self {
            dev: dev.into(),
            callback,
            callback: Self::devres_callback,
            // INVARIANT: `inner` is properly initialized.
            inner <- {
                // SAFETY: `this` is a valid pointer to uninitialized memory.
@@ -151,7 +149,7 @@ pub fn new<'a, E>(
                //    properly initialized, because we require `dev` (i.e. the *bound* device) to
                //    live at least as long as the returned `impl PinInit<Self, Error>`.
                to_result(unsafe {
                    bindings::devm_add_action(dev.as_raw(), Some(callback), inner.cast())
                    bindings::devm_add_action(dev.as_raw(), Some(*callback), inner.cast())
                })?;

                Opaque::pin_init(try_pin_init!(Inner {
+1 −8
Original line number Diff line number Diff line
@@ -356,18 +356,11 @@ struct ClosureWork<T> {
    func: Option<T>,
}

impl<T> ClosureWork<T> {
    fn project(self: Pin<&mut Self>) -> &mut Option<T> {
        // SAFETY: The `func` field is not structurally pinned.
        unsafe { &mut self.get_unchecked_mut().func }
    }
}

impl<T: FnOnce()> WorkItem for ClosureWork<T> {
    type Pointer = Pin<KBox<Self>>;

    fn run(mut this: Pin<KBox<Self>>) {
        if let Some(func) = this.as_mut().project().take() {
        if let Some(func) = this.as_mut().project().func.take() {
            (func)()
        }
    }
+12 −0
Original line number Diff line number Diff line
@@ -6,6 +6,18 @@
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/Rust-for-Linux/pin-init/test.yml)
# `pin-init`

> [!NOTE]
> 
> This crate was originally named [`pinned-init`], but the migration to
> `pin-init` is not yet complete. The `legcay` branch contains the current
> version of the `pinned-init` crate & the `main` branch already incorporates
> the rename to `pin-init`.
>
> There are still some changes needed on the kernel side before the migration
> can be completed.

[`pinned-init`]: https://crates.io/crates/pinned-init

<!-- cargo-rdme start -->

Library to safely and fallibly initialize pinned `struct`s using in-place constructors.
+3 −1
Original line number Diff line number Diff line
@@ -24,4 +24,6 @@ fn from(_: AllocError) -> Self {
}

#[allow(dead_code)]
fn main() {}
fn main() {
    let _ = Error;
}
+3 −1
Original line number Diff line number Diff line
@@ -740,6 +740,8 @@ macro_rules! stack_try_pin_init {
/// As already mentioned in the examples above, inside of `pin_init!` a `struct` initializer with
/// the following modifications is expected:
/// - Fields that you want to initialize in-place have to use `<-` instead of `:`.
/// - You can use `_: { /* run any user-code here */ },` anywhere where you can place fields in
///   order to run arbitrary code.
/// - In front of the initializer you can write `&this in` to have access to a [`NonNull<Self>`]
///   pointer named `this` inside of the initializer.
/// - Using struct update syntax one can place `..Zeroable::init_zeroed()` at the very end of the
@@ -994,7 +996,7 @@ macro_rules! try_init {
/// }
///
/// impl<T> Foo<T> {
///     fn project(self: Pin<&mut Self>) -> Pin<&mut T> {
///     fn project_this(self: Pin<&mut Self>) -> Pin<&mut T> {
///         assert_pinned!(Foo<T>, elem, T, inline);
///
///         // SAFETY: The field is structurally pinned.
Loading