mirror of https://github.com/fafhrd91/actix-web
Add from_bytes/u8_bytes to dev::Payload (#3595)
* feat: Add from_bytes/u8_bytes to dev::Payload This allows convinent construction of Payload from bytes which is useful in middlewares closes actix/actix-web#3589 Add doc comment and changelog entry * implement from<bytes/vec> for payload --------- Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
parent
0796f8e796
commit
df0885cf21
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
- Add `header::CLEAR_SITE_DATA` constant.
|
- Add `header::CLEAR_SITE_DATA` constant.
|
||||||
- Add `Extensions::get_or_insert[_with]()` methods.
|
- Add `Extensions::get_or_insert[_with]()` methods.
|
||||||
|
- Implement `From<Bytes>` for `Payload`.
|
||||||
|
- Implement `From<Vec<u8>>` for `Payload`.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|
|
@ -41,13 +41,31 @@ pin_project! {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> From<crate::h1::Payload> for Payload<S> {
|
impl<S> From<crate::h1::Payload> for Payload<S> {
|
||||||
|
#[inline]
|
||||||
fn from(payload: crate::h1::Payload) -> Self {
|
fn from(payload: crate::h1::Payload) -> Self {
|
||||||
Payload::H1 { payload }
|
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")]
|
#[cfg(feature = "http2")]
|
||||||
impl<S> From<crate::h2::Payload> for Payload<S> {
|
impl<S> From<crate::h2::Payload> for Payload<S> {
|
||||||
|
#[inline]
|
||||||
fn from(payload: crate::h2::Payload) -> Self {
|
fn from(payload: crate::h2::Payload) -> Self {
|
||||||
Payload::H2 { payload }
|
Payload::H2 { payload }
|
||||||
}
|
}
|
||||||
|
@ -55,6 +73,7 @@ impl<S> From<crate::h2::Payload> for Payload<S> {
|
||||||
|
|
||||||
#[cfg(feature = "http2")]
|
#[cfg(feature = "http2")]
|
||||||
impl<S> From<::h2::RecvStream> for Payload<S> {
|
impl<S> From<::h2::RecvStream> for Payload<S> {
|
||||||
|
#[inline]
|
||||||
fn from(stream: ::h2::RecvStream) -> Self {
|
fn from(stream: ::h2::RecvStream) -> Self {
|
||||||
Payload::H2 {
|
Payload::H2 {
|
||||||
payload: crate::h2::Payload::new(stream),
|
payload: crate::h2::Payload::new(stream),
|
||||||
|
@ -63,13 +82,15 @@ impl<S> From<::h2::RecvStream> for Payload<S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BoxedPayloadStream> for Payload {
|
impl From<BoxedPayloadStream> for Payload {
|
||||||
|
#[inline]
|
||||||
fn from(payload: BoxedPayloadStream) -> Self {
|
fn from(payload: BoxedPayloadStream) -> Self {
|
||||||
Payload::Stream { payload }
|
Payload::Stream { payload }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> Payload<S> {
|
impl<S> Payload<S> {
|
||||||
/// 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> {
|
pub fn take(&mut self) -> Payload<S> {
|
||||||
mem::replace(self, Payload::None)
|
mem::replace(self, Payload::None)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue