diff options
author | Anthony Nowell <anowell@gmail.com> | 2017-10-05 21:59:51 -0600 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-11-02 15:01:10 +0100 |
commit | 89a90eb46c822957af5b6aef8615a7d7398e7de4 (patch) | |
tree | 3a5faf88d0276e3de82f2ee4e344a1c08dc171af /askama_shared | |
parent | cc51d201ab9e7ba958363d1f74b7bfd4dd12fecf (diff) | |
download | askama-89a90eb46c822957af5b6aef8615a7d7398e7de4.tar.gz askama-89a90eb46c822957af5b6aef8615a7d7398e7de4.tar.bz2 askama-89a90eb46c822957af5b6aef8615a7d7398e7de4.zip |
Make match ref/deref as needed
Much of this can be yanked out and made simpler
when match-modes lands in stable
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/generator.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 0c8b1e2..701de02 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -161,6 +161,7 @@ impl<'a> Generator<'a> { self.write_header(state, "::askama::Template", &[]); self.writeln("fn render_into(&self, writer: &mut ::std::fmt::Write) -> \ ::askama::Result<()> {"); + self.writeln("#[allow(unused_imports)] use ::std::ops::Deref as HiddenDerefTrait;"); self.handle(state, state.nodes, AstLevel::Top); self.flush_ws(&WS(false, false)); self.writeln("Ok(())"); @@ -199,6 +200,7 @@ impl<'a> Generator<'a> { "fn render_trait_into(&self, timpl: &{}, writer: &mut ::std::fmt::Write) \ -> ::askama::Result<()> {{", state.trait_name)); + self.writeln("#[allow(unused_imports)] use ::std::ops::Deref as HiddenDerefTrait;"); if let Some(nodes) = nodes { self.handle(state, nodes, AstLevel::Top); @@ -439,14 +441,18 @@ impl<'a> Generator<'a> { } self.write("match "); + self.write("(&"); self.visit_expr(expr); + self.write(").deref()"); self.writeln(" {"); for arm in arms { let &(ref ws, ref variant, ref params, ref body) = arm; self.locals.push(); match *variant { - Some(ref param) => { self.visit_match_param(param); }, + Some(ref param) => { + self.visit_match_variant(param); + }, None => self.write("_"), }; if params.len() > 0 { @@ -629,11 +635,28 @@ impl<'a> Generator<'a> { } } + fn visit_match_variant(&mut self, param: &MatchParameter) -> DisplayWrap { + match *param { + MatchParameter::StrLit(s) => self.visit_str_lit(s), + MatchParameter::NumLit(s) => { + // Variants need to be references until match-modes land + self.write("&"); + self.visit_num_lit(s) + }, + MatchParameter::Name(s) => { + self.write("&"); + self.write(s); + DisplayWrap::Unwrapped + } + } + } + fn visit_match_param(&mut self, param: &MatchParameter) -> DisplayWrap { match *param { MatchParameter::NumLit(s) => self.visit_num_lit(s), MatchParameter::StrLit(s) => self.visit_str_lit(s), MatchParameter::Name(s) => { + self.write("ref "); self.write(s); DisplayWrap::Unwrapped } |