mirror of https://github.com/fafhrd91/actix-web
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
This commit is contained in:
parent
b8bdee0606
commit
f0d6e1b25a
|
@ -6,6 +6,7 @@
|
|||
|
||||
- Add `header::CLEAR_SITE_DATA` constant.
|
||||
- Add `Extensions::get_or_insert[_with]()` methods.
|
||||
- Add `dev::Payload::from_[bytes/u8_bytes]()` methods.
|
||||
|
||||
### Changed
|
||||
|
||||
|
|
|
@ -69,6 +69,21 @@ impl From<BoxedPayloadStream> for 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
|
||||
pub fn take(&mut self) -> Payload<S> {
|
||||
mem::replace(self, Payload::None)
|
||||
|
|
Loading…
Reference in New Issue