diff options
author | Benno Lossin <lossin@kernel.org> | 2025-09-05 16:05:31 +0200 |
---|---|---|
committer | Benno Lossin <lossin@kernel.org> | 2025-09-11 23:26:44 +0200 |
commit | 1fa516794fdd27b96cee77f8b12ac916b8b6a9a7 (patch) | |
tree | a851aa6a8fa5e27a3bc49f96588ef6db0822867a /rust/pin-init/src/lib.rs | |
parent | 619db96daf942dad974c6c8157ed06d52f7bb969 (diff) |
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`");
},
})
Tested-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Tested-by: Danilo Krummrich <dakr@kernel.org>
Reviewed-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Benno Lossin <lossin@kernel.org>
Diffstat (limited to 'rust/pin-init/src/lib.rs')
-rw-r--r-- | rust/pin-init/src/lib.rs | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs index 2d0d9fd12524..dd553212836e 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 |