From ed2fd2b1053fac08bc2e4fa4a6de2d79c136d636 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 6 Aug 2017 20:55:41 +0200 Subject: Share HashSet for locals among nested Generators --- askama_derive/src/generator.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index e24aaa8..16d7c9e 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -22,14 +22,14 @@ struct Generator<'a> { buf: String, indent: u8, start: bool, - locals: HashSet, + locals: &'a mut HashSet, next_ws: Option<&'a str>, skip_ws: bool, } impl<'a> Generator<'a> { - fn new(locals: HashSet, indent: u8) -> Generator<'a> { + fn new<'n>(locals: &'n mut HashSet, indent: u8) -> Generator<'n> { Generator { buf: String::new(), indent: indent, @@ -40,12 +40,12 @@ impl<'a> Generator<'a> { } } - fn default() -> Generator<'a> { - Self::new(HashSet::new(), 0) + fn default<'n>(locals: &'n mut HashSet) -> Generator<'n> { + Self::new(locals, 0) } - fn child(&self) -> Generator<'a> { - Self::new(self.locals.clone(), self.indent) + fn child<'n>(&'n mut self) -> Generator<'n> { + Self::new(self.locals, self.indent) } fn indent(&mut self) { @@ -297,9 +297,12 @@ impl<'a> Generator<'a> { let path = path::find_template_from_path(&path, None); let src = path::get_template_source(&path); let nodes = parser::parse(&src); - let mut gen = self.child(); - gen.handle(&nodes); - self.buf.push_str(&gen.result()); + let nested = { + let mut gen = self.child(); + gen.handle(&nodes); + gen.result() + }; + self.buf.push_str(&nested); self.flush_ws(ws); } @@ -470,7 +473,8 @@ pub fn generate(ast: &syn::DeriveInput, path: &str, mut nodes: Vec) -> Str } } - let mut gen = Generator::default(); + let mut locals = HashSet::new(); + let mut gen = Generator::default(&mut locals); if !blocks.is_empty() { if base.is_none() { gen.define_trait(path, &block_names); -- cgit