Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved no-receiver size_hint method to a separate trait #270

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion askama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ pub use askama_escape::{Html, Text};
pub trait Template {
/// Helper method which allocates a new `String` and renders into it
fn render(&self) -> Result<String> {
let mut buf = String::with_capacity(Self::size_hint());
let mut buf = String::with_capacity(self.dyn_size_hint());
self.render_into(&mut buf)?;
Ok(buf)
}
Expand All @@ -502,6 +502,10 @@ pub trait Template {
fn extension() -> Option<&'static str>
where
Self: Sized;
fn dyn_size_hint(&self) -> usize;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say maybe we can just name this size_hint() for now? Not sure if the conflict causes issues in the common case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they have the same name then importing SizedTemplate and trying to use the receiverless size_hint() will give a "multiple applicable items in scope" compile error if Template is also imported.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think I'm okay with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it basically make the receiverless size_hint() unusable? Or is there a way to disambiguate?

(Giving extension() the same treatment causes the same compile error in testing/tests/simple.rs.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do something like SizedTemplate::extension(&t) or <t as SizedTemplate>::extension().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, it works. I've updated the PR.

}

pub trait TemplateSizeHint {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this to SizedTemplate and move both extension() and size_hint() into it?

/// Provides an conservative estimate of the expanded length of the rendered template
fn size_hint() -> usize;
}
Expand Down
8 changes: 8 additions & 0 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ impl<'a> Generator<'a> {
));
buf.writeln("}");

buf.writeln("fn dyn_size_hint(&self) -> usize {");
buf.writeln(&format!("{}", size_hint));
buf.writeln("}");

buf.writeln("}");

self.write_header(buf, "::askama::TemplateSizeHint", None);

buf.writeln("fn size_hint() -> usize {");
buf.writeln(&format!("{}", size_hint));
buf.writeln("}");
Expand Down