diff --git a/CHANGELOG.md b/CHANGELOG.md index 832e3a6..fb5cf5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ project adheres to ## Unreleased +* Breaking change: The generated template functions have a simpler + signature. * Improve error handling in optional warp support, PR #109. * Current stable rust is 1.57, MSRV is now 1.46.0. * Update nom dependency to 7.1.0. diff --git a/examples/actix/src/actix_ructe.rs b/examples/actix/src/actix_ructe.rs index 907ff9b..0768e72 100644 --- a/examples/actix/src/actix_ructe.rs +++ b/examples/actix/src/actix_ructe.rs @@ -1,5 +1,3 @@ -use std::io::Write; - macro_rules! render { ($template:path) => (Render(|o| $template(o))); ($template:path, $($arg:expr),*) => {{ @@ -12,9 +10,9 @@ macro_rules! render { }}; } -pub struct Render std::io::Result<()>>(pub T); +pub struct Render) -> std::io::Result<()>>(pub T); -impl std::io::Result<()>> From> +impl) -> std::io::Result<()>> From> for actix_web::body::Body { fn from(t: Render) -> Self { diff --git a/examples/actix/src/main.rs b/examples/actix/src/main.rs index dc9f7b6..5a8a0e1 100644 --- a/examples/actix/src/main.rs +++ b/examples/actix/src/main.rs @@ -107,7 +107,7 @@ impl actix_web::error::ResponseError for ExampleAppError { /// This method can be used as a "template tag", i.e. a method that /// can be called directly from a template. -fn footer(out: &mut dyn Write) -> io::Result<()> { +fn footer(out: &mut impl Write) -> io::Result<()> { templates::footer( out, &[ diff --git a/examples/gotham/src/main.rs b/examples/gotham/src/main.rs index 2880566..63a4304 100644 --- a/examples/gotham/src/main.rs +++ b/examples/gotham/src/main.rs @@ -41,7 +41,7 @@ fn homepage(state: State) -> (State, Response) { /// This method can be used as a "template tag", that is a method that /// can be called directly from a template. -fn footer(out: &mut dyn Write) -> io::Result<()> { +fn footer(out: &mut impl Write) -> io::Result<()> { templates::footer( out, &[ diff --git a/examples/gotham/src/ructe_response.rs b/examples/gotham/src/ructe_response.rs index aecb28b..fcef59e 100644 --- a/examples/gotham/src/ructe_response.rs +++ b/examples/gotham/src/ructe_response.rs @@ -2,18 +2,18 @@ use gotham::hyper::http::header::CONTENT_TYPE; use gotham::hyper::{Body, Response, StatusCode}; use gotham::state::State; use mime::TEXT_HTML_UTF_8; -use std::io::{self, Write}; +use std::io; pub trait RucteResponse: Sized { fn html(self, do_render: F) -> (Self, Response) where - F: FnOnce(&mut dyn Write) -> io::Result<()>; + F: FnOnce(&mut Vec) -> io::Result<()>; } impl RucteResponse for State { fn html(self, do_render: F) -> (Self, Response) where - F: FnOnce(&mut dyn Write) -> io::Result<()>, + F: FnOnce(&mut Vec) -> io::Result<()>, { let mut buf = Vec::new(); let res = match do_render(&mut buf) { diff --git a/examples/iron/src/main.rs b/examples/iron/src/main.rs index 58ceeb0..98746ee 100644 --- a/examples/iron/src/main.rs +++ b/examples/iron/src/main.rs @@ -36,7 +36,7 @@ fn frontpage(_: &mut Request) -> IronResult { /// This method can be used as a "template tag", that is a method that /// can be called directly from a template. -fn footer(out: &mut dyn Write) -> io::Result<()> { +fn footer(out: &mut impl Write) -> io::Result<()> { templates::footer( out, &[ diff --git a/examples/nickel/src/main.rs b/examples/nickel/src/main.rs index 54e8d46..cf87fc5 100644 --- a/examples/nickel/src/main.rs +++ b/examples/nickel/src/main.rs @@ -3,6 +3,7 @@ extern crate nickel; extern crate time; use nickel::hyper::header::{ContentType, Expires, HttpDate}; +use nickel::hyper::server::Streaming; use nickel::status::StatusCode; use nickel::{Halt, HttpRouter, MiddlewareResult, Nickel, Request, Response}; use std::io::{self, Write}; @@ -50,7 +51,7 @@ fn page<'mw>( fn render(res: Response, do_render: F) -> MiddlewareResult where - F: FnOnce(&mut dyn Write) -> io::Result<()>, + F: FnOnce(&mut Response<(), Streaming>) -> io::Result<()>, { let mut stream = res.start()?; match do_render(&mut stream) { @@ -61,7 +62,7 @@ where /// This method can be used as a "template tag", that is a method that /// can be called directly from a template. -fn footer(out: &mut dyn Write) -> io::Result<()> { +fn footer(out: &mut impl Write) -> io::Result<()> { templates::footer( out, &[ diff --git a/examples/simple/src/main.rs b/examples/simple/src/main.rs index 8c706fc..8cc1e70 100644 --- a/examples/simple/src/main.rs +++ b/examples/simple/src/main.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] // Most templates here are only used in tests. -use std::io::{self, Write}; +use std::io; include!(concat!(env!("OUT_DIR"), "/templates.rs")); use crate::templates::*; @@ -11,7 +11,7 @@ fn main() { fn r2s(call: Call) -> String where - Call: FnOnce(&mut dyn Write) -> io::Result<()>, + Call: FnOnce(&mut Vec) -> io::Result<()>, { let mut buf = Vec::new(); call(&mut buf).unwrap(); diff --git a/examples/simple/templates/issue_66.rs.html b/examples/simple/templates/issue_66.rs.html index 9ed32fa..e281e01 100644 --- a/examples/simple/templates/issue_66.rs.html +++ b/examples/simple/templates/issue_66.rs.html @@ -7,5 +7,5 @@ text.push('C'); } - return text; + text }) diff --git a/examples/static-sass/src/main.rs b/examples/static-sass/src/main.rs index be863e3..47a0087 100644 --- a/examples/static-sass/src/main.rs +++ b/examples/static-sass/src/main.rs @@ -20,7 +20,7 @@ fn main() { mod test { use crate::templates::statics::{StaticFile, STATICS}; use crate::templates::*; - use std::io::{self, Write}; + use std::io; #[test] fn page_w_static() { @@ -74,7 +74,7 @@ mod test { fn r2s(call: Call) -> String where - Call: FnOnce(&mut dyn Write) -> io::Result<()>, + Call: FnOnce(&mut Vec) -> io::Result<()>, { let mut buf = Vec::new(); call(&mut buf).unwrap(); diff --git a/examples/statics/src/main.rs b/examples/statics/src/main.rs index eb7fd08..5bdcc4d 100644 --- a/examples/statics/src/main.rs +++ b/examples/statics/src/main.rs @@ -66,7 +66,7 @@ fn test_all_statics_known() { #[cfg(test)] fn r2s(call: Call) -> String where - Call: FnOnce(&mut dyn Write) -> io::Result<()>, + Call: FnOnce(&mut Vec) -> io::Result<()>, { let mut buf = Vec::new(); call(&mut buf).unwrap(); diff --git a/examples/tide/src/main.rs b/examples/tide/src/main.rs index 88afc9a..3ac0b79 100644 --- a/examples/tide/src/main.rs +++ b/examples/tide/src/main.rs @@ -44,7 +44,7 @@ async fn frontpage(_req: Request<()>) -> Result { /// interface to get a file by url path. async fn static_file(req: Request<()>) -> Result { let path = req.param("path")?; - StaticFile::get(&path) + StaticFile::get(path) .ok_or_else(|| Error::from_str(StatusCode::NotFound, "not found")) .map(static_response) } @@ -70,7 +70,7 @@ const DAY: Duration = Duration::from_secs(24 * 60 * 60); /// This method can be used as a "template tag", i.e. a method that /// can be called directly from a template. -fn footer(out: &mut dyn Write) -> io::Result<()> { +fn footer(out: &mut impl Write) -> io::Result<()> { templates::footer( out, &[ diff --git a/examples/tide/src/ructe_tide.rs b/examples/tide/src/ructe_tide.rs index 1478e07..3c78ad3 100644 --- a/examples/tide/src/ructe_tide.rs +++ b/examples/tide/src/ructe_tide.rs @@ -34,7 +34,7 @@ pub trait Render { /// ``` fn render(&mut self, call: Call) -> std::io::Result<()> where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>; + Call: FnOnce(&mut Vec) -> std::io::Result<()>; /// Render a template to the html body of self. /// @@ -44,13 +44,13 @@ pub trait Render { /// [`HTML`]: ../../tide/http/mime/constant.HTML.html fn render_html(&mut self, call: Call) -> std::io::Result<()> where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>; + Call: FnOnce(&mut Vec) -> std::io::Result<()>; } impl Render for tide::Response { fn render(&mut self, call: Call) -> std::io::Result<()> where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>, + Call: FnOnce(&mut Vec) -> std::io::Result<()>, { let mut buf = Vec::new(); call(&mut buf)?; @@ -60,7 +60,7 @@ impl Render for tide::Response { fn render_html(&mut self, call: Call) -> std::io::Result<()> where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>, + Call: FnOnce(&mut Vec) -> std::io::Result<()>, { self.render(call)?; self.set_content_type(tide::http::mime::HTML); @@ -97,7 +97,7 @@ pub trait RenderBuilder { /// ``` fn render(self, call: Call) -> tide::ResponseBuilder where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>; + Call: FnOnce(&mut Vec) -> std::io::Result<()>; /// Render a template to the html body of self. /// @@ -107,13 +107,13 @@ pub trait RenderBuilder { /// [`HTML`]: ../../tide/http/mime/constant.HTML.html fn render_html(self, call: Call) -> tide::ResponseBuilder where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>; + Call: FnOnce(&mut Vec) -> std::io::Result<()>; } impl RenderBuilder for tide::ResponseBuilder { fn render(self, call: Call) -> tide::ResponseBuilder where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>, + Call: FnOnce(&mut Vec) -> std::io::Result<()>, { let mut buf = Vec::new(); match call(&mut buf) { @@ -131,7 +131,7 @@ impl RenderBuilder for tide::ResponseBuilder { fn render_html(self, call: Call) -> tide::ResponseBuilder where - Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>, + Call: FnOnce(&mut Vec) -> std::io::Result<()>, { self.content_type(tide::http::mime::HTML).render(call) } diff --git a/examples/warp03/src/main.rs b/examples/warp03/src/main.rs index 134e02c..3d88719 100644 --- a/examples/warp03/src/main.rs +++ b/examples/warp03/src/main.rs @@ -73,7 +73,7 @@ async fn arg_handler(what: String) -> Result { /// This method can be used as a "template tag", i.e. a method that /// can be called directly from a template. -fn footer(out: &mut dyn Write) -> io::Result<()> { +fn footer(out: &mut impl Write) -> io::Result<()> { templates::footer( out, &[ diff --git a/src/template.rs b/src/template.rs index ef3b028..01a6bbc 100644 --- a/src/template.rs +++ b/src/template.rs @@ -40,7 +40,7 @@ impl Template { writeln!( out, "\n\ - pub fn {name}(mut _ructe_out_: &mut W{args}) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write {{\n\ + pub fn {name}(_ructe_out_: &mut W{args}) -> io::Result<()> where W: Write {{\n\ {body}\ Ok(())\n\ }}", diff --git a/src/templateexpression.rs b/src/templateexpression.rs index 5a72add..aef39b0 100644 --- a/src/templateexpression.rs +++ b/src/templateexpression.rs @@ -82,7 +82,7 @@ impl TemplateExpression { format!("_ructe_out_.write_all({:?}.as_bytes())?;\n", text) } TemplateExpression::Expression { ref expr } => { - format!("{}.to_html(&mut _ructe_out_)?;\n", expr) + format!("{}.to_html(_ructe_out_)?;\n", expr) } TemplateExpression::ForLoop { ref name, @@ -126,7 +126,7 @@ impl TemplateExpression { ), TemplateExpression::CallTemplate { ref name, ref args } => { format!( - "{}(&mut _ructe_out_{})?;\n", + "{}(_ructe_out_{})?;\n", name, args.iter().format_with("", |arg, f| f(&format_args!( ", {}",