Hello, just about curiosity : recently I noticed that I often use this pattern, especially when moving variables into closures:
let tx = tx.clone();
So, what about adding a friendly macro rule:
macro_rules! let_clone {
($id:ident) => {
let $id = $id.clone();
};
}
let_clone!(tx);
But later in my code, I saw this:
let tx = self.tx.clone();
Ok, so let's add a special rule in the macro_rules
:
macro_rules! let_clone {
($id:ident) => {
let $id = $id.clone();
};
($parent:ident . $id:ident) => {
let $id = $parent.$id.clone();
};
}
let_clone!(self.tx);
But then I'm wondering if it's possible to handle a more general case, something like this:
macro_rules! let_clone {
($($expr:expr),* $(,)?) => {
$(
let last_ident_in_expr!($expr) = $expr.clone();
)*
};
}
The problem is I don't know how to implement last_ident_in_expr!()
; I guess it's feasible with a proc macro, but is it possible with a simple macro rule ?
EDIT:
I can't imagine an "objective" implementation of the macro rule let_clone!()
explained above, but in the context of this question, here are some examples of the use-cases I'm thinking about:
// let_clone!(a.x.y.z.foo);
let foo = a.x.y.z.foo.clone();
// let_clone!(a.x.y.z.foo());
let foo = a.x.y.z.foo().clone();
// let_clone!(a.x[42].y.z.foo());
let foo = a.x[42].y.z.foo().clone();