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 Aug 18, 2019
1 parent d191e22 commit 72b7c7e
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: Write + ?Sized>(&self, out: &mut W) -> io::Result<()>;
}

/// 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: Write + ?Sized>(&self, out: &mut W) -> io::Result<()> {
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: Write + ?Sized>(&self, out: &mut W) -> io::Result<()> {
write!(ToHtmlEscapingWriter(out), "{}", self)
}
}

struct ToHtmlEscapingWriter<'a>(&'a mut dyn Write);
struct ToHtmlEscapingWriter<'a, W: Write + ?Sized>(&'a mut W);

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

0 comments on commit 72b7c7e

Please sign in to comment.