macro_rules! emplace {
($P:ident <- $S:ident { $($fields:tt)* }) => { ... };
(@INIT [$pret:ident]) => { ... };
(@INIT [$pret:ident] $field:ident <- $F:ident ( $($args:expr),* $(,)? ), $($rest:tt)*) => { ... };
(@INIT [$pret:ident] $field:ident : $expr:expr, $($rest:tt)*) => { ... };
(@PAT $S:ident { $( $field:ident $(:)? $(<-)? $_:expr, )* }) => { ... };
}
Expand description
Initializes a struct, field by field, in-place on the heap.
§Examples
A translated version of the code from the Rust-for-Linux talk at CTCFT (Nov 2021).
ⓘ
struct SharedState {
state_changed: CondVar,
inner: Mutex<usize>,
}
fn try_new() -> Result<Ref<Self>> {
let ret = emplace!(UniqueRef <- SharedState {
state_changed <- CondVar("SharedState::state_changed"),
inner <- Mutex(0, "SharedState::inner"),
})?;
Ok(ret.into())
}
This assumes Emplace
is implemented for Mutex
and CondVar
and would require a small
tweak to the macro to support fallible allocation (try_emplace
?).