Skip to content

Commit df0885c

Browse files
Ultra-Coderobjtede
andauthored
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 #3589 Add doc comment and changelog entry * implement from<bytes/vec> for payload --------- Co-authored-by: Rob Ede <[email protected]>
1 parent 0796f8e commit df0885c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

actix-http/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Add `header::CLEAR_SITE_DATA` constant.
88
- Add `Extensions::get_or_insert[_with]()` methods.
9+
- Implement `From<Bytes>` for `Payload`.
10+
- Implement `From<Vec<u8>>` for `Payload`.
911

1012
### Changed
1113

actix-http/src/payload.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,39 @@ pin_project! {
4141
}
4242

4343
impl<S> From<crate::h1::Payload> for Payload<S> {
44+
#[inline]
4445
fn from(payload: crate::h1::Payload) -> Self {
4546
Payload::H1 { payload }
4647
}
4748
}
4849

50+
impl<S> From<Bytes> for Payload<S> {
51+
#[inline]
52+
fn from(bytes: Bytes) -> Self {
53+
let (_, mut pl) = crate::h1::Payload::create(true);
54+
pl.unread_data(bytes);
55+
self::Payload::from(pl)
56+
}
57+
}
58+
59+
impl<S> From<Vec<u8>> for Payload<S> {
60+
#[inline]
61+
fn from(vec: Vec<u8>) -> Self {
62+
Payload::from(Bytes::from(vec))
63+
}
64+
}
65+
4966
#[cfg(feature = "http2")]
5067
impl<S> From<crate::h2::Payload> for Payload<S> {
68+
#[inline]
5169
fn from(payload: crate::h2::Payload) -> Self {
5270
Payload::H2 { payload }
5371
}
5472
}
5573

5674
#[cfg(feature = "http2")]
5775
impl<S> From<::h2::RecvStream> for Payload<S> {
76+
#[inline]
5877
fn from(stream: ::h2::RecvStream) -> Self {
5978
Payload::H2 {
6079
payload: crate::h2::Payload::new(stream),
@@ -63,13 +82,15 @@ impl<S> From<::h2::RecvStream> for Payload<S> {
6382
}
6483

6584
impl From<BoxedPayloadStream> for Payload {
85+
#[inline]
6686
fn from(payload: BoxedPayloadStream) -> Self {
6787
Payload::Stream { payload }
6888
}
6989
}
7090

7191
impl<S> Payload<S> {
72-
/// Takes current payload and replaces it with `None` value
92+
/// Takes current payload and replaces it with `None` value.
93+
#[must_use]
7394
pub fn take(&mut self) -> Payload<S> {
7495
mem::replace(self, Payload::None)
7596
}

0 commit comments

Comments
 (0)