diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-12-15 14:08:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 14:08:45 +0100 |
commit | 5cfef325b03b25ad96d6c229a5ec3fd6a32f700d (patch) | |
tree | d1acb8c1f45e2bddc1453eeb5fe07e9858277a4e /askama_shared/src/generator.rs | |
parent | 228f6b2b34913077259e2d54036154a6d6f5bd10 (diff) | |
download | askama-5cfef325b03b25ad96d6c229a5ec3fd6a32f700d.tar.gz askama-5cfef325b03b25ad96d6c229a5ec3fd6a32f700d.tar.bz2 askama-5cfef325b03b25ad96d6c229a5ec3fd6a32f700d.zip |
Use a separate trait for object safety (#579)
This is relatively major change to the main trait's API. For context,
I always started from the concept of monomorphized traits, but later
several contributors asked about object safety. At that point I made
`Template` object-safe, and then even later added a `SizedTemplate`
to make some things easier for people who don't need object safety.
However, having object-safety in the primary trait is bad for
performance (a substantial number of calls into the virtual `Write`
trait is relatively slow), and I don't think those who don't need
object safety should pay for the cost of having it.
Additionally, I feel using associated consts for the extension and
size hint is more idiomatic than having accessor methods. I don't
know why I didn't use these from the start -- maybe associated
consts didn't exist yet, or I didn't yet know how/when to use them.
Askama is pretty old at this point...
Diffstat (limited to 'askama_shared/src/generator.rs')
-rw-r--r-- | askama_shared/src/generator.rs | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 41b81df..56c82aa 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -128,7 +128,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { ) -> Result<(), CompileError> { self.write_header(buf, "::askama::Template", None)?; buf.writeln( - "fn render_into(&self, writer: &mut dyn ::std::fmt::Write) -> \ + "fn render_into(&self, writer: &mut (impl ::std::fmt::Write + ?Sized)) -> \ ::askama::Result<()> {", )?; @@ -160,25 +160,13 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { buf.writeln("Ok(())")?; buf.writeln("}")?; - buf.writeln("fn extension(&self) -> Option<&'static str> {")?; + buf.writeln("const EXTENSION: ::std::option::Option<&'static ::std::primitive::str> = ")?; buf.writeln(&format!("{:?}", self.input.extension()))?; - buf.writeln("}")?; + buf.writeln(";")?; - buf.writeln("fn size_hint(&self) -> usize {")?; + buf.writeln("const SIZE_HINT: ::std::primitive::usize = ")?; buf.writeln(&format!("{}", size_hint))?; - buf.writeln("}")?; - - buf.writeln("}")?; - - self.write_header(buf, "::askama::SizedTemplate", None)?; - - buf.writeln("fn size_hint() -> usize {")?; - buf.writeln(&format!("{}", size_hint))?; - buf.writeln("}")?; - - buf.writeln("fn extension() -> Option<&'static str> {")?; - buf.writeln(&format!("{:?}", self.input.extension()))?; - buf.writeln("}")?; + buf.writeln(";")?; buf.writeln("}")?; Ok(()) |