After fiddling around with zero-allocation solutions, I concluded that all non-allocating approaches are too annoying to work with in realistic code. Using closures leads to yak-shaving with lifetimes; and because Iron needs to take ownership of the response body we often end up cloning the input data anyway. Removing this constraint has let me simplify the entire system, removing a net 300 lines from the library. The `html!` macro no longer takes a writer, and instead returns a `PreEscaped<String>`. This means that the result of an `html!` can be spliced directly into another `html!`, removing the need for the `impl Template` rigmarole. To rub it in, benchmarks show the new code is in fact *faster* than it was before. How lovely.
84 lines
1.8 KiB
Rust
84 lines
1.8 KiB
Rust
#![feature(conservative_impl_trait, plugin)]
|
|
#![plugin(maud_macros)]
|
|
|
|
extern crate maud;
|
|
|
|
use std::fmt;
|
|
|
|
#[test]
|
|
fn issue_13() {
|
|
let owned = String::from("yay");
|
|
let _ = html!((owned));
|
|
// Make sure the `html!` call didn't move it
|
|
let _owned = owned;
|
|
}
|
|
|
|
#[test]
|
|
fn issue_21() {
|
|
macro_rules! greet {
|
|
() => ({
|
|
let name = "Pinkie Pie";
|
|
html!(p { "Hello, " (name) "!" })
|
|
})
|
|
}
|
|
|
|
let s = greet!().into_string();
|
|
assert_eq!(s, "<p>Hello, Pinkie Pie!</p>");
|
|
}
|
|
|
|
#[test]
|
|
fn issue_21_2() {
|
|
macro_rules! greet {
|
|
($name:expr) => ({
|
|
html!(p { "Hello, " ($name) "!" })
|
|
})
|
|
}
|
|
|
|
let s = greet!("Pinkie Pie").into_string();
|
|
assert_eq!(s, "<p>Hello, Pinkie Pie!</p>");
|
|
}
|
|
|
|
#[test]
|
|
fn issue_23() {
|
|
macro_rules! wrapper {
|
|
($($x:tt)*) => {{
|
|
html!($($x)*)
|
|
}}
|
|
}
|
|
|
|
let name = "Lyra";
|
|
let s = wrapper!(p { "Hi, " (name) "!" }).into_string();
|
|
assert_eq!(s, "<p>Hi, Lyra!</p>");
|
|
}
|
|
|
|
#[test]
|
|
fn render_impl() {
|
|
struct R(&'static str);
|
|
impl maud::Render for R {
|
|
fn render(&self, w: &mut fmt::Write) -> fmt::Result {
|
|
w.write_str(self.0)
|
|
}
|
|
}
|
|
|
|
let r = R("pinkie");
|
|
// Since `R` is not `Copy`, this shows that Maud will auto-ref splice
|
|
// arguments to find a `Render` impl
|
|
let s1 = html!((r)).into_string();
|
|
let s2 = html!((r)).into_string();
|
|
assert_eq!(s1, "pinkie");
|
|
assert_eq!(s2, "pinkie");
|
|
}
|
|
|
|
#[test]
|
|
fn render_once_impl() {
|
|
struct Once(String);
|
|
impl maud::RenderOnce for Once {
|
|
fn render_once(self, w: &mut fmt::Write) -> fmt::Result {
|
|
w.write_str(&self.0)
|
|
}
|
|
}
|
|
|
|
let once = Once(String::from("pinkie"));
|
|
let s = html!((once)).into_string();
|
|
assert_eq!(s, "pinkie");
|
|
}
|