diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 23ebe43e1..703a4cad1 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -6,6 +6,8 @@ - Add `header::CLEAR_SITE_DATA` constant. - Add `Extensions::get_or_insert[_with]()` methods. +- Implement `From` for `Payload`. +- Implement `From>` for `Payload`. ### Changed diff --git a/actix-http/src/payload.rs b/actix-http/src/payload.rs index 7d476c55f..d7a52417e 100644 --- a/actix-http/src/payload.rs +++ b/actix-http/src/payload.rs @@ -41,13 +41,31 @@ pin_project! { } impl From for Payload { + #[inline] fn from(payload: crate::h1::Payload) -> Self { Payload::H1 { payload } } } +impl From for Payload { + #[inline] + fn from(bytes: Bytes) -> Self { + let (_, mut pl) = crate::h1::Payload::create(true); + pl.unread_data(bytes); + self::Payload::from(pl) + } +} + +impl From> for Payload { + #[inline] + fn from(vec: Vec) -> Self { + Payload::from(Bytes::from(vec)) + } +} + #[cfg(feature = "http2")] impl From for Payload { + #[inline] fn from(payload: crate::h2::Payload) -> Self { Payload::H2 { payload } } @@ -55,6 +73,7 @@ impl From for Payload { #[cfg(feature = "http2")] impl From<::h2::RecvStream> for Payload { + #[inline] fn from(stream: ::h2::RecvStream) -> Self { Payload::H2 { payload: crate::h2::Payload::new(stream), @@ -63,13 +82,15 @@ impl From<::h2::RecvStream> for Payload { } impl From for Payload { + #[inline] fn from(payload: BoxedPayloadStream) -> Self { Payload::Stream { payload } } } impl Payload { - /// 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 { mem::replace(self, Payload::None) }