Skip to content

Commit 3a2ba5c

Browse files
committed
Make the signature of templates cleaner.
1 parent f4e87dd commit 3a2ba5c

19 files changed

Lines changed: 38 additions & 37 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ project adheres to
1010
## Unreleased
1111

1212
* Current stable rust is 1.57, MSRV is now 1.46.0.
13+
* Breaking change: The generated template functions have a simpler
14+
signature.
1315

1416

1517
## Release 0.13.4 - 2021-06-25

examples/actix/src/actix_ructe.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::io::Write;
2-
31
macro_rules! render {
42
($template:path) => (Render(|o| $template(o)));
53
($template:path, $($arg:expr),*) => {{
@@ -12,9 +10,9 @@ macro_rules! render {
1210
}};
1311
}
1412

15-
pub struct Render<T: FnOnce(&mut dyn Write) -> std::io::Result<()>>(pub T);
13+
pub struct Render<T: FnOnce(&mut Vec<u8>) -> std::io::Result<()>>(pub T);
1614

17-
impl<T: FnOnce(&mut dyn Write) -> std::io::Result<()>> From<Render<T>>
15+
impl<T: FnOnce(&mut Vec<u8>) -> std::io::Result<()>> From<Render<T>>
1816
for actix_web::body::Body
1917
{
2018
fn from(t: Render<T>) -> Self {

examples/actix/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl actix_web::error::ResponseError for ExampleAppError {
107107

108108
/// This method can be used as a "template tag", i.e. a method that
109109
/// can be called directly from a template.
110-
fn footer(out: &mut dyn Write) -> io::Result<()> {
110+
fn footer<W: Write>(out: &mut W) -> io::Result<()> {
111111
templates::footer(
112112
out,
113113
&[

examples/gotham/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn homepage(state: State) -> (State, Response<Body>) {
4040

4141
/// This method can be used as a "template tag", that is a method that
4242
/// can be called directly from a template.
43-
fn footer(out: &mut dyn Write) -> io::Result<()> {
43+
fn footer(out: &mut impl Write) -> io::Result<()> {
4444
templates::footer(
4545
out,
4646
&[
@@ -64,7 +64,7 @@ pub struct FilePath {
6464
fn static_file(state: State) -> (State, Response<Body>) {
6565
let res = {
6666
let FilePath { ref name } = FilePath::borrow_from(&state);
67-
if let Some(data) = statics::StaticFile::get(&name) {
67+
if let Some(data) = statics::StaticFile::get(name) {
6868
Response::builder()
6969
.status(StatusCode::OK)
7070
.header(CONTENT_TYPE, data.mime.as_ref())

examples/gotham/src/ructe_response.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use gotham::hyper::http::header::CONTENT_TYPE;
22
use gotham::hyper::{Body, Response, StatusCode};
33
use gotham::state::State;
44
use mime::TEXT_HTML_UTF_8;
5-
use std::io::{self, Write};
5+
use std::io;
66

77
pub trait RucteResponse: Sized {
88
fn html<F>(self, do_render: F) -> (Self, Response<Body>)
99
where
10-
F: FnOnce(&mut dyn Write) -> io::Result<()>;
10+
F: FnOnce(&mut Vec<u8>) -> io::Result<()>;
1111
}
1212

1313
impl RucteResponse for State {
1414
fn html<F>(self, do_render: F) -> (Self, Response<Body>)
1515
where
16-
F: FnOnce(&mut dyn Write) -> io::Result<()>,
16+
F: FnOnce(&mut Vec<u8>) -> io::Result<()>,
1717
{
1818
let mut buf = Vec::new();
1919
let res = match do_render(&mut buf) {

examples/iron/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn frontpage(_: &mut Request) -> IronResult<Response> {
3636

3737
/// This method can be used as a "template tag", that is a method that
3838
/// can be called directly from a template.
39-
fn footer(out: &mut dyn Write) -> io::Result<()> {
39+
fn footer(out: &mut impl Write) -> io::Result<()> {
4040
templates::footer(
4141
out,
4242
&[

examples/nickel/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate nickel;
33
extern crate time;
44

55
use nickel::hyper::header::{ContentType, Expires, HttpDate};
6+
use nickel::hyper::server::Streaming;
67
use nickel::status::StatusCode;
78
use nickel::{Halt, HttpRouter, MiddlewareResult, Nickel, Request, Response};
89
use std::io::{self, Write};
@@ -50,7 +51,7 @@ fn page<'mw>(
5051

5152
fn render<F>(res: Response, do_render: F) -> MiddlewareResult
5253
where
53-
F: FnOnce(&mut dyn Write) -> io::Result<()>,
54+
F: FnOnce(&mut Response<(), Streaming>) -> io::Result<()>,
5455
{
5556
let mut stream = res.start()?;
5657
match do_render(&mut stream) {
@@ -61,7 +62,7 @@ where
6162

6263
/// This method can be used as a "template tag", that is a method that
6364
/// can be called directly from a template.
64-
fn footer(out: &mut dyn Write) -> io::Result<()> {
65+
fn footer(out: &mut impl Write) -> io::Result<()> {
6566
templates::footer(
6667
out,
6768
&[

examples/simple/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(dead_code)] // Most templates here are only used in tests.
22

3-
use std::io::{self, Write};
3+
use std::io;
44

55
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
66
use crate::templates::*;
@@ -11,7 +11,7 @@ fn main() {
1111

1212
fn r2s<Call>(call: Call) -> String
1313
where
14-
Call: FnOnce(&mut dyn Write) -> io::Result<()>,
14+
Call: FnOnce(&mut Vec<u8>) -> io::Result<()>,
1515
{
1616
let mut buf = Vec::new();
1717
call(&mut buf).unwrap();

examples/simple/templates/issue_66.rs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
text.push('C');
88
}
99

10-
return text;
10+
text
1111
})

examples/static-sass/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020
mod test {
2121
use crate::templates::statics::{StaticFile, STATICS};
2222
use crate::templates::*;
23-
use std::io::{self, Write};
23+
use std::io;
2424

2525
#[test]
2626
fn page_w_static() {
@@ -74,7 +74,7 @@ mod test {
7474

7575
fn r2s<Call>(call: Call) -> String
7676
where
77-
Call: FnOnce(&mut dyn Write) -> io::Result<()>,
77+
Call: FnOnce(&mut Vec<u8>) -> io::Result<()>,
7878
{
7979
let mut buf = Vec::new();
8080
call(&mut buf).unwrap();

0 commit comments

Comments
 (0)