Skip to content

Commit 1bb42ea

Browse files
committed
Merge branch 'dev/simple-underscore'
2 parents 10ff00e + b281afe commit 1bb42ea

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Changed
1111

1212
- `#[pin_data]` now generates a `*Projection` struct similar to the `pin-project` crate.
13+
- Add initializer code blocks to `[try_][pin_]init!` macros: make initializer
14+
macros accept any number of `_: {/* arbitrary code */},` & make them run the
15+
code at that point.
1316

1417
## [0.0.10] - 2025-08-19
1518

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,8 @@ macro_rules! stack_try_pin_init {
740740
/// As already mentioned in the examples above, inside of `pin_init!` a `struct` initializer with
741741
/// the following modifications is expected:
742742
/// - Fields that you want to initialize in-place have to use `<-` instead of `:`.
743+
/// - You can use `_: { /* run any user-code here */ },` anywhere where you can place fields in
744+
/// order to run arbitrary code.
743745
/// - In front of the initializer you can write `&this in` to have access to a [`NonNull<Self>`]
744746
/// pointer named `this` inside of the initializer.
745747
/// - Using struct update syntax one can place `..Zeroable::init_zeroed()` at the very end of the

src/macros.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,21 @@ macro_rules! __init_internal {
12631263
// have been initialized. Therefore we can now dismiss the guards by forgetting them.
12641264
$(::core::mem::forget($guards);)*
12651265
};
1266+
(init_slot($($use_data:ident)?):
1267+
@data($data:ident),
1268+
@slot($slot:ident),
1269+
@guards($($guards:ident,)*),
1270+
// arbitrary code block
1271+
@munch_fields(_: { $($code:tt)* }, $($rest:tt)*),
1272+
) => {
1273+
{ $($code)* }
1274+
$crate::__init_internal!(init_slot($($use_data)?):
1275+
@data($data),
1276+
@slot($slot),
1277+
@guards($($guards,)*),
1278+
@munch_fields($($rest)*),
1279+
);
1280+
};
12661281
(init_slot($use_data:ident): // `use_data` is present, so we use the `data` to init fields.
12671282
@data($data:ident),
12681283
@slot($slot:ident),
@@ -1412,6 +1427,20 @@ macro_rules! __init_internal {
14121427
);
14131428
}
14141429
};
1430+
(make_initializer:
1431+
@slot($slot:ident),
1432+
@type_name($t:path),
1433+
@munch_fields(_: { $($code:tt)* }, $($rest:tt)*),
1434+
@acc($($acc:tt)*),
1435+
) => {
1436+
// code blocks are ignored for the initializer check
1437+
$crate::__init_internal!(make_initializer:
1438+
@slot($slot),
1439+
@type_name($t),
1440+
@munch_fields($($rest)*),
1441+
@acc($($acc)*),
1442+
);
1443+
};
14151444
(make_initializer:
14161445
@slot($slot:ident),
14171446
@type_name($t:path),

tests/ui/compile-fail/init/wrong_generics2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ help: you might have forgotten to add the struct literal inside the block
1212
--> src/macros.rs
1313
|
1414
~ ::core::ptr::write($slot, $t { SomeStruct {
15-
|0 $($acc)*
15+
|5 $($acc)*
1616
~ } });
1717
|
1818

tests/underscore.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use pin_init::{try_init, Init};
2+
3+
pub struct Foo {
4+
x: u64,
5+
}
6+
7+
fn foo() -> bool {
8+
false
9+
}
10+
11+
fn bar() -> bool {
12+
true
13+
}
14+
15+
impl Foo {
16+
pub fn new() -> impl Init<Self, ()> {
17+
try_init!(Self {
18+
_: {
19+
if foo() {
20+
return Err(());
21+
}
22+
},
23+
x: 0,
24+
_: {
25+
if bar() {
26+
return Err(());
27+
}
28+
}
29+
}? ())
30+
}
31+
}

0 commit comments

Comments
 (0)