public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: Benno Lossin <lossin@kernel.org>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Fiona Behrens" <me@kloenk.dev>,
	"Christian Schrefl" <chrisi.schrefl@gmail.com>,
	"Alban Kurti" <kurti@invicto.ai>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: pin-init: add code blocks to `[try_][pin_]init!` macros
Date: Wed, 10 Sep 2025 23:21:33 +0100	[thread overview]
Message-ID: <20250910232133.37ce4fb0.gary@garyguo.net> (raw)
In-Reply-To: <20250905140534.3328297-1-lossin@kernel.org>

On Fri,  5 Sep 2025 16:05:31 +0200
Benno Lossin <lossin@kernel.org> wrote:

> Allow writing `_: { /* any number of statements */ }` in initializers to
> run arbitrary code during initialization.
> 
>     try_init!(MyStruct {
>         _: {
>             if check_something() {
>                 return Err(MyError);
>             }
>         },
>         foo: Foo::new(val),
>         _: {
>             println!("successfully initialized `MyStruct`");
>         },
>     })
> 
> Link: https://github.com/Rust-for-Linux/pin-init/pull/84/commits/2880a9b898336e2d54f80715f00ce00f21f74d2f
> Signed-off-by: Benno Lossin <lossin@kernel.org>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
> I originally wanted to do some more modifications to the syntax of
> initializer macros, but I didn't have the time this cycle. See
> 
>     https://github.com/Rust-for-Linux/pin-init/pull/69
> 
> For the development of that syntax, it's probably going to be like a
> closure where in order to support formatting via rustfmt. But it still
> needs some disucssion around removing `<-`.
> 
> That change would allow having a `let` that binds a value for use in the
> initializer. IIRC @Alice, you needed that for something.
> ---
>  rust/pin-init/src/lib.rs    |  2 ++
>  rust/pin-init/src/macros.rs | 29 +++++++++++++++++++++++++++++
>  2 files changed, 31 insertions(+)
> 
> diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
> index 62e013a5cc20..8b556b0e106a 100644
> --- a/rust/pin-init/src/lib.rs
> +++ b/rust/pin-init/src/lib.rs
> @@ -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
> diff --git a/rust/pin-init/src/macros.rs b/rust/pin-init/src/macros.rs
> index 9ced630737b8..752e77db998c 100644
> --- a/rust/pin-init/src/macros.rs
> +++ b/rust/pin-init/src/macros.rs
> @@ -1202,6 +1202,21 @@ fn assert_zeroable<T: $crate::Zeroable>(_: *mut T) {}
>          // have been initialized. Therefore we can now dismiss the guards by forgetting them.
>          $(::core::mem::forget($guards);)*
>      };
> +    (init_slot($($use_data:ident)?):
> +        @data($data:ident),
> +        @slot($slot:ident),
> +        @guards($($guards:ident,)*),
> +        // arbitrary code block
> +        @munch_fields(_: { $($code:tt)* }, $($rest:tt)*),
> +    ) => {
> +        { $($code)* }
> +        $crate::__init_internal!(init_slot($($use_data)?):
> +            @data($data),
> +            @slot($slot),
> +            @guards($($guards,)*),
> +            @munch_fields($($rest)*),
> +        );
> +    };
>      (init_slot($use_data:ident): // `use_data` is present, so we use the `data` to init fields.
>          @data($data:ident),
>          @slot($slot:ident),
> @@ -1351,6 +1366,20 @@ fn assert_zeroable<T: $crate::Zeroable>(_: *mut T) {}
>              );
>          }
>      };
> +    (make_initializer:
> +        @slot($slot:ident),
> +        @type_name($t:path),
> +        @munch_fields(_: { $($code:tt)* }, $($rest:tt)*),
> +        @acc($($acc:tt)*),
> +    ) => {
> +        // code blocks are ignored for the initializer check
> +        $crate::__init_internal!(make_initializer:
> +            @slot($slot),
> +            @type_name($t),
> +            @munch_fields($($rest)*),
> +            @acc($($acc)*),
> +        );
> +    };
>      (make_initializer:
>          @slot($slot:ident),
>          @type_name($t:path),
> 
> base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585


  parent reply	other threads:[~2025-09-10 22:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-05 14:05 [PATCH] rust: pin-init: add code blocks to `[try_][pin_]init!` macros Benno Lossin
2025-09-07 10:57 ` Alice Ryhl
2025-09-07 11:20   ` Danilo Krummrich
2025-09-07 11:33     ` Alice Ryhl
2025-09-07 11:49       ` Danilo Krummrich
2025-09-07 17:17 ` Danilo Krummrich
2025-09-10 10:13   ` Danilo Krummrich
2025-09-10 10:19 ` Alice Ryhl
2025-09-10 22:21 ` Gary Guo [this message]
2025-09-11  2:22 ` Alexandre Courbot
2025-09-11 21:34 ` Benno Lossin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250910232133.37ce4fb0.gary@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=chrisi.schrefl@gmail.com \
    --cc=dakr@kernel.org \
    --cc=kurti@invicto.ai \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=me@kloenk.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox