derive: remove trait BufferFmt

This commit is contained in:
René Kijewski 2025-08-06 17:22:22 +02:00 committed by René Kijewski
parent f62cdfb49a
commit 58bb4d921f
4 changed files with 25 additions and 33 deletions

View File

@ -363,7 +363,7 @@ impl<'a> Generator<'a, '_> {
if i > 0 { if i > 0 {
buf.write_token(Token![,], span); 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(()) Ok(())
} }

View File

@ -292,11 +292,9 @@ impl<'a> Generator<'a, '_> {
let arg = self.visit_arg(ctx, filter, ctx.span_for_node(filter.span()))?; let arg = self.visit_arg(ctx, filter, ctx.span_for_node(filter.span()))?;
let tmp = tmp.into_token_stream(); let tmp = tmp.into_token_stream();
quote_into!(buf, span, { askama::filters::reject( quote_into!(buf, span, {
#tmp, // coerce [T, &T, &&T...] to &T
// coerce [T, &T, &&T...] to &T askama::filters::reject(#tmp, (&&&(#arg)) as &_)?
(&&&(#arg))
as &_)?
}); });
} }

View File

@ -580,9 +580,11 @@ impl<'a> Generator<'a, '_> {
Ok(size_hint) Ok(size_hint)
})?; })?;
let cond_buf = cond_buf.into_token_stream(); let cond_buf = cond_buf.into_token_stream();
loop_buf.write_tokens(quote_spanned!(span=> if !#var_did_loop { quote_into!(&mut loop_buf, span, {
#cond_buf if !#var_did_loop {
})); #cond_buf
}
});
} else { } else {
this.handle_ws(loop_block.ws3); this.handle_ws(loop_block.ws3);
size_hint2 = this.write_buf_writable(ctx, &mut loop_buf)?; 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 // multiple times, e.g. in the case of macro
// parameters being used multiple times. // parameters being used multiple times.
_ => { _ => {
value.write( value.write_tokens(this.visit_expr_root(&call_ctx, expr)?);
this.visit_expr_root(&call_ctx, expr)?,
span_span,
);
// We need to normalize the arg to write it, thus we need to add it to // We need to normalize the arg to write it, thus we need to add it to
// locals in the normalized manner // locals in the normalized manner
let id = field_new(arg, span_span); let id = field_new(arg, span_span);

View File

@ -119,6 +119,16 @@ impl ToTokens for Buffer {
} }
} }
impl IntoIterator for Buffer {
type Item = <TokenStream as IntoIterator>::Item;
type IntoIter = <TokenStream as IntoIterator>::IntoIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.buf.into_iter()
}
}
impl Buffer { impl Buffer {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
@ -154,29 +164,24 @@ impl Buffer {
self.string_literals.push((literal, span)); self.string_literals.push((literal, span));
} }
#[inline]
pub(crate) fn into_token_stream(mut self) -> TokenStream { pub(crate) fn into_token_stream(mut self) -> TokenStream {
self.handle_str_lit(); self.handle_str_lit();
self.buf self.buf
} }
#[inline]
pub(crate) fn is_discard(&self) -> bool { pub(crate) fn is_discard(&self) -> bool {
self.discard self.discard
} }
#[inline]
pub(crate) fn set_discard(&mut self, discard: bool) { pub(crate) fn set_discard(&mut self, discard: bool) {
self.discard = discard; self.discard = discard;
} }
pub(crate) fn write(&mut self, src: impl BufferFmt, span: proc_macro2::Span) { #[inline]
if self.discard { pub(crate) fn write_tokens(&mut self, src: impl IntoIterator<Item = TokenTree>) {
return;
}
self.handle_str_lit();
src.append_to(&mut self.buf, span);
}
pub(crate) fn write_tokens(&mut self, src: TokenStream) {
if self.discard { if self.discard {
return; 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, /// Similar to `write!(dest, "{src:?}")`, but only escapes the strictly needed characters,
/// and without the surrounding `"…"` quotation marks. /// and without the surrounding `"…"` quotation marks.
pub(crate) fn string_escape(dest: &mut String, src: &str) { pub(crate) fn string_escape(dest: &mut String, src: &str) {