From abaa16748a6d951b951114a69f3d0e02457fcffa Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 5 Feb 2017 07:39:02 +0100 Subject: Extend Template API to avoid need for multiple buffers --- askama/src/generator.rs | 10 ++++------ askama/src/lib.rs | 7 ++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 56bd19e..a5686e6 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -44,9 +44,8 @@ impl Generator { self.writeln(" {"); self.indent(); - self.writeln("fn render(&self) -> String {"); + self.writeln("fn render_into(&self, writer: &mut std::fmt::Write) {"); self.indent(); - self.writeln("let mut buf = String::new();"); } fn indent(&mut self) { @@ -117,13 +116,13 @@ impl Generator { } fn write_lit(&mut self, s: &[u8]) { - self.write("buf.push_str("); + self.write("writer.write_str("); self.write(&format!("{:#?}", str::from_utf8(s).unwrap())); - self.writeln(");"); + self.writeln(").unwrap();"); } fn write_expr(&mut self, s: &Expr) { - self.write("std::fmt::Write::write_fmt(&mut buf, format_args!(\"{}\", "); + self.write("writer.write_fmt(format_args!(\"{}\", "); self.visit_expr(s); self.writeln(")).unwrap();"); } @@ -184,7 +183,6 @@ impl Generator { } fn finalize(&mut self) { - self.writeln("buf"); self.dedent(); self.writeln("}"); self.dedent(); diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 48aa4e3..80b5c25 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -3,7 +3,12 @@ extern crate nom; extern crate syn; pub trait Template { - fn render(&self) -> String; + fn render_into(&self, writer: &mut std::fmt::Write); + fn render(&self) -> String { + let mut buf = String::new(); + self.render_into(&mut buf); + buf + } } pub mod filters; -- cgit