diff --git a/rinja/src/lib.rs b/rinja/src/lib.rs index 66537fea..f9d79609 100644 --- a/rinja/src/lib.rs +++ b/rinja/src/lib.rs @@ -58,7 +58,7 @@ mod error; pub mod filters; pub mod helpers; -use std::fmt; +use std::{fmt, io}; pub use rinja_derive::Template; @@ -79,11 +79,11 @@ pub trait Template: fmt::Display { } /// Renders the template to the given `writer` fmt buffer - fn render_into(&self, writer: &mut (impl std::fmt::Write + ?Sized)) -> Result<()>; + fn render_into(&self, writer: &mut W) -> Result<()>; /// Renders the template to the given `writer` io buffer #[inline] - fn write_into(&self, writer: &mut (impl std::io::Write + ?Sized)) -> std::io::Result<()> { + fn write_into(&self, writer: &mut W) -> io::Result<()> { writer.write_fmt(format_args!("{self}")) } @@ -107,7 +107,7 @@ pub trait Template: fmt::Display { impl Template for &T { #[inline] - fn render_into(&self, writer: &mut (impl std::fmt::Write + ?Sized)) -> Result<()> { + fn render_into(&self, writer: &mut W) -> Result<()> { T::render_into(self, writer) } @@ -117,7 +117,7 @@ impl Template for &T { } #[inline] - fn write_into(&self, writer: &mut (impl std::io::Write + ?Sized)) -> std::io::Result<()> { + fn write_into(&self, writer: &mut W) -> io::Result<()> { T::write_into(self, writer) } @@ -136,10 +136,10 @@ pub trait DynTemplate { fn dyn_render(&self) -> Result; /// Renders the template to the given `writer` fmt buffer - fn dyn_render_into(&self, writer: &mut dyn std::fmt::Write) -> Result<()>; + fn dyn_render_into(&self, writer: &mut dyn fmt::Write) -> Result<()>; /// Renders the template to the given `writer` io buffer - fn dyn_write_into(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()>; + fn dyn_write_into(&self, writer: &mut dyn io::Write) -> io::Result<()>; /// Helper function to inspect the template's extension fn extension(&self) -> Option<&'static str>; @@ -156,13 +156,13 @@ impl DynTemplate for T { ::render(self) } - fn dyn_render_into(&self, writer: &mut dyn std::fmt::Write) -> Result<()> { + fn dyn_render_into(&self, writer: &mut dyn fmt::Write) -> Result<()> { ::render_into(self, writer) } #[inline] - fn dyn_write_into(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> { - writer.write_fmt(format_args!("{self}")) + fn dyn_write_into(&self, writer: &mut dyn io::Write) -> io::Result<()> { + ::write_into(self, writer) } fn extension(&self) -> Option<&'static str> { @@ -180,7 +180,7 @@ impl DynTemplate for T { impl fmt::Display for dyn DynTemplate { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.dyn_render_into(f).map_err(|_| ::std::fmt::Error {}) + self.dyn_render_into(f).map_err(|_| fmt::Error {}) } } @@ -195,7 +195,7 @@ mod tests { fn dyn_template() { struct Test; impl Template for Test { - fn render_into(&self, writer: &mut (impl std::fmt::Write + ?Sized)) -> Result<()> { + fn render_into(&self, writer: &mut W) -> Result<()> { Ok(writer.write_str("test")?) } diff --git a/rinja_derive/src/generator.rs b/rinja_derive/src/generator.rs index 2892e956..b95c326c 100644 --- a/rinja_derive/src/generator.rs +++ b/rinja_derive/src/generator.rs @@ -92,8 +92,10 @@ impl<'a> Generator<'a> { fn impl_template(&mut self, ctx: &Context<'a>, buf: &mut Buffer) -> Result<(), CompileError> { self.write_header(buf, format_args!("{CRATE}::Template"), None); buf.writeln(format_args!( - "fn render_into(&self, writer: &mut (impl ::std::fmt::Write + ?Sized)) \ - -> {CRATE}::Result<()> {{", + "fn render_into(&self, writer: &mut RinjaW) -> {CRATE}::Result<()>\n\ + where\n\ + RinjaW: ::core::fmt::Write + ?::core::marker::Sized,\n\ + {{", )); buf.writeln(format_args!("use {CRATE}::filters::AutoEscape as _;")); buf.writeln(format_args!("use ::core::fmt::Write as _;")); diff --git a/rinja_derive/src/tests.rs b/rinja_derive/src/tests.rs index 5d16b3c3..777c2aa6 100644 --- a/rinja_derive/src/tests.rs +++ b/rinja_derive/src/tests.rs @@ -26,7 +26,10 @@ struct Foo;"## } let expected = format!( r#"impl ::rinja::Template for Foo {{ - fn render_into(&self, writer: &mut (impl ::std::fmt::Write + ?Sized)) -> ::rinja::Result<()> {{ + fn render_into(&self, writer: &mut RinjaW) -> ::rinja::Result<()> + where + RinjaW: ::core::fmt::Write + ?::core::marker::Sized, + {{ use ::rinja::filters::AutoEscape as _; use ::core::fmt::Write as _; {new_expected}