Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,21 @@ macro_rules! __init_internal {
// 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),
Expand Down Expand Up @@ -1351,6 +1366,20 @@ macro_rules! __init_internal {
);
}
};
(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),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/compile-fail/init/wrong_generics2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ help: you might have forgotten to add the struct literal inside the block
--> src/macros.rs
|
~ ::core::ptr::write($slot, $t { SomeStruct {
|9 $($acc)*
|4 $($acc)*
~ } });
|

Expand Down
31 changes: 31 additions & 0 deletions tests/underscore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use pin_init::{try_init, Init};

pub struct Foo {
x: u64,
}

fn foo() -> bool {
false
}

fn bar() -> bool {
true
}

impl Foo {
pub fn new() -> impl Init<Self, ()> {
try_init!(Self {
_: {
if foo() {
return Err(());
}
},
x: 0,
_: {
if bar() {
return Err(());
}
}
}? ())
}
}
Loading