From 15628891efac1cd55f86588ca3814df59e2af122 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 19 Aug 2019 10:24:47 +0100 Subject: [PATCH] Require buf reference to implement Write, not buf itself Allows one level of indirection less --- src/template.rs | 2 +- src/template_utils.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/template.rs b/src/template.rs index c7b72f8..60d4933 100644 --- a/src/template.rs +++ b/src/template.rs @@ -41,7 +41,7 @@ impl Template { writeln!( out, "\n\ - pub fn {name}(mut out: W{args}) -> io::Result<()> {{\n\ + pub fn {name}(mut out: &mut W{args}) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write {{\n\ {body}\ Ok(())\n\ }}", diff --git a/src/template_utils.rs b/src/template_utils.rs index 35ef9c0..0fa01f2 100644 --- a/src/template_utils.rs +++ b/src/template_utils.rs @@ -6,7 +6,7 @@ /// formats the value using Display and then html-encodes the result. pub trait ToHtml { /// Write self to `out`, which is in html representation. - fn to_html(&self, out: &mut W) -> io::Result<()>; + fn to_html(&self, out: &mut W) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write; } /// Wrapper object for data that should be outputted as raw html @@ -16,21 +16,21 @@ pub struct Html(pub T); impl ToHtml for Html { #[inline] - fn to_html(&self, out: &mut W) -> io::Result<()> { + fn to_html(&self, mut out: &mut W) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write { write!(out, "{}", self.0) } } impl ToHtml for T { #[inline] - fn to_html(&self, out: &mut W) -> io::Result<()> { + fn to_html(&self, out: &mut W) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write { write!(ToHtmlEscapingWriter(out), "{}", self) } } -struct ToHtmlEscapingWriter<'a, W: Write + ?Sized>(&'a mut W); +struct ToHtmlEscapingWriter<'w, W: ?Sized>(&'w mut W); -impl<'a, W: Write + ?Sized> Write for ToHtmlEscapingWriter<'a, W> { +impl<'w, W> Write for ToHtmlEscapingWriter<'w, W> where W: ?Sized, for<'a> &'a mut W: Write { #[inline] // This takes advantage of the fact that `write` doesn't have to write everything, // and the call will be retried with the rest of the data @@ -56,10 +56,10 @@ impl<'a, W: Write + ?Sized> Write for ToHtmlEscapingWriter<'a, W> { } } -impl<'a, W: Write + ?Sized> ToHtmlEscapingWriter<'a, W> { +impl<'w, W> ToHtmlEscapingWriter<'w, W> where W: ?Sized, for<'a> &'a mut W: Write { #[inline(never)] fn write_one_byte_escaped( - out: &mut W, + mut out: &mut W, data: &[u8], ) -> io::Result { let next = data.get(0);