Add #call_box instruction

This commit is contained in:
Chris Wong 2015-09-24 11:50:33 +12:00
parent 7d124e616e
commit 0311bab45b
3 changed files with 44 additions and 18 deletions
maud_macros

View file

@ -158,6 +158,11 @@ impl<'cx, 'i> Parser<'cx, 'i> {
let func = try!(self.splice(sp)); let func = try!(self.splice(sp));
self.render.emit_call(func); self.render.emit_call(func);
}, },
[pound!(), ident!(sp, name), ..] if name.name == "call_box" => {
self.shift(2);
let func = try!(self.splice(sp));
self.render.emit_call_box(func);
},
// Splice // Splice
[ref tt @ dollar!(), dollar!(), ..] => { [ref tt @ dollar!(), dollar!(), ..] => {
self.shift(2); self.shift(2);

View file

@ -197,6 +197,16 @@ impl<'cx> Renderer<'cx> {
let stmt = self.wrap_try(expr); let stmt = self.wrap_try(expr);
self.push(stmt); self.push(stmt);
} }
pub fn emit_call_box(&mut self, func: P<Expr>) {
let w = self.writer;
let expr = quote_expr!(self.cx,
::std::boxed::FnBox::call_box(
$func,
(&mut *$w as &mut ::std::fmt::Write,)));
let stmt = self.wrap_try(expr);
self.push(stmt);
}
} }
fn html_escape(s: &str) -> String { fn html_escape(s: &str) -> String {

View file

@ -1,10 +1,8 @@
#![feature(plugin)] #![feature(fnbox, plugin)]
#![plugin(maud_macros)] #![plugin(maud_macros)]
extern crate maud; extern crate maud;
use std::fmt;
#[test] #[test]
fn literals() { fn literals() {
let mut s = String::new(); let mut s = String::new();
@ -274,22 +272,35 @@ mod issue_10 {
} }
} }
#[test] mod subtemplates {
fn call() { use std::fmt;
fn ducks(w: &mut fmt::Write) -> fmt::Result { fn ducks(w: &mut fmt::Write) -> fmt::Result {
write!(w, "Ducks") write!(w, "Ducks")
} }
let mut s = String::new();
let swans = |yes| #[test]
if yes { fn call() {
|w: &mut fmt::Write| write!(w, "Swans") let mut s = String::new();
} else { let swans = |yes|
panic!("oh noes") if yes {
}; |w: &mut fmt::Write| write!(w, "Swans")
html!(s, { } else {
#call ducks panic!("oh noes")
#call (|w: &mut fmt::Write| write!(w, "Geese")) };
#call swans(true) html!(s, {
}).unwrap(); #call ducks
assert_eq!(s, "DucksGeeseSwans"); #call (|w: &mut fmt::Write| write!(w, "Geese"))
#call swans(true)
}).unwrap();
assert_eq!(s, "DucksGeeseSwans");
}
#[test]
fn call_box() {
let mut s = String::new();
let ducks = Box::new(ducks);
html!(s, #call_box ducks).unwrap();
assert_eq!(s, "Ducks");
}
} }