From 0f9c5a02810f111da2f5a9f780e403b3429b4568 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 2 Oct 2024 22:11:00 +0200 Subject: [PATCH] Improve handling of `*` and `&` operators in functions/methods arguments --- rinja_derive/src/generator.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/rinja_derive/src/generator.rs b/rinja_derive/src/generator.rs index 49cd7b8c..f77466ce 100644 --- a/rinja_derive/src/generator.rs +++ b/rinja_derive/src/generator.rs @@ -1957,7 +1957,23 @@ impl<'a> Generator<'a> { buf: &mut Buffer, arg: &WithSpan<'_, Expr<'_>>, ) -> Result<(), CompileError> { - let borrow = !is_copyable(arg); + self._visit_arg_inner(ctx, buf, arg, false) + } + + fn _visit_arg_inner( + &mut self, + ctx: &Context<'_>, + buf: &mut Buffer, + arg: &WithSpan<'_, Expr<'_>>, + // This variable is needed because `Expr::Unary` is not copyable but since we might + // skip a few levels. + need_borrow: bool, + ) -> Result<(), CompileError> { + if let Expr::Unary(expr @ ("*" | "&"), ref arg) = **arg { + buf.write(expr); + return self._visit_arg_inner(ctx, buf, arg, true); + } + let borrow = need_borrow || !is_copyable(arg); if borrow { buf.write("&("); }