Skip to content

Commit

Permalink
Require buf reference to implement Write, not buf itself
Browse files Browse the repository at this point in the history
Allows one level of indirection less
  • Loading branch information
kornelski committed Aug 19, 2019
1 parent 72b7c7e commit ac8951d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Template {
writeln!(
out,
"\n\
pub fn {name}<W: Write>(mut out: W{args}) -> io::Result<()> {{\n\
pub fn {name}<W>(out: &mut W{args}) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write {{\n\
{body}\
Ok(())\n\
}}",
Expand Down
14 changes: 7 additions & 7 deletions src/template_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: Write + ?Sized>(&self, out: &mut W) -> io::Result<()>;
fn to_html<W>(&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
Expand All @@ -16,21 +16,21 @@ pub struct Html<T>(pub T);

impl<T: Display> ToHtml for Html<T> {
#[inline]
fn to_html<W: Write + ?Sized>(&self, out: &mut W) -> io::Result<()> {
fn to_html<W>(&self, mut out: &mut W) -> io::Result<()> where W: ?Sized, for<'a> &'a mut W: Write {
write!(out, "{}", self.0)
}
}

impl<T: Display> ToHtml for T {
#[inline]
fn to_html<W: Write + ?Sized>(&self, out: &mut W) -> io::Result<()> {
fn to_html<W>(&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
Expand All @@ -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<usize> {
let next = data.get(0);
Expand Down

0 comments on commit ac8951d

Please sign in to comment.