Skip to content

Commit

Permalink
Use reference to concrete possibly-unsized type
Browse files Browse the repository at this point in the history
Avoids use of `dyn`
  • Loading branch information
kornelski committed Feb 1, 2020
1 parent 5c86c60 commit 26336f5
Showing 1 changed file with 7 additions and 7 deletions.
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(&self, out: &mut dyn Write) -> 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(&self, out: &mut dyn Write) -> 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(&self, out: &mut dyn Write) -> 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>(&'a mut dyn Write);
struct ToHtmlEscapingWriter<'w, W: ?Sized>(&'w mut W);

impl<'a> Write for ToHtmlEscapingWriter<'a> {
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> Write for ToHtmlEscapingWriter<'a> {
}
}

impl<'a> ToHtmlEscapingWriter<'a> {
impl<'w, W> ToHtmlEscapingWriter<'w, W> where W: ?Sized, for<'a> &'a mut W: Write {
#[inline(never)]
fn write_one_byte_escaped(
out: &mut impl Write,
mut out: &mut W,
data: &[u8],
) -> io::Result<usize> {
let next = data.get(0);
Expand Down

0 comments on commit 26336f5

Please sign in to comment.