From f0d6e1b25a65e4116730ffc631efdf1a022c7d54 Mon Sep 17 00:00:00 2001 From: Ultra-Code Date: Mon, 3 Mar 2025 18:40:24 +0000 Subject: [PATCH] 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 --- actix-http/CHANGES.md | 1 + actix-http/src/payload.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 23ebe43e..e0698a4a 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -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 diff --git a/actix-http/src/payload.rs b/actix-http/src/payload.rs index 7d476c55..11bb7882 100644 --- a/actix-http/src/payload.rs +++ b/actix-http/src/payload.rs @@ -69,6 +69,21 @@ impl From for Payload { } impl Payload { + /// Create Payload from bytes::Bytes + pub fn from_bytes(bytes: bytes::Bytes) -> Payload { + let (_, mut pl) = crate::h1::Payload::create(true); + pl.unread_data(bytes); + self::Payload::from(pl) + } + + /// Create Payload from `Vec` bytes + pub fn from_u8_bytes(u8_bytes: Vec) -> Payload { + 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 { mem::replace(self, Payload::None)