Move tuple_types_to_var_identifiers macro functionality into quote macro
Currently, our TryClone
derive macro needs a second proc macro called tuple_types_to_var_identifiers
to generate the match arms for enums using tuple variants in try_clone()
.
The second macro is needed to generate unique variable names for the tuple fields. Here's an example for better understanding:
#[derive(TryClone)]
pub enum Example {
Variant1,
Variant2(u32, WasmString)
}
impl TryClone for Example {
fn try_clone(&self) -> core::result::Result<Self, TryCloneError> {
match self {
Example::Variant1 => Ok(Example::Variant1),
Example::Variant2(v0, v1) => Ok(Example::Variant2(v0.try_clone()?, v1.try_clone()?)),
}
}
}
The magic happens here: to produce Example::Variant2(v0, v1)
, we need to generate unique (e.g. incrementing) variable names for every tuple field. This is something that can be done using the quote
crate in "normal" Rust. The Linux Kernel already offers an alternative quote!
macro but without the needed feature:
let i = (0..self.fields.len()).map(syn::Index::from);
// expands to 0 + self.0.heap_size() + self.1.heap_size() + ...
quote! {
0 #( + self.#i.heap_size() )*
}
Something like that would be a very good extension for the current quote!
macro in the Kernel. We would be able to remove our custom tuple_types_to_var_identifiers
macro.