From c565965865d8d6cc8bf6ab287456180f371036a1 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 10 Nov 2017 13:42:32 -0800 Subject: [PATCH] rename BinaryBody --- examples/multipart/Cargo.toml | 3 +- examples/websocket-chat/Cargo.toml | 4 +- src/body.rs | 190 ++++++++++++++--------------- src/context.rs | 4 +- src/lib.rs | 2 +- src/route.rs | 4 +- 6 files changed, 103 insertions(+), 104 deletions(-) diff --git a/examples/multipart/Cargo.toml b/examples/multipart/Cargo.toml index dfd548085..420532ed5 100644 --- a/examples/multipart/Cargo.toml +++ b/examples/multipart/Cargo.toml @@ -9,6 +9,5 @@ path = "src/main.rs" [dependencies] env_logger = "*" -#actix = "0.3" -actix = { git = "https://github.com/actix/actix.git" } +actix = "^0.3.1" actix-web = { path = "../../" } diff --git a/examples/websocket-chat/Cargo.toml b/examples/websocket-chat/Cargo.toml index f5d27cca9..43521ac93 100644 --- a/examples/websocket-chat/Cargo.toml +++ b/examples/websocket-chat/Cargo.toml @@ -24,6 +24,6 @@ serde = "1.0" serde_json = "1.0" serde_derive = "1.0" -#actix = "0.3" -actix = { git = "https://github.com/actix/actix.git" } +actix = "^0.3.1" +#actix = { git = "https://github.com/actix/actix.git" } actix-web = { path = "../../" } diff --git a/src/body.rs b/src/body.rs index 8b1d18e7e..405d6dfc0 100644 --- a/src/body.rs +++ b/src/body.rs @@ -11,7 +11,7 @@ pub enum Body { /// Empty response. `Content-Length` header is set to `0` Empty, /// Specific response body. - Binary(BinaryBody), + Binary(Binary), /// Streaming response body with specified length. Length(u64), /// Unspecified streaming response. Developer is responsible for setting @@ -24,7 +24,7 @@ pub enum Body { /// Represents various types of binary body. /// `Content-Length` header is set to length of the body. #[derive(Debug)] -pub enum BinaryBody { +pub enum Binary { /// Bytes body Bytes(Bytes), /// Static slice @@ -60,143 +60,143 @@ impl Body { /// Create body from slice (copy) pub fn from_slice(s: &[u8]) -> Body { - Body::Binary(BinaryBody::Bytes(Bytes::from(s))) + Body::Binary(Binary::Bytes(Bytes::from(s))) } } -impl From for Body where T: Into{ +impl From for Body where T: Into{ fn from(b: T) -> Body { Body::Binary(b.into()) } } -impl BinaryBody { +impl Binary { pub fn is_empty(&self) -> bool { self.len() == 0 } pub fn len(&self) -> usize { match *self { - BinaryBody::Bytes(ref bytes) => bytes.len(), - BinaryBody::Slice(slice) => slice.len(), - BinaryBody::SharedBytes(ref bytes) => bytes.len(), - BinaryBody::ArcSharedBytes(ref bytes) => bytes.len(), - BinaryBody::SharedString(ref s) => s.len(), - BinaryBody::ArcSharedString(ref s) => s.len(), + Binary::Bytes(ref bytes) => bytes.len(), + Binary::Slice(slice) => slice.len(), + Binary::SharedBytes(ref bytes) => bytes.len(), + Binary::ArcSharedBytes(ref bytes) => bytes.len(), + Binary::SharedString(ref s) => s.len(), + Binary::ArcSharedString(ref s) => s.len(), } } /// Create binary body from slice - pub fn from_slice(s: &[u8]) -> BinaryBody { - BinaryBody::Bytes(Bytes::from(s)) + pub fn from_slice(s: &[u8]) -> Binary { + Binary::Bytes(Bytes::from(s)) } } -impl From<&'static str> for BinaryBody { - fn from(s: &'static str) -> BinaryBody { - BinaryBody::Slice(s.as_ref()) +impl From<&'static str> for Binary { + fn from(s: &'static str) -> Binary { + Binary::Slice(s.as_ref()) } } -impl From<&'static [u8]> for BinaryBody { - fn from(s: &'static [u8]) -> BinaryBody { - BinaryBody::Slice(s) +impl From<&'static [u8]> for Binary { + fn from(s: &'static [u8]) -> Binary { + Binary::Slice(s) } } -impl From> for BinaryBody { - fn from(vec: Vec) -> BinaryBody { - BinaryBody::Bytes(Bytes::from(vec)) +impl From> for Binary { + fn from(vec: Vec) -> Binary { + Binary::Bytes(Bytes::from(vec)) } } -impl From for BinaryBody { - fn from(s: String) -> BinaryBody { - BinaryBody::Bytes(Bytes::from(s)) +impl From for Binary { + fn from(s: String) -> Binary { + Binary::Bytes(Bytes::from(s)) } } -impl<'a> From<&'a String> for BinaryBody { - fn from(s: &'a String) -> BinaryBody { - BinaryBody::Bytes(Bytes::from(AsRef::<[u8]>::as_ref(&s))) +impl<'a> From<&'a String> for Binary { + fn from(s: &'a String) -> Binary { + Binary::Bytes(Bytes::from(AsRef::<[u8]>::as_ref(&s))) } } -impl From for BinaryBody { - fn from(s: Bytes) -> BinaryBody { - BinaryBody::Bytes(s) +impl From for Binary { + fn from(s: Bytes) -> Binary { + Binary::Bytes(s) } } -impl From for BinaryBody { - fn from(s: BytesMut) -> BinaryBody { - BinaryBody::Bytes(s.freeze()) +impl From for Binary { + fn from(s: BytesMut) -> Binary { + Binary::Bytes(s.freeze()) } } -impl From> for BinaryBody { - fn from(body: Rc) -> BinaryBody { - BinaryBody::SharedBytes(body) +impl From> for Binary { + fn from(body: Rc) -> Binary { + Binary::SharedBytes(body) } } -impl<'a> From<&'a Rc> for BinaryBody { - fn from(body: &'a Rc) -> BinaryBody { - BinaryBody::SharedBytes(Rc::clone(body)) +impl<'a> From<&'a Rc> for Binary { + fn from(body: &'a Rc) -> Binary { + Binary::SharedBytes(Rc::clone(body)) } } -impl From> for BinaryBody { - fn from(body: Arc) -> BinaryBody { - BinaryBody::ArcSharedBytes(body) +impl From> for Binary { + fn from(body: Arc) -> Binary { + Binary::ArcSharedBytes(body) } } -impl<'a> From<&'a Arc> for BinaryBody { - fn from(body: &'a Arc) -> BinaryBody { - BinaryBody::ArcSharedBytes(Arc::clone(body)) +impl<'a> From<&'a Arc> for Binary { + fn from(body: &'a Arc) -> Binary { + Binary::ArcSharedBytes(Arc::clone(body)) } } -impl From> for BinaryBody { - fn from(body: Rc) -> BinaryBody { - BinaryBody::SharedString(body) +impl From> for Binary { + fn from(body: Rc) -> Binary { + Binary::SharedString(body) } } -impl<'a> From<&'a Rc> for BinaryBody { - fn from(body: &'a Rc) -> BinaryBody { - BinaryBody::SharedString(Rc::clone(body)) +impl<'a> From<&'a Rc> for Binary { + fn from(body: &'a Rc) -> Binary { + Binary::SharedString(Rc::clone(body)) } } -impl From> for BinaryBody { - fn from(body: Arc) -> BinaryBody { - BinaryBody::ArcSharedString(body) +impl From> for Binary { + fn from(body: Arc) -> Binary { + Binary::ArcSharedString(body) } } -impl<'a> From<&'a Arc> for BinaryBody { - fn from(body: &'a Arc) -> BinaryBody { - BinaryBody::ArcSharedString(Arc::clone(body)) +impl<'a> From<&'a Arc> for Binary { + fn from(body: &'a Arc) -> Binary { + Binary::ArcSharedString(Arc::clone(body)) } } -impl AsRef<[u8]> for BinaryBody { +impl AsRef<[u8]> for Binary { fn as_ref(&self) -> &[u8] { match *self { - BinaryBody::Bytes(ref bytes) => bytes.as_ref(), - BinaryBody::Slice(slice) => slice, - BinaryBody::SharedBytes(ref bytes) => bytes.as_ref(), - BinaryBody::ArcSharedBytes(ref bytes) => bytes.as_ref(), - BinaryBody::SharedString(ref s) => s.as_bytes(), - BinaryBody::ArcSharedString(ref s) => s.as_bytes(), + Binary::Bytes(ref bytes) => bytes.as_ref(), + Binary::Slice(slice) => slice, + Binary::SharedBytes(ref bytes) => bytes.as_ref(), + Binary::ArcSharedBytes(ref bytes) => bytes.as_ref(), + Binary::SharedString(ref s) => s.as_bytes(), + Binary::ArcSharedString(ref s) => s.as_bytes(), } } } -impl From for Frame { - fn from(b: BinaryBody) -> Frame { +impl From for Frame { + fn from(b: Binary) -> Frame { Frame::Payload(Some(b)) } } @@ -207,70 +207,70 @@ mod tests { #[test] fn test_static_str() { - assert_eq!(BinaryBody::from("test").len(), 4); - assert_eq!(BinaryBody::from("test").as_ref(), "test".as_bytes()); + assert_eq!(Binary::from("test").len(), 4); + assert_eq!(Binary::from("test").as_ref(), "test".as_bytes()); } #[test] fn test_static_bytes() { - assert_eq!(BinaryBody::from(b"test".as_ref()).len(), 4); - assert_eq!(BinaryBody::from(b"test".as_ref()).as_ref(), "test".as_bytes()); - assert_eq!(BinaryBody::from_slice(b"test".as_ref()).len(), 4); - assert_eq!(BinaryBody::from_slice(b"test".as_ref()).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(b"test".as_ref()).len(), 4); + assert_eq!(Binary::from(b"test".as_ref()).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from_slice(b"test".as_ref()).len(), 4); + assert_eq!(Binary::from_slice(b"test".as_ref()).as_ref(), "test".as_bytes()); } #[test] fn test_vec() { - assert_eq!(BinaryBody::from(Vec::from("test")).len(), 4); - assert_eq!(BinaryBody::from(Vec::from("test")).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(Vec::from("test")).len(), 4); + assert_eq!(Binary::from(Vec::from("test")).as_ref(), "test".as_bytes()); } #[test] fn test_bytes() { - assert_eq!(BinaryBody::from(Bytes::from("test")).len(), 4); - assert_eq!(BinaryBody::from(Bytes::from("test")).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(Bytes::from("test")).len(), 4); + assert_eq!(Binary::from(Bytes::from("test")).as_ref(), "test".as_bytes()); } #[test] fn test_rc_bytes() { let b = Rc::new(Bytes::from("test")); - assert_eq!(BinaryBody::from(b.clone()).len(), 4); - assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes()); - assert_eq!(BinaryBody::from(&b).len(), 4); - assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(b.clone()).len(), 4); + assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(&b).len(), 4); + assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); } #[test] fn test_ref_string() { let b = Rc::new("test".to_owned()); - assert_eq!(BinaryBody::from(&b).len(), 4); - assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(&b).len(), 4); + assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); } #[test] fn test_rc_string() { let b = Rc::new("test".to_owned()); - assert_eq!(BinaryBody::from(b.clone()).len(), 4); - assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes()); - assert_eq!(BinaryBody::from(&b).len(), 4); - assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(b.clone()).len(), 4); + assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(&b).len(), 4); + assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); } #[test] fn test_arc_bytes() { let b = Arc::new(Bytes::from("test")); - assert_eq!(BinaryBody::from(b.clone()).len(), 4); - assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes()); - assert_eq!(BinaryBody::from(&b).len(), 4); - assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(b.clone()).len(), 4); + assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(&b).len(), 4); + assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); } #[test] fn test_arc_string() { let b = Arc::new("test".to_owned()); - assert_eq!(BinaryBody::from(b.clone()).len(), 4); - assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes()); - assert_eq!(BinaryBody::from(&b).len(), 4); - assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(b.clone()).len(), 4); + assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes()); + assert_eq!(Binary::from(&b).len(), 4); + assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); } } diff --git a/src/context.rs b/src/context.rs index 4b201184a..c2493dd64 100644 --- a/src/context.rs +++ b/src/context.rs @@ -13,7 +13,7 @@ use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCel Envelope, ToEnvelope, RemoteEnvelope}; use task::{IoContext, DrainFut}; -use body::BinaryBody; +use body::Binary; use route::{Route, Frame}; use httpresponse::HttpResponse; @@ -136,7 +136,7 @@ impl HttpContext where A: Actor + Route { } /// Write payload - pub fn write>(&mut self, data: B) { + pub fn write>(&mut self, data: B) { self.stream.push_back(Frame::Payload(Some(data.into()))) } diff --git a/src/lib.rs b/src/lib.rs index 95a8a53f6..7bb19417f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,7 +65,7 @@ pub mod multipart; pub mod middlewares; pub use encoding::ContentEncoding; pub use error::ParseError; -pub use body::{Body, BinaryBody}; +pub use body::{Body, Binary}; pub use application::{Application, ApplicationBuilder}; pub use httprequest::{HttpRequest, UrlEncoded}; pub use httpresponse::{HttpResponse, HttpResponseBuilder}; diff --git a/src/route.rs b/src/route.rs index c2f7e83ff..d5a2b94b8 100644 --- a/src/route.rs +++ b/src/route.rs @@ -8,7 +8,7 @@ use http::{header, Version}; use futures::Stream; use task::{Task, DrainFut}; -use body::BinaryBody; +use body::Binary; use context::HttpContext; use resource::Reply; use payload::Payload; @@ -21,7 +21,7 @@ use httpcodes::HTTPExpectationFailed; #[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))] pub enum Frame { Message(HttpResponse), - Payload(Option), + Payload(Option), Drain(Rc>), }