diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-05 07:39:02 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-05 07:39:02 +0100 |
commit | abaa16748a6d951b951114a69f3d0e02457fcffa (patch) | |
tree | 3040d672b04549ded6f23a06173968960b589774 | |
parent | 6a6ea52a58432b617304c0b08949db995252a539 (diff) | |
download | askama-abaa16748a6d951b951114a69f3d0e02457fcffa.tar.gz askama-abaa16748a6d951b951114a69f3d0e02457fcffa.tar.bz2 askama-abaa16748a6d951b951114a69f3d0e02457fcffa.zip |
Extend Template API to avoid need for multiple buffers
-rw-r--r-- | askama/src/generator.rs | 10 | ||||
-rw-r--r-- | 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; |