From 58bb4d921f81393208056f63bf58b25404562f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Wed, 6 Aug 2025 17:22:22 +0200 Subject: [PATCH] derive: remove trait `BufferFmt` --- askama_derive/src/generator/expr.rs | 2 +- askama_derive/src/generator/filter.rs | 8 +++--- askama_derive/src/generator/node.rs | 13 +++++----- askama_derive/src/integration.rs | 35 ++++++++++++--------------- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/askama_derive/src/generator/expr.rs b/askama_derive/src/generator/expr.rs index e4fdf5fd..44abb235 100644 --- a/askama_derive/src/generator/expr.rs +++ b/askama_derive/src/generator/expr.rs @@ -363,7 +363,7 @@ impl<'a> Generator<'a, '_> { if i > 0 { buf.write_token(Token![,], span); } - buf.write(self.visit_arg(ctx, arg, span)?, ctx.template_span); + buf.write_tokens(self.visit_arg(ctx, arg, span)?); } Ok(()) } diff --git a/askama_derive/src/generator/filter.rs b/askama_derive/src/generator/filter.rs index b0abdd52..c2d84477 100644 --- a/askama_derive/src/generator/filter.rs +++ b/askama_derive/src/generator/filter.rs @@ -292,11 +292,9 @@ impl<'a> Generator<'a, '_> { let arg = self.visit_arg(ctx, filter, ctx.span_for_node(filter.span()))?; let tmp = tmp.into_token_stream(); - quote_into!(buf, span, { askama::filters::reject( - #tmp, - // coerce [T, &T, &&T...] to &T - (&&&(#arg)) - as &_)? + quote_into!(buf, span, { + // coerce [T, &T, &&T...] to &T + askama::filters::reject(#tmp, (&&&(#arg)) as &_)? }); } diff --git a/askama_derive/src/generator/node.rs b/askama_derive/src/generator/node.rs index 60eafd8f..d0f57d33 100644 --- a/askama_derive/src/generator/node.rs +++ b/askama_derive/src/generator/node.rs @@ -580,9 +580,11 @@ impl<'a> Generator<'a, '_> { Ok(size_hint) })?; let cond_buf = cond_buf.into_token_stream(); - loop_buf.write_tokens(quote_spanned!(span=> if !#var_did_loop { - #cond_buf - })); + quote_into!(&mut loop_buf, span, { + if !#var_did_loop { + #cond_buf + } + }); } else { this.handle_ws(loop_block.ws3); size_hint2 = this.write_buf_writable(ctx, &mut loop_buf)?; @@ -1159,10 +1161,7 @@ impl<'a> Generator<'a, '_> { // multiple times, e.g. in the case of macro // parameters being used multiple times. _ => { - value.write( - this.visit_expr_root(&call_ctx, expr)?, - span_span, - ); + value.write_tokens(this.visit_expr_root(&call_ctx, expr)?); // We need to normalize the arg to write it, thus we need to add it to // locals in the normalized manner let id = field_new(arg, span_span); diff --git a/askama_derive/src/integration.rs b/askama_derive/src/integration.rs index 7e3b52e5..fdb2e9a4 100644 --- a/askama_derive/src/integration.rs +++ b/askama_derive/src/integration.rs @@ -119,6 +119,16 @@ impl ToTokens for Buffer { } } +impl IntoIterator for Buffer { + type Item = ::Item; + type IntoIter = ::IntoIter; + + #[inline] + fn into_iter(self) -> Self::IntoIter { + self.buf.into_iter() + } +} + impl Buffer { pub(crate) fn new() -> Self { Self { @@ -154,29 +164,24 @@ impl Buffer { self.string_literals.push((literal, span)); } + #[inline] pub(crate) fn into_token_stream(mut self) -> TokenStream { self.handle_str_lit(); self.buf } + #[inline] pub(crate) fn is_discard(&self) -> bool { self.discard } + #[inline] pub(crate) fn set_discard(&mut self, discard: bool) { self.discard = discard; } - pub(crate) fn write(&mut self, src: impl BufferFmt, span: proc_macro2::Span) { - if self.discard { - return; - } - - self.handle_str_lit(); - src.append_to(&mut self.buf, span); - } - - pub(crate) fn write_tokens(&mut self, src: TokenStream) { + #[inline] + pub(crate) fn write_tokens(&mut self, src: impl IntoIterator) { if self.discard { return; } @@ -269,16 +274,6 @@ impl Buffer { } } -pub(crate) trait BufferFmt { - fn append_to(self, buf: &mut TokenStream, span: proc_macro2::Span); -} - -impl BufferFmt for TokenStream { - fn append_to(self, buf: &mut TokenStream, _span: proc_macro2::Span) { - buf.extend(self); - } -} - /// Similar to `write!(dest, "{src:?}")`, but only escapes the strictly needed characters, /// and without the surrounding `"…"` quotation marks. pub(crate) fn string_escape(dest: &mut String, src: &str) {