From cdbb5dd2b27133d6fffea53be74758b4e1211fb8 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 27 Nov 2020 01:10:05 +0000 Subject: [PATCH] use Bytes re-export closes #202 --- content/docs/extractors.md | 2 +- content/docs/handlers.md | 2 +- examples/async-handlers/src/stream.rs | 8 +++----- examples/errors/src/logging.rs | 2 +- examples/extractors/src/json_two.rs | 2 +- examples/requests/src/manual.rs | 1 - examples/responder-trait/src/main.rs | 2 +- examples/responses/src/chunked.rs | 5 ++--- examples/server/src/signals.rs | 2 +- examples/server/src/ssl.rs | 3 ++- examples/testing/src/integration_two.rs | 3 ++- examples/testing/src/stream_response.rs | 24 ++++++++++++++++-------- 12 files changed, 31 insertions(+), 25 deletions(-) diff --git a/content/docs/extractors.md b/content/docs/extractors.md index 6857122..9c852b9 100644 --- a/content/docs/extractors.md +++ b/content/docs/extractors.md @@ -80,7 +80,7 @@ Actix-web also provides several other extractors: need access to the request. * *String* - You can convert a request's payload to a *String*. [*Example*][stringexample] is available in doc strings. -* *bytes::Bytes* - You can convert a request's payload into *Bytes*. +* *actix_web::web::Bytes* - You can convert a request's payload into *Bytes*. [*Example*][bytesexample] is available in doc strings. * *Payload* - You can access a request's payload. diff --git a/content/docs/handlers.md b/content/docs/handlers.md index 879d6f9..e34fc0d 100644 --- a/content/docs/handlers.md +++ b/content/docs/handlers.md @@ -38,7 +38,7 @@ complex types are involved. ```rust async fn index(_req: HttpRequest) -> impl Responder { - Bytes::from_static(b"Hello world!") + web::Bytes::from_static(b"Hello world!") } ``` diff --git a/examples/async-handlers/src/stream.rs b/examples/async-handlers/src/stream.rs index 96f9d86..d67990a 100644 --- a/examples/async-handlers/src/stream.rs +++ b/examples/async-handlers/src/stream.rs @@ -1,12 +1,10 @@ // -use actix_web::{get, App, Error, HttpResponse, HttpServer}; -use bytes::Bytes; -use futures::future::ok; -use futures::stream::once; +use actix_web::{get, web, App, Error, HttpResponse, HttpServer}; +use futures::{future::ok, stream::once}; #[get("/stream")] async fn stream() -> HttpResponse { - let body = once(ok::<_, Error>(Bytes::from_static(b"test"))); + let body = once(ok::<_, Error>(web::Bytes::from_static(b"test"))); HttpResponse::Ok() .content_type("application/json") diff --git a/examples/errors/src/logging.rs b/examples/errors/src/logging.rs index 82d299c..2ee5a84 100644 --- a/examples/errors/src/logging.rs +++ b/examples/errors/src/logging.rs @@ -1,7 +1,7 @@ // use actix_web::{error, get, middleware::Logger, App, HttpServer, Result}; -use log::debug; use derive_more::{Display, Error}; +use log::debug; #[derive(Debug, Display, Error)] #[display(fmt = "my error: {}", name)] diff --git a/examples/extractors/src/json_two.rs b/examples/extractors/src/json_two.rs index 30d2090..92b892b 100644 --- a/examples/extractors/src/json_two.rs +++ b/examples/extractors/src/json_two.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] // -use actix_web::{error, web, App, FromRequest, HttpResponse, HttpServer, Responder}; +use actix_web::{error, web, App, HttpResponse, HttpServer, Responder}; use serde::Deserialize; #[derive(Deserialize)] diff --git a/examples/requests/src/manual.rs b/examples/requests/src/manual.rs index e7640c0..17f2bba 100644 --- a/examples/requests/src/manual.rs +++ b/examples/requests/src/manual.rs @@ -2,7 +2,6 @@ use actix_web::{error, post, web, App, Error, HttpResponse}; use futures::StreamExt; use serde::{Deserialize, Serialize}; -use serde_json; #[derive(Serialize, Deserialize)] struct MyObj { diff --git a/examples/responder-trait/src/main.rs b/examples/responder-trait/src/main.rs index b494ecc..70d6777 100644 --- a/examples/responder-trait/src/main.rs +++ b/examples/responder-trait/src/main.rs @@ -1,7 +1,7 @@ // use actix_web::{Error, HttpRequest, HttpResponse, Responder}; -use serde::Serialize; use futures::future::{ready, Ready}; +use serde::Serialize; #[derive(Serialize)] struct MyObj { diff --git a/examples/responses/src/chunked.rs b/examples/responses/src/chunked.rs index 13278ba..0700b80 100644 --- a/examples/responses/src/chunked.rs +++ b/examples/responses/src/chunked.rs @@ -1,12 +1,11 @@ // -use actix_web::{get, App, Error, HttpRequest, HttpResponse, HttpServer}; -use bytes::Bytes; +use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer}; use futures::future::ok; use futures::stream::once; #[get("/")] async fn index(_req: HttpRequest) -> HttpResponse { - HttpResponse::Ok().streaming(once(ok::<_, Error>(Bytes::from_static(b"data")))) + HttpResponse::Ok().streaming(once(ok::<_, Error>(web::Bytes::from_static(b"data")))) } // diff --git a/examples/server/src/signals.rs b/examples/server/src/signals.rs index 8437b74..b674c6c 100644 --- a/examples/server/src/signals.rs +++ b/examples/server/src/signals.rs @@ -1,5 +1,5 @@ // -use actix_web::{web, App, HttpResponse, HttpServer, rt::System}; +use actix_web::{rt::System, web, App, HttpResponse, HttpServer}; use std::sync::mpsc; use std::thread; diff --git a/examples/server/src/ssl.rs b/examples/server/src/ssl.rs index f30a4bc..6904069 100644 --- a/examples/server/src/ssl.rs +++ b/examples/server/src/ssl.rs @@ -14,7 +14,8 @@ async fn main() -> std::io::Result<()> { // load ssl keys // to create a self-signed temporary cert for testing: // `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'` - let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); + let mut builder = + SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); builder .set_private_key_file("key.pem", SslFiletype::PEM) .unwrap(); diff --git a/examples/testing/src/integration_two.rs b/examples/testing/src/integration_two.rs index 9808d95..551be8b 100644 --- a/examples/testing/src/integration_two.rs +++ b/examples/testing/src/integration_two.rs @@ -23,7 +23,8 @@ mod tests { App::new() .data(AppState { count: 4 }) .route("/", web::get().to(index)), - ).await; + ) + .await; let req = test::TestRequest::get().uri("/").to_request(); let resp: AppState = test::read_response_json(&mut app, req).await; diff --git a/examples/testing/src/stream_response.rs b/examples/testing/src/stream_response.rs index e81b8ed..1beda16 100644 --- a/examples/testing/src/stream_response.rs +++ b/examples/testing/src/stream_response.rs @@ -1,22 +1,21 @@ // -use std::task::Poll; -use bytes::Bytes; use futures::stream::poll_fn; +use std::task::Poll; use actix_web::http::{ContentEncoding, StatusCode}; -use actix_web::{web, http, App, Error, HttpRequest, HttpResponse}; +use actix_web::{http, web, App, Error, HttpRequest, HttpResponse}; async fn sse(_req: HttpRequest) -> HttpResponse { let mut counter: usize = 5; // yields `data: N` where N in [5; 1] - let server_events = poll_fn(move |_cx| -> Poll>> { + let server_events = poll_fn(move |_cx| -> Poll>> { if counter == 0 { return Poll::Ready(None); } let payload = format!("data: {}\n\n", counter); counter -= 1; - Poll::Ready(Some(Ok(Bytes::from(payload)))) + Poll::Ready(Some(Ok(web::Bytes::from(payload)))) }); HttpResponse::build(StatusCode::OK) @@ -51,15 +50,24 @@ mod tests { // first chunk let (bytes, mut resp) = resp.take_body().into_future().await; - assert_eq!(bytes.unwrap().unwrap(), Bytes::from_static(b"data: 5\n\n")); + assert_eq!( + bytes.unwrap().unwrap(), + web::Bytes::from_static(b"data: 5\n\n") + ); // second chunk let (bytes, mut resp) = resp.take_body().into_future().await; - assert_eq!(bytes.unwrap().unwrap(), Bytes::from_static(b"data: 4\n\n")); + assert_eq!( + bytes.unwrap().unwrap(), + web::Bytes::from_static(b"data: 4\n\n") + ); // remaining part let bytes = test::load_stream(resp.take_body().into_stream()).await; - assert_eq!(bytes.unwrap(), Bytes::from_static(b"data: 3\n\ndata: 2\n\ndata: 1\n\n")); + assert_eq!( + bytes.unwrap(), + web::Bytes::from_static(b"data: 3\n\ndata: 2\n\ndata: 1\n\n") + ); } } //