Browse Source

Update yarte to 0.14 (#6231)

Juan Aguilar 4 years ago
parent
commit
9c83c863f6

+ 1 - 1
frameworks/Rust/actix/Cargo.toml

@@ -33,7 +33,7 @@ actix-server = "1.0.4"
 actix-service = "1.0.6"
 snmalloc-rs = "0.2.6"
 askama = "0.8"
-yarte = { version = "0.12", features = ["bytes-buf"] }
+yarte = { version = "0.14", features = ["bytes-buf"] }
 serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
 env_logger = "0.7"

+ 3 - 3
frameworks/Rust/actix/src/main_pg.rs

@@ -8,7 +8,7 @@ use actix_service::map_config;
 use actix_web::dev::{AppConfig, Body, Server};
 use actix_web::http::{header::CONTENT_TYPE, header::SERVER, HeaderValue, StatusCode};
 use actix_web::{web, App, Error, HttpRequest, HttpResponse};
-use bytes::BytesMut;
+use bytes::{Bytes, BytesMut};
 use yarte::ywrite_html;
 
 mod db_pg;
@@ -97,11 +97,11 @@ async fn fortune(db: web::Data<Addr<PgConnection>>) -> Result<HttpResponse, Erro
 
     match res {
         Ok(fortunes) => {
-            let mut body = BytesMut::with_capacity(2048);
+            let mut body = Vec::with_capacity(2048);
             ywrite_html!(body, "{{> fortune }}");
 
             let mut res =
-                HttpResponse::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
+                HttpResponse::with_body(StatusCode::OK, Body::Bytes(Bytes::from(body)));
             res.headers_mut()
                 .insert(SERVER, HeaderValue::from_static("Actix"));
             res.headers_mut().insert(

+ 6 - 4
frameworks/Rust/actix/src/main_platform.rs

@@ -11,7 +11,7 @@ use actix_http::http::{HeaderValue, StatusCode};
 use actix_http::{Error, HttpService, KeepAlive, Request, Response};
 use actix_server::Server;
 use actix_service::{Service, ServiceFactory};
-use bytes::BytesMut;
+use bytes::{Bytes, BytesMut};
 use futures::future::ok;
 use serde_json::to_writer;
 use yarte::ywrite_html;
@@ -66,11 +66,13 @@ impl Service for App {
                 Box::pin(async move {
                     let fortunes = fut.await?;
 
-                    let mut body = BytesMut::with_capacity(2048);
+                    let mut body = Vec::with_capacity(2048);
                     ywrite_html!(body, "{{> fortune }}");
 
-                    let mut res =
-                        Response::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
+                    let mut res = Response::with_body(
+                        StatusCode::OK,
+                        Body::Bytes(Bytes::from(body)),
+                    );
                     let hdrs = res.headers_mut();
                     hdrs.insert(SERVER, h_srv);
                     hdrs.insert(CONTENT_TYPE, h_ct);

+ 1 - 1
frameworks/Rust/actix/src/utils.rs

@@ -32,7 +32,7 @@ impl<'a> io::Write for Writer<'a> {
 }
 
 pub fn get_query_param(query: &str) -> u16 {
-    let q = if let Some(pos) = query.find("q") {
+    let q = if let Some(pos) = query.find('q') {
         query.split_at(pos + 2).1.parse::<u16>().ok().unwrap_or(1)
     } else {
         1

+ 1 - 1
frameworks/Rust/ntex/Cargo.toml

@@ -18,7 +18,7 @@ path = "src/main_raw.rs"
 [dependencies]
 ntex = "0.1.21"
 mimalloc = { version = "0.1.21", default-features = false }
-yarte = { version = "0.12", features = ["bytes-buf", "json"] }
+yarte = { version = "0.14", features = ["bytes-buf", "json"] }
 env_logger = "0.7"
 random-fast-rng = "0.1.1"
 bytes = "0.5.6"

+ 2 - 2
frameworks/Rust/ntex/src/db.rs

@@ -188,10 +188,10 @@ impl PgConnection {
 
             fortunes.sort_by(|it, next| it.message.cmp(&next.message));
 
-            let mut buf = BytesMut::with_capacity(2048);
+            let mut buf = Vec::with_capacity(2048);
             ywrite_html!(buf, "{{> fortune }}");
 
-            Ok(buf.freeze())
+            Ok(Bytes::from(buf))
         }
     }
 }

+ 8 - 7
frameworks/Rust/ntex/src/main.rs

@@ -1,7 +1,7 @@
 #[global_allocator]
 static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
-use bytes::{Bytes, BytesMut};
+use bytes::Bytes;
 use ntex::{http, web};
 use yarte::Serialize;
 
@@ -14,14 +14,15 @@ pub struct Message {
 }
 
 async fn json() -> web::HttpResponse {
+    let mut body = Vec::with_capacity(SIZE);
+    Message {
+        message: "Hello, World!",
+    }
+    .to_bytes_mut(&mut body);
+
     let mut res = web::HttpResponse::with_body(
         http::StatusCode::OK,
-        http::body::Body::Bytes(
-            Message {
-                message: "Hello, World!",
-            }
-            .to_bytes::<BytesMut>(SIZE),
-        ),
+        http::body::Body::Bytes(Bytes::from(body)),
     );
     res.headers_mut().insert(
         http::header::SERVER,

+ 1 - 1
frameworks/Rust/ntex/src/utils.rs

@@ -6,7 +6,7 @@ use atoi::FromRadix10;
 pub const SIZE: usize = 27;
 
 pub fn get_query_param(query: &str) -> u16 {
-    let q = if let Some(pos) = query.find("q") {
+    let q = if let Some(pos) = query.find('q') {
         u16::from_radix_10(query.split_at(pos + 2).1.as_ref()).0
     } else {
         1