rust: pin-init: add code blocks to `[try_][pin_]init!` macros
Subsystems (3)
Benno Lossin
maintainerPATCH 1/1 rust: pin-init: add code blocks to `[try_][pin_]init!` macros
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`");
},
})---
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(+)
rust/pin-init/src/lib.rs +2 -0
@@ -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
rust/pin-init/src/macros.rs +29 -0
@@ -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
Signature · 1 line
2.50.1
Alice Ryhl
reviewerQuoted 15 lines · Benno Lossin
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`"); }, })
Quoted 2 lines
Nice! Would it be possible to include a user so I can see it work in practice? E.g., for the irq feature?
Alice
Danilo Krummrich
maintainerQuoted 16 lines · Alice Ryhl
Quoted 15 lines · Benno Lossin
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`"); }, })
Quoted 5 lines
Quoted 2 lines
Nice! Would it be possible to include a user so I can see it work in practice? E.g., for the irq feature?
Devres needs this too, but the corresponding devres stuff was a fix and is in the current -rc only, so that's not a candidate.
The IRQ stuff is in driver-core-next going to Linus for v6.18, hence, using it there, this patch would have to go through the driver-core tree as well.
For me it is fine either way.
Alice Ryhl
reviewerQuoted 23 lines · Danilo Krummrich
Quoted 16 lines · Alice Ryhl
Quoted 15 lines · Benno Lossin
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`"); }, })Quoted 5 lines
Quoted 2 lines
Nice! Would it be possible to include a user so I can see it work in practice? E.g., for the irq feature?
Quoted 7 lines
Devres needs this too, but the corresponding devres stuff was a fix and is in the current -rc only, so that's not a candidate.
The IRQ stuff is in driver-core-next going to Linus for v6.18, hence, using it there, this patch would have to go through the driver-core tree as well.
For me it is fine either way.
It doesn't have to land together. I would be happy with:
THIS CYCLE: Land just this patch. NEXT CYCLE: Land the irq and/or devres user.
But I'd like to see it work in practice before I give a Reviewed-by. It's hard to evaluate this kind of macro change just from the macro itself.
Alice
Danilo Krummrich
maintainerQuoted 32 lines · Alice Ryhl
Quoted 23 lines · Danilo Krummrich
Quoted 16 lines · Alice Ryhl
Quoted 15 lines · Benno Lossin
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`"); }, })Quoted 5 lines
Quoted 2 lines
Nice! Would it be possible to include a user so I can see it work in practice? E.g., for the irq feature?
Quoted 7 lines
Devres needs this too, but the corresponding devres stuff was a fix and is in the current -rc only, so that's not a candidate.
The IRQ stuff is in driver-core-next going to Linus for v6.18, hence, using it there, this patch would have to go through the driver-core tree as well.
For me it is fine either way.
Quoted 8 lines
It doesn't have to land together. I would be happy with:
THIS CYCLE: Land just this patch. NEXT CYCLE: Land the irq and/or devres user.
But I'd like to see it work in practice before I give a Reviewed-by. It's hard to evaluate this kind of macro change just from the macro itself.
Here's a patch for using it in devres (we backmerged -rc3 into driver-core-next):
From 23e48f081b7ba6bebd375c6fa929f070f7977fc5 Mon Sep 17 00:00:00 2001
From: Danilo Krummrich <[email protected]>
Date: Sun, 7 Sep 2025 13:44:43 +0200
Subject: [PATCH] rust: devres: take advantage of initializer code blocks
Use pin-init initializer code blocks to register the devres action.
This resolved the TODO introduced by commit 75a7b151e808 ("rust: devres:
fix leaking call to devm_add_action()").
--- rust/kernel/devres.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
rust/kernel/devres.rs +1 -5
@@ -119,7 +119,6 @@ pub struct Devres<T: Send> {// explicit `Send` and `Sync' impls can be removed. #[pin] inner: Opaque<Inner<T>>, - _add_action: (), }impl <T :Send >Devres <T > {
@@ -146,10 +145,7 @@ pub fn new<'a, E>( revoke <- Completion::new(), data <- Revocable::new(data), })), -// TODO: Replace with "initializer code blocks" [1] once available. -// -// [1] https://github.com/Rust-for-Linux/pin-init/pull/69 - _add_action: { + _: {// SAFETY: `this` is a valid pointer to uninitialized memory. let inner =unsafe { &raw mut (*this.as_ptr ()).inner };
base-commit: 544c94f0e155614551e0210074597a23d117830c --
2.51.0
Quoted 15 lines · Benno Lossin
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`"); }, })
Quoted 2 lines
Quoted 1 line · Danilo Krummrich
Tested-by Danilo Krummrich <[email protected]>
Quoted 15 lines · Benno Lossin
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`"); }, })
Quoted 2 lines
Quoted 15 lines · On Fri, 5 Sep 2025 16:05:31 +0200 Benno Lossin <[email protected]> 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`"); }, })
Quoted 2 lines
Quoted 79 lines
---
I originally wanted to do some more modifications to the syntax of initializer macros, but I didn't have the time this cycle. Seehttps://github.com/Rust-for-Linux/pin-init/pull/69For 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
letthat 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(+)rust/pin-init/src/lib.rs +2
-0@@ -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 rust/pin-init/src/macros.rs +29
-0@@ -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
Quoted 15 lines · Benno Lossin
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`"); }, })
Quoted 2 lines
Thanks, this is going to be very helpful for nova-core as well.
Quoted 15 lines · Benno Lossin
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`"); }, })
Quoted 2 lines
Applied to pin-init-next, thanks everyone!
---
Cheers,
Benno