aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Douman <douman@gmx.se>2019-03-17 09:22:05 +0300
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2019-03-17 09:15:31 +0100
commit72b317b71428a1c0f63f920ebf2eecd26944f651 (patch)
tree6062fdb7b4ef06cec9645a05e256126813523e3b
parent23ff7ad636b4c0e26d3242747d50b7123831a1a8 (diff)
downloadaskama-72b317b71428a1c0f63f920ebf2eecd26944f651.tar.gz
askama-72b317b71428a1c0f63f920ebf2eecd26944f651.tar.bz2
askama-72b317b71428a1c0f63f920ebf2eecd26944f651.zip
Improve actix-web integration by creating bytes buffer instead of string
-rw-r--r--askama/Cargo.toml3
-rw-r--r--askama/src/lib.rs37
2 files changed, 36 insertions, 4 deletions
diff --git a/askama/Cargo.toml b/askama/Cargo.toml
index 172ade4..2b71d19 100644
--- a/askama/Cargo.toml
+++ b/askama/Cargo.toml
@@ -24,7 +24,7 @@ serde-json = ["askama_shared/serde_json"]
serde-yaml = ["askama_shared/serde_yaml"]
with-iron = ["iron", "askama_derive/iron"]
with-rocket = ["rocket", "askama_derive/rocket"]
-with-actix-web = ["actix-web", "askama_derive/actix-web", "mime_guess"]
+with-actix-web = ["actix-web", "askama_derive/actix-web", "mime_guess", "bytes"]
with-gotham = ["gotham", "askama_derive/gotham", "hyper", "mime_guess"]
[dependencies]
@@ -37,6 +37,7 @@ actix-web = { version = "0.7", optional = true }
mime_guess = { version = "2.0.0-alpha", optional = true }
gotham = { version = "0.3", optional = true }
hyper = { version = "0.12", optional = true }
+bytes = { version = "0.4", optional = true }
[package.metadata.docs.rs]
features = [ "serde-json" ]
diff --git a/askama/src/lib.rs b/askama/src/lib.rs
index dea5833..8492d8f 100644
--- a/askama/src/lib.rs
+++ b/askama/src/lib.rs
@@ -490,6 +490,36 @@ pub mod rocket {
pub mod actix_web {
extern crate actix_web;
extern crate mime_guess;
+ extern crate bytes;
+
+ use std::fmt;
+
+ struct BytesWriter {
+ buf: bytes::BytesMut,
+ }
+
+ impl BytesWriter {
+ #[inline]
+ pub fn new() -> Self {
+ Self {
+ buf: bytes::BytesMut::with_capacity(4096)
+ }
+ }
+
+ #[inline]
+ pub fn freeze(self) -> bytes::Bytes {
+ self.buf.freeze()
+ }
+
+ }
+
+ impl fmt::Write for BytesWriter {
+ #[inline]
+ fn write_str(&mut self, buf: &str) -> fmt::Result {
+ self.buf.extend_from_slice(buf.as_bytes());
+ Ok(())
+ }
+ }
// actix_web technically has this as a pub fn in later versions, fs::file_extension_to_mime.
// Older versions that don't have it exposed are easier this way. If ext is empty or no
@@ -506,11 +536,12 @@ pub mod actix_web {
impl<T: super::Template> TemplateIntoResponse for T {
fn into_response(&self) -> Result<HttpResponse, Error> {
- let rsp = self
- .render()
+ let mut buffer = BytesWriter::new();
+ self.render_into(&mut buffer)
.map_err(|_| ErrorInternalServerError("Template parsing error"))?;
+
let ctype = get_mime_type(T::extension().unwrap_or("txt")).to_string();
- Ok(HttpResponse::Ok().content_type(ctype.as_str()).body(rsp))
+ Ok(HttpResponse::Ok().content_type(ctype.as_str()).body(buffer.freeze()))
}
}
}