diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index e0698a4ad..703a4cad1 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -6,7 +6,8 @@ - Add `header::CLEAR_SITE_DATA` constant. - Add `Extensions::get_or_insert[_with]()` methods. -- Add `dev::Payload::from_[bytes/u8_bytes]()` methods. +- Implement `From<Bytes>` for `Payload`. +- Implement `From<Vec<u8>>` for `Payload`. ### Changed diff --git a/actix-http/src/payload.rs b/actix-http/src/payload.rs index 11bb78824..d7a52417e 100644 --- a/actix-http/src/payload.rs +++ b/actix-http/src/payload.rs @@ -41,13 +41,31 @@ pin_project! { } impl<S> From<crate::h1::Payload> for Payload<S> { + #[inline] fn from(payload: crate::h1::Payload) -> Self { Payload::H1 { payload } } } +impl<S> From<Bytes> for Payload<S> { + #[inline] + fn from(bytes: Bytes) -> Self { + let (_, mut pl) = crate::h1::Payload::create(true); + pl.unread_data(bytes); + self::Payload::from(pl) + } +} + +impl<S> From<Vec<u8>> for Payload<S> { + #[inline] + fn from(vec: Vec<u8>) -> Self { + Payload::from(Bytes::from(vec)) + } +} + #[cfg(feature = "http2")] impl<S> From<crate::h2::Payload> for Payload<S> { + #[inline] fn from(payload: crate::h2::Payload) -> Self { Payload::H2 { payload } } @@ -55,6 +73,7 @@ impl<S> From<crate::h2::Payload> for Payload<S> { #[cfg(feature = "http2")] impl<S> From<::h2::RecvStream> for Payload<S> { + #[inline] fn from(stream: ::h2::RecvStream) -> Self { Payload::H2 { payload: crate::h2::Payload::new(stream), @@ -63,28 +82,15 @@ impl<S> From<::h2::RecvStream> for Payload<S> { } impl From<BoxedPayloadStream> for Payload { + #[inline] fn from(payload: BoxedPayloadStream) -> Self { Payload::Stream { payload } } } impl<S> Payload<S> { - /// Create Payload from bytes::Bytes - pub fn from_bytes(bytes: bytes::Bytes) -> Payload<S> { - let (_, mut pl) = crate::h1::Payload::create(true); - pl.unread_data(bytes); - self::Payload::from(pl) - } - - /// Create Payload from `Vec<u8>` bytes - pub fn from_u8_bytes(u8_bytes: Vec<u8>) -> Payload<S> { - let (_, mut pl) = crate::h1::Payload::create(true); - let bytes = bytes::Bytes::from(u8_bytes); - pl.unread_data(bytes); - self::Payload::from(pl) - } - - /// Takes current payload and replaces it with `None` value + /// Takes current payload and replaces it with `None` value. + #[must_use] pub fn take(&mut self) -> Payload<S> { mem::replace(self, Payload::None) }