From 5cfef325b03b25ad96d6c229a5ec3fd6a32f700d Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 15 Dec 2021 14:08:45 +0100 Subject: 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... --- askama_actix/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'askama_actix') diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs index 2496d92..baf9fd6 100644 --- a/askama_actix/src/lib.rs +++ b/askama_actix/src/lib.rs @@ -11,13 +11,12 @@ pub trait TemplateToResponse { impl TemplateToResponse for T { fn to_response(&self) -> HttpResponse { - let mut buffer = BytesMut::with_capacity(self.size_hint()); + let mut buffer = BytesMut::with_capacity(T::SIZE_HINT); if self.render_into(&mut buffer).is_err() { return ErrorInternalServerError("Template parsing error").error_response(); } - let ctype = - askama::mime::extension_to_mime_type(self.extension().unwrap_or("txt")).to_string(); + let ctype = askama::mime::extension_to_mime_type(T::EXTENSION.unwrap_or("txt")).to_string(); HttpResponse::Ok() .content_type(ctype.as_str()) .body(buffer.freeze()) -- cgit