diff --git a/CHANGES.md b/CHANGES.md index 163d136f..faa7c9f4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,8 @@ ## 0.4.4 (2018-03-xx) +* Allow to use Arc> as response/request body + * Fix handling of requests with an encoded body with a length > 8192 #93 ## 0.4.3 (2018-03-03) diff --git a/src/body.rs b/src/body.rs index ebd011e9..fe630343 100644 --- a/src/body.rs +++ b/src/body.rs @@ -36,6 +36,8 @@ pub enum Binary { /// Shared string body #[doc(hidden)] ArcSharedString(Arc), + /// Shared vec body + SharedVec(Arc>), } impl Body { @@ -115,6 +117,7 @@ impl Binary { Binary::Slice(slice) => slice.len(), Binary::SharedString(ref s) => s.len(), Binary::ArcSharedString(ref s) => s.len(), + Binary::SharedVec(ref s) => s.len(), } } @@ -134,8 +137,9 @@ impl Clone for Binary { match *self { Binary::Bytes(ref bytes) => Binary::Bytes(bytes.clone()), Binary::Slice(slice) => Binary::Bytes(Bytes::from(slice)), - Binary::SharedString(ref s) => Binary::Bytes(Bytes::from(s.as_str())), - Binary::ArcSharedString(ref s) => Binary::Bytes(Bytes::from(s.as_str())), + Binary::SharedString(ref s) => Binary::SharedString(s.clone()), + Binary::ArcSharedString(ref s) => Binary::ArcSharedString(s.clone()), + Binary::SharedVec(ref s) => Binary::SharedVec(s.clone()), } } } @@ -147,6 +151,7 @@ impl Into for Binary { Binary::Slice(slice) => Bytes::from(slice), Binary::SharedString(s) => Bytes::from(s.as_str()), Binary::ArcSharedString(s) => Bytes::from(s.as_str()), + Binary::SharedVec(s) => Bytes::from(AsRef::<[u8]>::as_ref(s.as_ref())), } } } @@ -217,6 +222,18 @@ impl<'a> From<&'a Arc> for Binary { } } +impl From>> for Binary { + fn from(body: Arc>) -> Binary { + Binary::SharedVec(body) + } +} + +impl<'a> From<&'a Arc>> for Binary { + fn from(body: &'a Arc>) -> Binary { + Binary::SharedVec(Arc::clone(body)) + } +} + impl AsRef<[u8]> for Binary { fn as_ref(&self) -> &[u8] { match *self { @@ -224,6 +241,7 @@ impl AsRef<[u8]> for Binary { Binary::Slice(slice) => slice, Binary::SharedString(ref s) => s.as_bytes(), Binary::ArcSharedString(ref s) => s.as_bytes(), + Binary::SharedVec(ref s) => s.as_ref().as_ref(), } } } @@ -304,6 +322,15 @@ mod tests { assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes()); } + #[test] + fn test_shared_vec() { + let b = Arc::new(Vec::from(&b"test"[..])); + assert_eq!(Binary::from(b.clone()).len(), 4); + assert_eq!(Binary::from(b.clone()).as_ref(), &b"test"[..]); + assert_eq!(Binary::from(&b).len(), 4); + assert_eq!(Binary::from(&b).as_ref(), &b"test"[..]); + } + #[test] fn test_bytes_mut() { let b = BytesMut::from("test");