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:
Ultra-Code 2025-03-03 18:40:24 +00:00
parent b8bdee0606
commit f0d6e1b25a
No known key found for this signature in database
GPG Key ID: C2A2C53574321095
2 changed files with 16 additions and 0 deletions

View File

@ -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

View File

@ -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)